package p.studio.window;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
import p.studio.Container;
import p.studio.StudioAppInfo;
import p.studio.projects.ProjectReference;
import p.studio.utilities.i18n.I18n;

import java.util.Objects;

public final class ProjectLoadingShieldView extends VBox {
    public ProjectLoadingShieldView(ProjectReference projectReference) {
        Objects.requireNonNull(projectReference, "projectReference");

        getStyleClass().add("studio-project-loading-shield");
        setPadding(new Insets(28));
        setSpacing(14);
        setAlignment(Pos.CENTER_LEFT);

        final Label markLabel = new Label("P");
        markLabel.getStyleClass().add("studio-project-loading-mark");

        final Label appTitle = new Label(Container.i18n().text(I18n.APP_TITLE));
        appTitle.getStyleClass().add("studio-project-loading-title");

        final Label appVersion = new Label(Container.i18n().format(I18n.SHIELD_VERSION, StudioAppInfo.version()));
        appVersion.getStyleClass().add("studio-project-loading-version");

        final VBox brandBlock = new VBox(4, appTitle, appVersion);
        final HBox brandRow = new HBox(14, buildMark(markLabel), brandBlock);
        brandRow.setAlignment(Pos.CENTER_LEFT);

        final Label projectLabel = new Label(projectReference.name());
        projectLabel.getStyleClass().add("studio-project-loading-project");

        final Label statusLabel = new Label(Container.i18n().format(I18n.SHIELD_LOADING_PROJECT, projectReference.name()));
        statusLabel.getStyleClass().add("studio-project-loading-status");

        final ProgressBar progressBar = new ProgressBar(0.18);
        progressBar.getStyleClass().add("studio-project-loading-progress");
        progressBar.setMaxWidth(Double.MAX_VALUE);

        final HBox phaseRow = new HBox(
                8,
                phasePill(Container.i18n().text(I18n.SHIELD_PHASE_INDEXING), true),
                phasePill(Container.i18n().text(I18n.SHIELD_PHASE_PREPARING), false),
                phasePill(Container.i18n().text(I18n.SHIELD_PHASE_RESTORING), false));
        phaseRow.setAlignment(Pos.CENTER_LEFT);

        final Label tipLabel = new Label(Container.i18n().text(I18n.SHIELD_TIP_SHORTCUTS));
        tipLabel.getStyleClass().add("studio-project-loading-tip");
        tipLabel.setWrapText(true);

        final Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, ignored -> {
                    statusLabel.setText(Container.i18n().format(I18n.SHIELD_LOADING_PROJECT, projectReference.name()));
                    tipLabel.setText(Container.i18n().text(I18n.SHIELD_TIP_SHORTCUTS));
                    progressBar.setProgress(0.24);
                }),
                new KeyFrame(Duration.millis(500), ignored -> {
                    statusLabel.setText(Container.i18n().text(I18n.SHIELD_STATUS_INDEXING));
                    tipLabel.setText(Container.i18n().text(I18n.SHIELD_TIP_WORKSPACES));
                    progressBar.setProgress(0.58);
                }),
                new KeyFrame(Duration.millis(1000), ignored -> {
                    statusLabel.setText(Container.i18n().text(I18n.SHIELD_STATUS_RESTORING));
                    tipLabel.setText(Container.i18n().text(I18n.SHIELD_TIP_ACTIVITY));
                    progressBar.setProgress(0.86);
                }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();

        sceneProperty().addListener((ignored, oldScene, newScene) -> {
            if (newScene == null) {
                timeline.stop();
            }
        });

        getChildren().addAll(
                brandRow,
                projectLabel,
                statusLabel,
                progressBar,
                phaseRow,
                tipLabel);
    }

    private StackPane buildMark(Label markLabel) {
        final StackPane mark = new StackPane(markLabel);
        mark.getStyleClass().add("studio-project-loading-mark-shell");
        return mark;
    }

    private Region phasePill(String text, boolean active) {
        final Label label = new Label(text);
        label.getStyleClass().add("studio-project-loading-phase");
        if (active) {
            label.getStyleClass().add("studio-project-loading-phase-active");
        }
        return label;
    }
}
