package eu.jonahbauer.wizard.client.libgdx.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import eu.jonahbauer.wizard.client.libgdx.WizardGame;

public class SessionListScreen extends MenuScreen {

    TextButton backButton;
    TextButton continueButton;
    TextButton createGameButton;

    List<String> gameList;
    ScrollPane listContainer;

    ClickListener listener = new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            var target = event.getListenerActor();
            if (target == backButton) {
                game.setScreen(new ServerScreen(game));
                sfxClick();
            } else if (target == continueButton) {
                game.setScreen(new WaitingScreen(game));
                sfxClick();
            } else if (target == createGameButton) {
                game.setScreen(new CreateGameScreen(game));
                sfxClick();
            }
        }
    };

    public SessionListScreen(WizardGame game) {
        super(game);
        this.game = game;
    }

    @Override
    public void show() {
        super.show();

        backButton = new TextButton("Zurück", skin);
        backButton.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.18f);
        backButton.setSize(120, backButton.getHeight());
        continueButton = new TextButton("Fortfahren", skin);
        continueButton.setPosition(WizardGame.WIDTH * 0.6f, WizardGame.HEIGHT * 0.45f);
        createGameButton = new TextButton("Spiel erstellen", skin);
        createGameButton.setPosition(WizardGame.WIDTH * 0.7f - continueButton.getWidth(), WizardGame.HEIGHT * 0.18f);
        createGameButton.setSize(120, backButton.getHeight());

        gameList = new List<>(skin);
        String[] items = new String[]{
                "testgame1",
                "testgame2",
                "testgame3",
                "testgame4",
                "testgame5",
                "testgame6",
                "testgame7",
                "testgame8",
                "testgame9",
                "testgame10",
                "testgame11",
                "testgame12",
                "testgame13",
                "testgame14"
        };
        gameList.setItems(items);
        //gameList.setPosition(WizardGame.WIDTH*0.3f, WizardGame.HEIGHT*0.2f);
        gameList.setSize(250, 400);

        listContainer = new ScrollPane(gameList);
        listContainer.setSize(250, 300);
        listContainer.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.3f);

        Gdx.input.setInputProcessor(stage);
        stage.addActor(backButton);
        stage.addActor(continueButton);
        stage.addActor(createGameButton);
        stage.addActor(listContainer);

        backButton.addListener(listener);
        continueButton.addListener(listener);
        createGameButton.addListener(listener);
    }

    @Override
    protected void renderInternal(float delta) {}
}
