package eu.jonahbauer.wizard.client.libgdx.actors.game.overlay;

import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import eu.jonahbauer.wizard.client.libgdx.screens.GameScreen;

import java.util.stream.IntStream;

public class MakePredictionOverlay extends Overlay {

    private final int[] values;
    private final TextButton[] buttons;

    public MakePredictionOverlay(GameScreen gameScreen, int round) {
        this(gameScreen, IntStream.range(0, round + 2).toArray());
    }

    protected MakePredictionOverlay(GameScreen gameScreen, int[] values) {
        super(gameScreen);
        this.values = values;
        this.buttons = new TextButton[values.length];
    }

    @Override
    public VerticalGroup createContent() {
        var root = new VerticalGroup().columnCenter().space(10);

        var prompt = new Label("Please make your prediction:", data.skin);
        var buttonGroup = new HorizontalGroup().space(20);

        for (int i = 0; i < values.length; i++) {
            buttons[i] = new TextButton(String.valueOf(i), data.skin);
            buttonGroup.addActor(buttons[i]);
        }

        buttonGroup.addListener(new ClickListener() {
            private boolean finished = false;

            @Override
            public void clicked(InputEvent event, float x, float y) {
                if (finished) return;

                finished = true;
                var target = event.getTarget();
                for (int i = 0; i < values.length; i++) {
                    if (buttons[i] == target) {
                        System.out.println(values[i]);
                        break;
                    }
                }

                getRoot().remove();
            }
        });

        root.addActor(prompt);
        root.addActor(buttonGroup);

        return root;
    }
}