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

import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;
import eu.jonahbauer.wizard.client.libgdx.actors.game.CardActor;
import eu.jonahbauer.wizard.client.libgdx.screens.GameScreen;
import eu.jonahbauer.wizard.common.model.Card;

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
import static eu.jonahbauer.wizard.client.libgdx.actions.MyActions.*;

public class TrumpOverlay extends Overlay {

    private final String player;
    private final Card card;
    private final Card.Suit suit;

    private final CardActor trumpCardActor;

    public TrumpOverlay(GameScreen gameScreen, String player, Card card, Card.Suit suit) {
        super(gameScreen);
        this.player = player;
        this.card = card;
        this.suit = suit;
        this.trumpCardActor = gameScreen.getTrumpCardActor();
    }

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

        String text;

        if (player == null) {
            text = switch (suit) {
                case YELLOW -> "The trump suit is [#ffff00]yellow[#ffffff].";
                case GREEN -> "The trump suit is [#00ff00]green[#ffffff].";
                case BLUE -> "The trump suit is [#0000ff]blue[#ffffff].";
                case RED -> "The trump suit is [#ff0000]red[#ffffff].";
                default -> "There is no trump suit.";
            };
        } else {
            text = player + switch (suit) {
                case YELLOW -> " chose the trump suit [#ffff00]yellow[#ffffff].";
                case GREEN -> " chose the trump suit [#00ff00]green[#ffffff].";
                case BLUE -> " chose the trump suit [#0000ff]blue[#ffffff].";
                case RED -> " chose the trump suit [#ff0000]red[#ffffff].";
                default -> " has decided there will be no trump suit this round.";
            };
        }

        var label = new Label(text, data.skin);
        label.getStyle().font.getData().markupEnabled = true;
        label.setFontScale(1.5f);

        root.addActor(label);
        root.addActor(trumpCardActor);

        return root;
    }

    @Override
    public void show(Stage stage) {
        var root = getRoot();

        trumpCardActor.setCard(card);

        stage.addActor(root);
        stage.addAction(sequence(
                delay(2.0f),
                parallel(
                        sequence(
                                target(alpha(0.0f, .1f, Interpolation.pow2Out), root),
                                removeActor(root)
                        ),
                        sequence(
                                removeActorSilently(trumpCardActor),
                                target(changeParent(stage.getRoot()), trumpCardActor),
                                target(moveTo(10, 10, .25f), trumpCardActor)
                        )
                )
        ));
    }
}
