package at.fos.ermodel.gui;

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 B1
        extends Stage {
    private final B1 thisActionMessageDialog;
    private final TextField nameTF;
    private final CheckBox createColumnCB;
    private final TextField columnNameTF;
    private final TextField relationNameTF;
    private final TextField relationNamePraefixTF;
    private final TextField columnPostfixTF;
    private final TextField datatypeTF;
    private final CheckBox isAutoincrementCB;
    private final CheckBox isKeyCB;
    private final CheckBox isNullableCB;
    private final CheckBox isUniqueCB;
    private final Button okBTN;
    private final String holdName;
    private Button cancelBTN;

    public B1(final A3 canvas, double xPos, double yPos, String name, boolean createColumn, String columnName, String relationName, String relationNamePraefix, String columnPostfix, String datatype, boolean isAutoincrement, boolean isKey, boolean isNullable, boolean isUnique) {
        String title = "Multivalue Attribute Transformation";


        this.holdName = name;

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


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


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

        this.createColumnCB.setDisable(true);
        if (createColumn) this.createColumnCB.setSelected(true);
        this.createColumnCB.setOnMouseEntered(event -> T3.fdssdf4354(B1.this.createColumnCB, C2.MouseSelectedColor));
        this.createColumnCB.setOnMouseExited(event -> B1.this.createColumnCB.setEffect(null));

        this.columnPostfixTF = new TextField(columnPostfix);
        this.columnPostfixTF.setOnMouseEntered(event -> T3.fdssdf4354(B1.this.columnPostfixTF, C2.MouseSelectedColor));
        this.columnPostfixTF.setOnMouseExited(event -> B1.this.columnPostfixTF.setEffect(null));

        this.columnNameTF = new TextField(columnName);
        this.columnNameTF.setOnMouseEntered(event -> T3.fdssdf4354(B1.this.columnNameTF, C2.MouseSelectedColor));
        this.columnNameTF.setOnMouseExited(event -> B1.this.columnNameTF.setEffect(null));

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

        this.relationNamePraefixTF = new TextField(relationNamePraefix);
        this.relationNamePraefixTF.setOnMouseEntered(event -> T3.fdssdf4354(B1.this.relationNamePraefixTF, C2.MouseSelectedColor));
        this.relationNamePraefixTF.setOnMouseExited(event -> B1.this.relationNamePraefixTF.setEffect(null));

        this.datatypeTF = new TextField(datatype);
        this.datatypeTF.setOnMouseEntered(event -> T3.fdssdf4354(B1.this.datatypeTF, C2.MouseSelectedColor));
        this.datatypeTF.setOnMouseExited(event -> B1.this.datatypeTF.setEffect(null));

        this.isAutoincrementCB = new CheckBox();
        this.isAutoincrementCB.setSelected(false);
        if (isAutoincrement) this.isAutoincrementCB.setSelected(true);
        this.isAutoincrementCB.setOnMouseEntered(event -> T3.fdssdf4354(B1.this.isAutoincrementCB, C2.MouseSelectedColor));
        this.isAutoincrementCB.setOnMouseExited(event -> B1.this.isAutoincrementCB.setEffect(null));

        this.isKeyCB = new CheckBox();
        this.isKeyCB.setSelected(false);
        if (isKey) this.isKeyCB.setSelected(true);
        this.isKeyCB.setOnMouseEntered(event -> T3.fdssdf4354(B1.this.isKeyCB, C2.MouseSelectedColor));
        this.isKeyCB.setOnMouseExited(event -> B1.this.isKeyCB.setEffect(null));

        this.isNullableCB = new CheckBox();
        this.isNullableCB.setSelected(false);
        if (isNullable) this.isNullableCB.setSelected(true);
        this.isNullableCB.setOnMouseEntered(event -> T3.fdssdf4354(B1.this.isNullableCB, C2.MouseSelectedColor));
        this.isNullableCB.setOnMouseExited(event -> B1.this.isNullableCB.setEffect(null));

        this.isUniqueCB = new CheckBox();
        this.isUniqueCB.setSelected(false);
        if (isUnique) this.isUniqueCB.setSelected(true);
        this.isUniqueCB.setOnMouseEntered(event -> T3.fdssdf4354(B1.this.isUniqueCB, C2.MouseSelectedColor));
        this.isUniqueCB.setOnMouseExited(event -> B1.this.isUniqueCB.setEffect(null));

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

                this.columnPostfixTF.setEffect(null);

                this.cancelBTN.setEffect(null);
            } else {
                this.okBTN.setEffect(null);
            }
        });
        this.okBTN.setOnAction(event -> {
            if (!canvas.getTabWithCanvas().getText().contains("*"))
                canvas.getTabWithCanvas().setText(canvas.getTabWithCanvas().getText() + "*");

            B1.this.setDataAndLeave();
        });
        this.okBTN.setOnKeyPressed(event -> {
            if (Objects.requireNonNull(event.getCode()) == KeyCode.ENTER) {
                B1.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 -> {
            B1.this.getScene().getRoot().setCursor(Cursor.HAND);
            T3.fdssdf4354(B1.this.cancelBTN, C2.MouseSelectedColor);
        });
        this.cancelBTN.setOnMouseExited(event -> {
            B1.this.getScene().getRoot().setCursor(Cursor.DEFAULT);
            B1.this.cancelBTN.setEffect(null);
            if (B1.this.cancelBTN.isFocused()) T3.fdssdf4354(B1.this.cancelBTN, C2.FocusOnComponentColor);

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

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


        });
        if (isKey) {
            this.isKeyCB.setDisable(true);
            this.isUniqueCB.setDisable(true);
            this.createColumnCB.setDisable(true);
            this.isNullableCB.setDisable(true);
        } else {

            this.isKeyCB.setDisable(true);
        }


        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 column:"), 0, 1);
        gridPane.add(this.createColumnCB, 1, 1);
        gridPane.add(new Label("Column name:"), 0, 2);
        gridPane.add(this.columnNameTF, 1, 2);
        gridPane.add(new Label("Relation name:"), 0, 3);
        gridPane.add(this.relationNameTF, 1, 3);
        gridPane.add(new Label("Relation name praefix:"), 0, 4);
        gridPane.add(this.relationNamePraefixTF, 1, 4);
        gridPane.add(new Label("Column postfix:"), 0, 5);
        gridPane.add(this.columnPostfixTF, 1, 5);
        gridPane.add(new Label("Column datatype:"), 0, 6);
        gridPane.add(this.datatypeTF, 1, 6);
        gridPane.add(new Label("Key:"), 0, 7);
        gridPane.add(this.isKeyCB, 1, 7);
        gridPane.add(new Label("Autoincrement:"), 0, 8);
        gridPane.add(this.isAutoincrementCB, 1, 8);
        gridPane.add(new Label("Nullable:"), 0, 9);
        gridPane.add(this.isNullableCB, 1, 9);
        gridPane.add(new Label("Unique:"), 0, 10);
        gridPane.add(this.isUniqueCB, 1, 10);


        FlowPane fp = new FlowPane();
        fp.setHgap(10.0D);
        fp.getChildren().addAll(this.okBTN, this.cancelBTN);
        gridPane.add(fp, 0, 11, 2, 1);


        Scene scene = new Scene(gridPane, 300.0D, 325.0D);
        setScene(scene);

        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.columnNameTF.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 getCreateColumnCB() {
        return this.createColumnCB;
    }


    public TextField getColumnNameTF() {
        return this.columnNameTF;
    }


    public TextField getColumnPostfixTF() {
        return this.columnPostfixTF;
    }


    public TextField getDatatypeTF() {
        return this.datatypeTF;
    }


    public CheckBox getIsAutoincrementCB() {
        return this.isAutoincrementCB;
    }


    public CheckBox getIsKeyCB() {
        return this.isKeyCB;
    }


    public CheckBox getIsNullableCB() {
        return this.isNullableCB;
    }


    public CheckBox getIsUniqueCB() {
        return this.isUniqueCB;
    }


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


    public TextField getRelationNamePraefixTF() {
        return this.relationNamePraefixTF;
    }
}


