package at.fos.ermodel.gui;

import javafx.event.Event;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;

import java.util.Optional;

public class C1 {
    private Optional<ButtonType> result;

    public C1(Window window, Alert.AlertType alertType, String title, String header, String contentText, Boolean showAndWait, Image image, String okBtnText, String cancelBtnText, Color btnBackgroundColor, Color btnSelectedColor, Color btnFocusColor) {
        Alert alert = setAlert(window, alertType, title, header, contentText, image);

        Button button = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
        if (button != null) {
            setButtonLookAndFeel(button, okBtnText, btnBackgroundColor, btnSelectedColor, btnFocusColor);
        }

        button = (Button) alert.getDialogPane().lookupButton(ButtonType.CANCEL);
        if (button != null)
            setButtonLookAndFeel(button, cancelBtnText, btnBackgroundColor, btnSelectedColor, btnFocusColor);

        button = (Button) alert.getDialogPane().lookupButton(ButtonType.APPLY);
        if (button != null) setButtonLookAndFeel(button, null, btnBackgroundColor, btnSelectedColor, btnFocusColor);

        button = (Button) alert.getDialogPane().lookupButton(ButtonType.CLOSE);
        if (button != null) setButtonLookAndFeel(button, null, btnBackgroundColor, btnSelectedColor, btnFocusColor);

        button = (Button) alert.getDialogPane().lookupButton(ButtonType.FINISH);
        if (button != null) setButtonLookAndFeel(button, null, btnBackgroundColor, btnSelectedColor, btnFocusColor);

        button = (Button) alert.getDialogPane().lookupButton(ButtonType.NEXT);
        if (button != null) setButtonLookAndFeel(button, null, btnBackgroundColor, btnSelectedColor, btnFocusColor);

        button = (Button) alert.getDialogPane().lookupButton(ButtonType.NO);
        if (button != null) setButtonLookAndFeel(button, null, btnBackgroundColor, btnSelectedColor, btnFocusColor);

        button = (Button) alert.getDialogPane().lookupButton(ButtonType.PREVIOUS);
        if (button != null) setButtonLookAndFeel(button, null, btnBackgroundColor, btnSelectedColor, btnFocusColor);

        button = (Button) alert.getDialogPane().lookupButton(ButtonType.YES);
        if (button != null) setButtonLookAndFeel(button, null, btnBackgroundColor, btnSelectedColor, btnFocusColor);

        if (showAndWait) {
            this.result = alert.showAndWait();
        } else {
            alert.show();
        }
    }

    private void setButtonLookAndFeel(final Button button, String buttonText, Color btnBackgroundColor, final Color btnSelectedColor, final Color btnFocusColor) {
        button.setBackground(new Background(new BackgroundFill(btnBackgroundColor, new CornerRadii(5.0D), Insets.EMPTY)));
        button.setOnMouseEntered(event -> {
            button.getScene().getRoot().setCursor(Cursor.HAND);
            T3.fdssdf4354(button, btnSelectedColor);
        });
        button.setOnMouseExited(event -> {
            button.getScene().getRoot().setCursor(Cursor.DEFAULT);
            button.setEffect(null);
            if (button.isFocused()) {
                T3.fdssdf4354(button, btnFocusColor);
            } else {

                button.setEffect(null);
            }
        });
        button.focusedProperty().addListener((obs, oldVal, newVal) -> {
            if (button.isFocused()) {
                T3.fdssdf4354(button, btnSelectedColor);
            } else {
                button.setEffect(null);
            }
        });


        if (buttonText != null) button.setText(buttonText);

    }


    private Alert setAlert(Window window, Alert.AlertType alertType, String title, String header, String contentText, Image image) {
        Alert alert = new Alert(alertType);
        alert.setTitle(title);
        alert.setHeaderText(header);
        alert.setContentText(contentText);
        alert.initOwner(window);
        alert.initStyle(StageStyle.DECORATED);


        StackPane iv = switch (alertType) {
            case ERROR -> C2.alerterror;
            case INFORMATION -> C2.alertinformation;
            default -> C2.alertconfirmation;
        };
        alert.setGraphic(iv);

        alert.getDialogPane().getChildren().stream().filter(node -> node instanceof Label).forEach(node -> ((Label) node).setMinHeight(Double.NEGATIVE_INFINITY));
        alert.getDialogPane().getChildren().stream().filter(node -> node instanceof Label).forEach(node -> ((Label) node).setMinWidth(Double.NEGATIVE_INFINITY));
        alert.getDialogPane().getChildren().stream().filter(node -> node instanceof Label).forEach(node -> ((Label) node).setMaxWidth(Double.NEGATIVE_INFINITY));

        alert.getDialogPane().setBackground(new Background(new BackgroundFill(Color.GAINSBORO, CornerRadii.EMPTY, Insets.EMPTY)));

        Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();


        if (image != null) {
            stage.getIcons().add(image);
        }


        stage.setOnCloseRequest(Event::consume);


        stage.centerOnScreen();

        stage.setAlwaysOnTop(true);

        return alert;
    }


    public Optional<ButtonType> getResult() {
        return this.result;
    }
}


