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

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.Layout;
import eu.jonahbauer.wizard.client.libgdx.UiskinAtlas;
import eu.jonahbauer.wizard.client.libgdx.WizardGame;
import eu.jonahbauer.wizard.client.libgdx.listeners.KeyboardFocusManager;
import eu.jonahbauer.wizard.client.libgdx.state.Menu;

public class MainMenuScreen extends MenuScreen {

    private TextButton buttonPlay;
    private TextButton buttonQuit;
    private TextButton buttonInstruction;

    private final ChangeListener listener = new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            if (actor == buttonPlay) {
                game.getClient().execute(Menu.class, Menu::showConnectScreen);
            } else if (actor == buttonQuit) {
                Gdx.app.exit();
            } else if (actor == buttonInstruction) {
                game.getClient().execute(Menu.class, Menu::showInstructionScreen);
            }
        }
    };

    public MainMenuScreen(WizardGame game) {
        super(game);
    }

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

        int width = 160, height = 224;
        int left = 384, right = 384, top = 384, bottom = 192;
        var symbols = new Image[]{
                new Image(skin.getRegion(UiskinAtlas.SYMBOL_0)),
                new Image(skin.getRegion(UiskinAtlas.SYMBOL_1)),
                new Image(skin.getRegion(UiskinAtlas.SYMBOL_2)),
                new Image(skin.getRegion(UiskinAtlas.SYMBOL_3))
        };
        symbols[0].setPosition(left, bottom);
        symbols[1].setPosition(left, WizardGame.HEIGHT - top - height);
        symbols[2].setPosition(WizardGame.WIDTH - right - width, bottom);
        symbols[3].setPosition(WizardGame.WIDTH - right - width, WizardGame.HEIGHT - top - height);
        for (var symbol : symbols) {
            symbol.setSize(width, height);
            stage.addActor(symbol);
        }

        var buttonGroup = new VerticalGroup() {
            @Override
            public void layout() {
                float contentHeight = 0;
                for (Actor child : getChildren()) {
                    if (child instanceof Layout layout) {
                        contentHeight += layout.getPrefHeight();
                    } else {
                        contentHeight += child.getHeight();
                    }
                }

                space(Math.max(0, (getHeight() - contentHeight) / (getChildren().size - 1)));
                super.layout();
            }
        }.center().fill();
        buttonGroup.setPosition(WizardGame.WIDTH * 0.25f, 192 + 60f);
        buttonGroup.setSize(WizardGame.WIDTH * 0.5f, 504 - 120f);
        stage.addActor(buttonGroup);

        buttonPlay = new TextButton(messages.get("menu.main.play"), skin);
        buttonPlay.addListener(listener);
        buttonGroup.addActor(buttonPlay);

        buttonInstruction = new TextButton(messages.get("menu.main.instructions"), skin);
        buttonInstruction.addListener(listener);
        buttonGroup.addActor(buttonInstruction);

        buttonQuit = new TextButton(messages.get("menu.main.quit"), skin);
        buttonQuit.addListener(listener);
        buttonGroup.addActor(buttonQuit);

        stage.addCaptureListener(new KeyboardFocusManager(buttonPlay, buttonInstruction, buttonQuit));

        buttonPlay.setName("button_player");
        buttonInstruction.setName("button_instruction");
        buttonQuit.setName("button_quit");
    }
}
