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.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
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 PlayColoredCardOverlay extends Overlay implements InteractionOverlay {

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

    private final Card card;

    public PlayColoredCardOverlay(@NotNull GameScreen gameScreen, long timeout, @NotNull Card card,
                                  @NotNull Card red, @NotNull Card green, @NotNull Card blue, @NotNull Card yellow)
    {
        super(gameScreen, timeout);
        this.card = card;
        this.cards.put(Card.Suit.RED, red);
        this.cards.put(Card.Suit.GREEN, green);
        this.cards.put(Card.Suit.BLUE, blue);
        this.cards.put(Card.Suit.YELLOW, yellow);
    }

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

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

        var card = new CardActor(this.card, atlas);
        root.addActorAt(0, card);
        root.padTop(- CardActor.PREF_HEIGHT);

        actors.put(Card.Suit.RED, new CardActor(Card.Suit.RED, atlas));
        actors.put(Card.Suit.GREEN, new CardActor(Card.Suit.GREEN, atlas));
        actors.put(Card.Suit.BLUE, new CardActor(Card.Suit.BLUE, atlas));
        actors.put(Card.Suit.YELLOW, new CardActor(Card.Suit.YELLOW, atlas));
        actors.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 (actors.get(suit) == target) {
                        screen.onCardClicked(cards.get(suit));
                        break;
                    }
                }
            }
        });

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

        var cancel = new TextButton(messages.get("game.overlay.play_colored_card.cancel"), skin, "simple");
        cancel.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                close();
            }
        });
        root.addActor(cancel);

        return root;
    }
}
