package ctbrec.ui.controls;

import static javafx.scene.control.ButtonType.*;

import java.io.InputStream;
import java.util.Optional;

import ctbrec.ui.AutosizeAlert;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Region;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Dialogs {

    private Dialogs() {}

    private static Scene scene;

    public static void setScene(Scene scene) {
        Dialogs.scene = scene;
    }

    public static void showError(String header, String text, Throwable t) {
        showError(scene, header, text, t);
    }

    public static void showError(Scene parent, String header, String text, Throwable t) {
        Runnable r = () -> {
            Alert alert = new AutosizeAlert(Alert.AlertType.ERROR, parent);
            alert.setTitle("Error");
            alert.setHeaderText(header);
            String content = text;
            if (t != null) {
                content += " " + t.getLocalizedMessage();
            }
            alert.setContentText(content);
            if (parent != null) {
                Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
                stage.getScene().getStylesheets().addAll(parent.getStylesheets());
            }
            alert.showAndWait();
        };

        if (Platform.isFxApplicationThread()) {
            r.run();
        } else {
            Platform.runLater(r);
        }
    }

    public static Optional<String> showTextInput(Scene parent, String title, String header, String text) {
        Dialog<String> dialog = new Dialog<>();
        dialog.setTitle(title);
        dialog.setHeaderText(header);
        dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
        dialog.initModality(Modality.APPLICATION_MODAL);
        dialog.setResizable(true);
        InputStream icon = Dialogs.class.getResourceAsStream("/icon.png");
        Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(icon));
        if (parent != null) {
            stage.getScene().getStylesheets().addAll(parent.getStylesheets());
        }

        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 150, 10, 10));

        TextArea notes = new TextArea(text);
        notes.setPrefRowCount(3);
        grid.add(notes, 0, 0);
        dialog.getDialogPane().setContent(grid);

        Platform.runLater(notes::requestFocus);

        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == ButtonType.OK) {
                return notes.getText();
            }
            return null;
        });

        return dialog.showAndWait();
    }

    public static Boolean showCustomInput(Scene parent, String title, Region region) {
        Dialog<?> dialog = new Dialog<>();
        dialog.setTitle(title);
        dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
        dialog.initModality(Modality.APPLICATION_MODAL);
        dialog.setResizable(true);
        InputStream icon = Dialogs.class.getResourceAsStream("/icon.png");
        Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(icon));
        if (parent != null) {
            stage.getScene().getStylesheets().addAll(parent.getStylesheets());
        }
        dialog.getDialogPane().setContent(region);
        dialog.showAndWait();
        return dialog.getResult() == ButtonType.OK;
    }

    public static boolean showConfirmDialog(String title, String message, String header, Scene parent) {
        AutosizeAlert confirm = new AutosizeAlert(AlertType.CONFIRMATION, message, parent, YES, NO);
        confirm.setTitle(title);
        confirm.setHeaderText(header);
        confirm.showAndWait();
        return confirm.getResult() == ButtonType.YES;
    }

    public static ButtonType showShutdownDialog(Scene parent) {
        String message = "There are recordings in progress";
        AutosizeAlert confirm = new AutosizeAlert(AlertType.CONFIRMATION, "", parent, YES, FINISH, NO);
        confirm.setTitle("Shutdown");
        confirm.setHeaderText(message);
        ((Button) confirm.getDialogPane().lookupButton(ButtonType.YES)).setText("Shutdown Now");
        ((Button) confirm.getDialogPane().lookupButton(ButtonType.YES)).setDefaultButton(false);
        ((Button) confirm.getDialogPane().lookupButton(ButtonType.FINISH)).setText("Shutdown Gracefully");
        ((Button) confirm.getDialogPane().lookupButton(ButtonType.FINISH)).setDefaultButton(true);
        ((Button) confirm.getDialogPane().lookupButton(ButtonType.NO)).setText("Keep Running");
        ((Button) confirm.getDialogPane().lookupButton(ButtonType.NO)).setDefaultButton(false);
        confirm.showAndWait();
        return confirm.getResult();
    }
}
