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

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import eu.jonahbauer.wizard.client.libgdx.WizardGame;
import eu.jonahbauer.wizard.client.libgdx.actors.PadOfTruth;
import eu.jonahbauer.wizard.client.libgdx.screens.GameScreen;
import eu.jonahbauer.wizard.client.libgdx.util.AnimationTimings;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
import static eu.jonahbauer.wizard.client.libgdx.actions.MyActions.changeParent;
import static eu.jonahbauer.wizard.client.libgdx.screens.GameScreen.PAD_OF_TRUTH_POSITION;
import static eu.jonahbauer.wizard.client.libgdx.util.AnimationTimings.OVERLAY_SHARED_ELEMENT;

public class ScoreOverlay extends Overlay {
    private final boolean finalScores;

    private final PadOfTruth padOfTruth;
    private TextButton back;

    public ScoreOverlay(@NotNull GameScreen gameScreen, boolean finalScores) {
        super(gameScreen, Long.MAX_VALUE);
        this.finalScores = finalScores;
        this.padOfTruth = Objects.requireNonNull(gameScreen.getPadOfTruth());
    }

    @Override
    protected Actor createContent() {
        var group = new Group();
        group.addActor(padOfTruth);

        if (finalScores) {
            back = new TextButton(messages.get("game.overlay.scores.return_to_session"), skin);
            back.setPosition(
                    (WizardGame.WIDTH - back.getWidth()) / 2,
                    (WizardGame.HEIGHT - padOfTruth.getHeight()) / 2 + 30 - back.getHeight()
            );
            back.setVisible(false);
            back.addListener(new ChangeListener() {
                @Override
                public void changed(ChangeEvent event, Actor actor) {
                    screen.showSessionScreen();
                }
            });
            group.addActor(back);
        }

        return group;
    }

    @Override
    protected void show(Group parent) {
        var root = getRoot().fill();
        super.show(parent);

        if (finalScores) {
            root.addAction(sequence(
                    run(() -> padOfTruth.setEnabled(false)),
                    parallel(
                            targeting(padOfTruth, scaleTo(1, 1, OVERLAY_SHARED_ELEMENT)),
                            targeting(padOfTruth, moveTo((WizardGame.WIDTH - padOfTruth.getWidth()) / 2,(WizardGame.HEIGHT - padOfTruth.getHeight()) / 2 + 50, OVERLAY_SHARED_ELEMENT))
                    ),
                    delay(AnimationTimings.OVERLAY_HOLD),
                    targeting(back, visible(true))
            ));
        } else {
            root.addAction(sequence(
                    run(() -> padOfTruth.setEnabled(false)),
                    parallel(
                            targeting(padOfTruth, scaleTo(1, 1, OVERLAY_SHARED_ELEMENT)),
                            targeting(padOfTruth, moveTo((WizardGame.WIDTH - padOfTruth.getWidth()) / 2,(WizardGame.HEIGHT - padOfTruth.getHeight()) / 2, OVERLAY_SHARED_ELEMENT))
                    ),
                    delay(AnimationTimings.OVERLAY_HOLD),
                    parallel(
                            targeting(padOfTruth, scaleTo(PadOfTruth.COLLAPSED_SCALE, PadOfTruth.COLLAPSED_SCALE, OVERLAY_SHARED_ELEMENT)),
                            targeting(padOfTruth, moveTo(PAD_OF_TRUTH_POSITION.x, PAD_OF_TRUTH_POSITION.y, OVERLAY_SHARED_ELEMENT) )
                    ),
                    targeting(padOfTruth, changeParent(screen.getContentRoot())),
                    run(() -> padOfTruth.setEnabled(true)),
                    run(this::close)
            ));
        }
    }
}
