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

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.Layout;
import eu.jonahbauer.wizard.client.libgdx.UiskinAtlas;
import eu.jonahbauer.wizard.client.libgdx.WizardGame;
import lombok.AccessLevel;
import lombok.Getter;

public abstract class MenuScreen extends WizardScreen {
    private Image[] corners;
    @Getter(value = AccessLevel.PROTECTED, lazy = true)
    private final HorizontalGroup buttonGroup = createButtonGroup();

    @Getter(value = AccessLevel.PROTECTED, lazy = true)
    private final TextField.TextFieldStyle textFieldErrorStyle = skin.get("error", TextField.TextFieldStyle.class);
    @Getter(value = AccessLevel.PROTECTED, lazy = true)
    private final ScrollPane.ScrollPaneStyle scrollPaneErrorStyle = skin.get("error", ScrollPane.ScrollPaneStyle.class);
    @Getter(value = AccessLevel.PROTECTED, lazy = true)
    private final SelectBox.SelectBoxStyle selectBoxErrorStyle = skin.get("error", SelectBox.SelectBoxStyle.class);

    @Getter(AccessLevel.PROTECTED)
    private Image title;

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

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

        corners = new Image[4];
        corners[0] = new Image(skin.getRegion(UiskinAtlas.CORNER_TOP_LEFT));
        corners[1] = new Image(skin.getRegion(UiskinAtlas.CORNER_BOTTOM_LEFT));
        corners[2] = new Image(skin.getRegion(UiskinAtlas.CORNER_BOTTOM_RIGHT));
        corners[3] = new Image(skin.getRegion(UiskinAtlas.CORNER_TOP_RIGHT));
        for (var corner : corners) {
            stage.addActor(corner);
        }

        title = new Image(skin.getRegion(UiskinAtlas.TITLE));
        title.setSize(810, 192);
        title.setPosition(555, WizardGame.HEIGHT - 192 - 96);
        stage.addActor(title);
    }

    private HorizontalGroup createButtonGroup() {
        var group = new HorizontalGroup() {
            @Override
            public void layout() {
                float contentWidth = 0;
                for (Actor child : getChildren()) {
                    if (child instanceof Layout layout) {
                        contentWidth += layout.getPrefWidth();
                    } else {
                        contentWidth += child.getWidth();
                    }
                }

                space(Math.max(0, (getWidth() - contentWidth) / (getChildren().size - 1)));
                super.layout();
            }

            @Override
            protected void sizeChanged() {
                setPosition((WizardGame.WIDTH - getWidth()) / 2f, getY());
            }
        };
        group.setSize(WizardGame.WIDTH * 0.45f, 125);
        group.setY(WizardGame.HEIGHT * 0.15f);
        group.center();
        stage.addActor(group);
        return group;
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);

        var worldWidth = viewport.getWorldWidth();
        var worldHeight = viewport.getWorldHeight();
        var offsetX = (worldWidth - WizardGame.WIDTH) / 2;
        var offsetY = (worldHeight - WizardGame.HEIGHT) / 2;

        corners[0].setPosition(- offsetX, worldHeight - corners[0].getHeight() - offsetY);
        corners[1].setPosition(- offsetX, - offsetY);
        corners[2].setPosition(worldWidth - corners[2].getWidth() - offsetX, - offsetY);
        corners[3].setPosition(worldWidth - corners[3].getWidth() - offsetX, worldHeight - corners[3].getHeight() - offsetY);
    }
}
