package at.fos.ermodel.gui;

import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;


public class A6
        extends Stage {
    private final A6 thisActionMessageDialog;
    private final TextField p_tTF;
    private final TextField d_ndTF;
    private final String holdpartial_or_total;
    private final String holddisjunct_or_notdisjunct;
    private boolean doNothing;

    public A6(final A3 canvas, double xPos, double yPos, String partial_or_total, String disjunct_or_notdisjunct) {

        String title = "Enter P-T-D-ND";

        this.doNothing = false;

        if (partial_or_total == null) partial_or_total = "p";
        if (disjunct_or_notdisjunct == null) disjunct_or_notdisjunct = "nd";
        this.holdpartial_or_total = partial_or_total;
        this.holddisjunct_or_notdisjunct = disjunct_or_notdisjunct;

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


        this.p_tTF = new TextField(partial_or_total);
        this.p_tTF.setPrefWidth(50.0D);
        this.p_tTF.textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.length() == 1 && newValue.charAt(0) != 'p' && newValue.charAt(0) != 't') {
                newValue = "p";

                this.p_tTF.setText(newValue);

                return;
            }
            if (newValue.length() > 1) {
                newValue = oldValue;
                this.p_tTF.setText(newValue);
                return;
            }
            if (newValue.length() == 1 && (newValue.charAt(0) == 'p' || newValue.charAt(0) == 't')) {
                this.p_tTF.setText(newValue);
            }
        });
        this.p_tTF.setOnMouseEntered(event -> T3.fdssdf4354(A6.this.p_tTF, C2.MouseSelectedColor));
        this.p_tTF.setOnMouseExited(event -> A6.this.p_tTF.setEffect(null));
        this.p_tTF.setOnKeyPressed(new EventHandler<>() {

            public void handle(KeyEvent event) {
                switch (event.getCode()) {
                    case ENTER -> A6.this.d_ndTF.requestFocus();
                    case ESCAPE -> A6.this.setNothingAndLeave();
                }

            }
        });
        this.d_ndTF = new TextField(disjunct_or_notdisjunct);
        this.d_ndTF.setPrefWidth(50.0D);
        this.d_ndTF.textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.length() == 1 && newValue.charAt(0) != 'd') {
                newValue = "d";

                this.d_ndTF.setText(newValue);
                return;
            }
            if (newValue.length() == 2 && !newValue.equals("nd")) {
                newValue = "nd";
                this.d_ndTF.setText(newValue);
                return;
            }
            if (newValue.length() > 2) {
                newValue = oldValue;
                this.d_ndTF.setText(newValue);
                return;
            }
            if (newValue.length() == 1 && newValue.charAt(0) == 'd') {
                this.d_ndTF.setText(newValue);
            }
        });
        this.d_ndTF.setOnMouseEntered(event -> T3.fdssdf4354(A6.this.d_ndTF, C2.MouseSelectedColor));
        this.d_ndTF.setOnMouseExited(event -> A6.this.d_ndTF.setEffect(null));
        this.d_ndTF.setOnKeyPressed(event -> {
            switch (event.getCode()) {
                case ENTER -> {
                    A6.this.setDataAndLeave();
                    if (!canvas.getTabWithCanvas().getText().contains("*"))
                        canvas.getTabWithCanvas().setText(canvas.getTabWithCanvas().getText() + "*");
                }
                case ESCAPE -> A6.this.setNothingAndLeave();
            }

        });
        GridPane gPane = new GridPane();
        gPane.setPadding(new Insets(10.0D, 10.0D, 10.0D, 10.0D));

        gPane.setHgap(4.0D);

        gPane.add(new Label("ESC->Cancel"), 0, 0);
        gPane.add(new Label("Partial[p]/Total[t]:"), 0, 1);
        gPane.add(this.p_tTF, 1, 1);
        gPane.add(new Label("Disjunct[d]/Not disjunct[nd]:"), 0, 2);
        gPane.add(this.d_ndTF, 1, 2);


        initStyle(StageStyle.UNDECORATED);
        Scene scene = new Scene(gPane, 230.0D, 90.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.p_tTF.requestFocus();
        this.p_tTF.selectPositionCaret(0);
        this.p_tTF.selectEndOfNextWord();

        sizeToScene();
        showAndWait();
    }


    private void setDataAndLeave() {
        if (this.p_tTF.getText() == null || this.p_tTF.getText().length() == 0) this.p_tTF.setText("p");
        if (this.d_ndTF.getText() == null || this.d_ndTF.getText().length() == 0) this.d_ndTF.setText("nd");
        this.thisActionMessageDialog.close();
    }


    private void setNothingAndLeave() {
        this.p_tTF.setText(this.holdpartial_or_total);
        this.d_ndTF.setText(this.holddisjunct_or_notdisjunct);
        this.doNothing = true;

        this.thisActionMessageDialog.close();
    }


    public TextField getP_tTF() {
        return this.p_tTF;
    }


    public TextField getD_ndTF() {
        return this.d_ndTF;
    }


    public boolean isDoNothing() {
        return this.doNothing;
    }
}