package com.ghost.technic;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.ghost.technic.screen.BattlefieldScreen;
import com.ghost.technic.server.Player;
import com.ghost.technic.server.game.GameActionService;
import com.ghost.technic.server.game.GameState;
import com.ghost.technic.server.game.event.EventBus;

/** {@link com.badlogic.gdx.ApplicationListener} implementation shared by all platforms. */
public class TechnicClient extends ApplicationAdapter {

    private Skin skin;
    private Screen currentScreen;
    private AssetManager assetManager;
    private GameState gameState;
    private GameActionService gameActionService;

    @Override
    public void create() {

        skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
        assetManager = new AssetManager();
        assetManager.load("cards/cards.atlas", TextureAtlas.class);
        assetManager.finishLoading();
        Gdx.app.log("TechnicClient | INFO", "Finished loading assets!");

        gameState = new GameState(new EventBus());
        gameActionService = new GameActionService(gameState);

        Player playerOne = Player.builder().name("Alice").build();
        Player playerTwo = Player.builder().name("Bob").build();
        gameState.setPlayers(playerOne, playerTwo);
        gameState.setup();

        currentScreen = new BattlefieldScreen(skin); // store reference
        currentScreen.show();
    }

    @Override
    public void render() {
        if (currentScreen != null) {
            currentScreen.render(Gdx.graphics.getDeltaTime());
        }
    }

    @Override
    public void resize(int width, int height) {
        if (currentScreen != null) {
            currentScreen.resize(width, height);
        }
    }

    @Override
    public void dispose() {
        if (currentScreen != null) {
            currentScreen.dispose();
        }
        skin.dispose();
    }
}
