package eu.jonahbauer.wizard.client.libgdx.actions.overlay;

import com.badlogic.gdx.scenes.scene2d.Actor;
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.VerticalGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import eu.jonahbauer.wizard.client.libgdx.actors.CardActor;
import eu.jonahbauer.wizard.client.libgdx.screens.GameScreen;
import eu.jonahbauer.wizard.common.model.Card;
import org.jetbrains.annotations.NotNull;

import java.util.EnumMap;

public class PickTrumpOverlay extends Overlay implements InteractionOverlay {

    private final boolean allowNone;

    private final EnumMap<Card.Suit, CardActor> cards = new EnumMap<>(Card.Suit.class);

    public PickTrumpOverlay(@NotNull GameScreen gameScreen, long timeout, boolean allowNone) {
        super(gameScreen, timeout);
        this.allowNone = allowNone;
    }

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

        var prompt = new Label(messages.get("game.overlay.pick_trump.prompt"), skin);
        var cardGroup = new HorizontalGroup().space(20);

        cards.put(Card.Suit.RED, new CardActor(Card.Suit.RED, atlas));
        cards.put(Card.Suit.GREEN, new CardActor(Card.Suit.GREEN, atlas));
        cards.put(Card.Suit.BLUE, new CardActor(Card.Suit.BLUE, atlas));
        cards.put(Card.Suit.YELLOW, new CardActor(Card.Suit.YELLOW, atlas));
        if (allowNone) {
            cards.put(Card.Suit.NONE, new CardActor(Card.Suit.NONE, atlas));
        }

        cards.values().forEach(cardGroup::addActor);

        cardGroup.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                if (isClosing()) return;
                var target = event.getTarget();
                for (Card.Suit suit : Card.Suit.values()) {
                    if (cards.get(suit) == target) {
                        screen.onSuitClicked(suit);
                        break;
                    }
                }
            }
        });

        root.addActor(prompt);
        root.addActor(cardGroup);

        return root;
    }
}
