package p.studio.workspaces.assets.wizards;

import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
import p.studio.Container;
import p.studio.utilities.i18n.I18n;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

public final class DeleteAssetDialog {
    private final Stage stage;
    private final AtomicReference<Boolean> result = new AtomicReference<>(false);
    private final String assetName;
    private final TextField confirmationField = new TextField();

    private DeleteAssetDialog(Window owner, String assetName) {
        this.assetName = Objects.requireNonNull(assetName, "assetName").trim();
        this.stage = new Stage();
        stage.initOwner(owner);
        stage.initModality(Modality.WINDOW_MODAL);
        stage.setTitle(Container.i18n().text(I18n.ASSETS_DELETE_DIALOG_TITLE));
        stage.setScene(new Scene(buildRoot(), 520, 240));
        stage.getScene().getStylesheets().add(Container.theme().getDefaultTheme());
    }

    public static boolean showAndWait(Window owner, String assetName) {
        final DeleteAssetDialog dialog = new DeleteAssetDialog(owner, assetName);
        dialog.stage.showAndWait();
        return Optional.ofNullable(dialog.result.get()).orElse(false);
    }

    private VBox buildRoot() {
        final Label title = new Label(Container.i18n().text(I18n.ASSETS_DELETE_DIALOG_TITLE));
        title.getStyleClass().add("studio-launcher-section-title");

        final Label description = new Label(Container.i18n().format(I18n.ASSETS_DELETE_DIALOG_DESCRIPTION, assetName));
        description.getStyleClass().add("studio-launcher-subtitle");
        description.setWrapText(true);

        confirmationField.setPromptText(Container.i18n().text(I18n.ASSETS_DELETE_DIALOG_PROMPT));
        confirmationField.setMaxWidth(Double.MAX_VALUE);

        final Label note = new Label(Container.i18n().text(I18n.ASSETS_DELETE_DIALOG_NOTE));
        note.getStyleClass().add("studio-launcher-feedback");
        note.setWrapText(true);

        final Button confirmButton = new Button();
        confirmButton.textProperty().bind(Container.i18n().bind(I18n.ASSETS_DELETE_DIALOG_CONFIRM));
        confirmButton.getStyleClass().addAll("studio-button", "studio-button-danger");
        confirmButton.disableProperty().bind(Bindings.createBooleanBinding(
                () -> !assetName.equals(confirmationField.getText().trim()),
                confirmationField.textProperty()));
        confirmButton.setOnAction(ignored -> {
            result.set(true);
            stage.close();
        });

        final Button cancelButton = new Button();
        cancelButton.textProperty().bind(Container.i18n().bind(I18n.WIZARD_CANCEL));
        cancelButton.getStyleClass().addAll("studio-button", "studio-button-cancel");
        cancelButton.setOnAction(ignored -> stage.close());

        final HBox actions = new HBox(12, confirmButton, cancelButton);
        actions.setAlignment(Pos.CENTER_RIGHT);

        final VBox root = new VBox(16, title, description, confirmationField, note, actions);
        root.setPadding(new Insets(24));
        VBox.setVgrow(confirmationField, Priority.NEVER);
        return root;
    }
}
