package at.fos.ermodel.gui;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.*;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.util.Objects;


public class A9
        extends Stage {
    private final A9 thisActionMessageDialog;
    private final TextField nameTF;
    private final CheckBox createRelationCB;
    private final TextField relationNameTF;
    private final TextField relationPraefixTF;
    private final CheckBox createInsertStatementsCB;
    private final TextField numberRowsToInsertTF;
    private final Button okBTN;
    private final String holdName;
    private Button cancelBTN;

    public A9(final A3 canvas, double xPos, double yPos, String name, boolean createRelation, String relationName, String relationPraefix, boolean createInsertStatements, long numberRowsToInsert, boolean setCreationRelationChangeable) {

        String title = "ES/RS Transformation";


        this.holdName = name;

        this.thisActionMessageDialog = this;
        setTitle(title);
        initModality(Modality.APPLICATION_MODAL);


        this.nameTF = new TextField(name);
        this.nameTF.setDisable(true);


        this.createRelationCB = new CheckBox();
        this.createRelationCB.setSelected(false);

        if (!setCreationRelationChangeable) this.createRelationCB.setDisable(true);
        if (createRelation) this.createRelationCB.setSelected(true);

        this.relationPraefixTF = new TextField(relationPraefix);
        this.relationPraefixTF.setOnMouseEntered(event -> T3.fdssdf4354(A9.this.relationPraefixTF, C2.MouseSelectedColor));
        this.relationPraefixTF.setOnMouseExited(event -> A9.this.relationPraefixTF.setEffect(null));
        this.relationNameTF = new TextField(relationName);
        this.relationNameTF.setOnMouseEntered(event -> T3.fdssdf4354(A9.this.relationNameTF, C2.MouseSelectedColor));
        this.relationNameTF.setOnMouseExited(event -> A9.this.relationNameTF.setEffect(null));


        this.createInsertStatementsCB = new CheckBox();
        this.createInsertStatementsCB.setSelected(true);
        this.createInsertStatementsCB.setDisable(true);
        if (createInsertStatements) this.createInsertStatementsCB.setSelected(true);
        this.createInsertStatementsCB.setOnAction(new EventHandler<>() {
            public void handle(ActionEvent event) {
                A9.this.numberRowsToInsertTF.setDisable(!A9.this.createInsertStatementsCB.isSelected());
            }
        });
        this.numberRowsToInsertTF = new TextField(String.valueOf(numberRowsToInsert));
        this.numberRowsToInsertTF.setDisable(true);
        if (createInsertStatements) this.numberRowsToInsertTF.setDisable(false);

        this.okBTN = new Button("Ok");
        this.okBTN.setBackground(new Background(new BackgroundFill(C2.ButtonBackgroundColor, new CornerRadii(5.0D), Insets.EMPTY)));
        this.okBTN.setOnMouseEntered(event -> {
            A9.this.getScene().getRoot().setCursor(Cursor.HAND);
            T3.fdssdf4354(A9.this.okBTN, C2.MouseSelectedColor);
        });
        this.okBTN.setOnMouseExited(event -> {
            A9.this.getScene().getRoot().setCursor(Cursor.DEFAULT);
            A9.this.okBTN.setEffect(null);
            if (A9.this.okBTN.isFocused()) T3.fdssdf4354(A9.this.okBTN, C2.FocusOnComponentColor);
        });
        this.okBTN.focusedProperty().addListener((obs, oldVal, newVal) -> {
            if (this.okBTN.isFocused()) {
                T3.fdssdf4354(this.okBTN, C2.FocusOnComponentColor);

                this.relationPraefixTF.setEffect(null);

                this.cancelBTN.setEffect(null);
            } else {
                this.okBTN.setEffect(null);
            }
        });
        this.okBTN.setOnAction(event -> {
            A9.this.setDataAndLeave();

            if (!canvas.getTabWithCanvas().getText().contains("*"))
                canvas.getTabWithCanvas().setText(canvas.getTabWithCanvas().getText() + "*");
        });
        this.okBTN.setOnKeyPressed(event -> {
            if (Objects.requireNonNull(event.getCode()) == KeyCode.ENTER) {
                A9.this.setDataAndLeave();

                if (!canvas.getTabWithCanvas().getText().contains("*"))
                    canvas.getTabWithCanvas().setText(canvas.getTabWithCanvas().getText() + "*");
            }


        });
        this.cancelBTN = new Button("Cancel");
        this.cancelBTN.setBackground(new Background(new BackgroundFill(C2.ButtonBackgroundColor, new CornerRadii(5.0D), Insets.EMPTY)));
        this.cancelBTN.setOnMouseEntered(event -> {
            A9.this.getScene().getRoot().setCursor(Cursor.HAND);
            T3.fdssdf4354(A9.this.cancelBTN, C2.MouseSelectedColor);
        });
        this.cancelBTN.setOnMouseExited(event -> {
            A9.this.getScene().getRoot().setCursor(Cursor.DEFAULT);
            A9.this.cancelBTN.setEffect(null);
            if (A9.this.cancelBTN.isFocused()) T3.fdssdf4354(A9.this.cancelBTN, C2.FocusOnComponentColor);

        });
        this.cancelBTN.focusedProperty().addListener((obs, oldVal, newVal) -> {
            if (this.cancelBTN.isFocused()) {
                T3.fdssdf4354(this.cancelBTN, C2.FocusOnComponentColor);

                this.relationPraefixTF.setEffect(null);
                this.okBTN.setEffect(null);
            } else {
                this.cancelBTN.setEffect(null);
            }
        });
        this.cancelBTN.setOnAction(event -> A9.this.setNothingAndLeave());
        this.cancelBTN.setOnKeyPressed(event -> {
            if (Objects.requireNonNull(event.getCode()) == KeyCode.ENTER) {
                A9.this.setNothingAndLeave();
            }


        });
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10.0D, 10.0D, 10.0D, 10.0D));
        gridPane.setVgap(4.0D);
        gridPane.setHgap(4.0D);

        gridPane.add(new Label("Name:"), 0, 0);
        gridPane.add(this.nameTF, 1, 0);
        gridPane.add(new Label("Create relation:"), 0, 1);
        gridPane.add(this.createRelationCB, 1, 1);
        gridPane.add(new Label("Relation name:"), 0, 2);
        gridPane.add(this.relationNameTF, 1, 2);
        gridPane.add(new Label("Relation praefix:"), 0, 3);
        gridPane.add(this.relationPraefixTF, 1, 3);
        gridPane.add(new Label("Create inserts:"), 0, 4);
        gridPane.add(this.createInsertStatementsCB, 1, 4);
        gridPane.add(new Label("Number rows:"), 0, 5);
        gridPane.add(this.numberRowsToInsertTF, 1, 5);
        FlowPane fp = new FlowPane();
        fp.setHgap(10.0D);


        fp.getChildren().addAll(this.okBTN, this.cancelBTN);
        gridPane.add(fp, 0, 6, 2, 1);


        Scene scene = new Scene(gridPane, 300.0D, 205.0D);

        setScene(scene);
        sizeToScene();

        setX(xPos);
        setY(yPos);
        if (yPos + scene.getHeight() > canvas.getView().getHeight() - 5.0D) {
            setY(yPos - scene.getHeight() - 20.0D);
        }
        if (xPos + scene.getWidth() > canvas.getView().getWidth() - 5.0D) {
            setX(xPos - scene.getWidth() - 20.0D);
        }

        this.relationNameTF.requestFocus();


        sizeToScene();
        showAndWait();
    }


    private void setDataAndLeave() {
        if (this.nameTF.getText() == null || this.nameTF.getText().length() == 0)
            return;
        this.thisActionMessageDialog.close();
    }


    private void setNothingAndLeave() {
        this.nameTF.setText(this.holdName);
        this.thisActionMessageDialog.close();
    }


    public CheckBox getCreateRelationCB() {
        return this.createRelationCB;
    }


    public TextField getRelationNameTF() {
        return this.relationNameTF;
    }


    public TextField getRelationPraefixTF() {
        return this.relationPraefixTF;
    }


    public CheckBox getCreateInsertStatementsCB() {
        return this.createInsertStatementsCB;
    }


    public TextField getNumberRowsToInsertTF() {
        return this.numberRowsToInsertTF;
    }
}


