package at.fos.ermodel.gui;

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

public class A4
        extends Stage {
    private final A4 thisActionMessageDialog;
    private final TextField minTF;
    private final TextField maxTF;
    private final String holdminimum;
    private final String holdmaximum;
    private boolean doNothing;

    public A4(final A3 canvas, double xPos, double yPos, String minimum, String maximum) {

        String title = "Enter Min-Max";

        this.holdminimum = minimum;
        this.holdmaximum = maximum;

        this.doNothing = false;

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


        this.minTF = new TextField(minimum);
        this.minTF.textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.length() > 1 && newValue.charAt(0) == '0') {
                newValue = newValue.replaceAll("^0*", "");
                if (newValue.length() == 0)
                    newValue = "0";
                this.minTF.setText(newValue);
            }
            if (!newValue.matches("\\d*"))
                this.minTF.setText(newValue.replaceAll("[^\\d]", ""));
        });
        this.minTF.setPrefWidth(50.0D);
        this.minTF.setOnMouseEntered(event -> T3.fdssdf4354(A4.this.minTF, C2.MouseSelectedColor));
        this.minTF.setOnMouseExited(event -> A4.this.minTF.setEffect(null));

        this.minTF.setOnKeyPressed(event -> {
            switch (event.getCode()) {
                case ENTER -> A4.this.maxTF.requestFocus();
                case ESCAPE -> A4.this.setNothingAndLeave();
            }

        });
        this.maxTF = new TextField(maximum);
        this.maxTF.setPrefWidth(50.0D);
        this.maxTF.textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.length() == 1 && newValue.charAt(0) == '0') {
                newValue = "n";
                this.maxTF.setText(newValue);
                return;
            }
            if (newValue.length() > 1 && !newValue.matches("\\d*")) {
                newValue = "n";
                this.maxTF.setText(newValue);
                return;
            }
            if (newValue.length() == 1 && newValue.charAt(0) == 'n') {
                newValue = "n";
                this.maxTF.setText(newValue);
                return;
            }
            if (newValue.length() > 1 && newValue.charAt(0) == '0') {
                newValue = newValue.replaceAll("^0*", "");
                if (newValue.length() == 0)
                    newValue = "0";
                this.maxTF.setText(newValue);
                return;
            }
            if (!newValue.matches("\\d*"))
                this.maxTF.setText(newValue.replaceAll("[^\\d]", ""));
        });
        this.maxTF.setOnMouseEntered(event -> T3.fdssdf4354(A4.this.maxTF, C2.MouseSelectedColor));
        this.maxTF.setOnMouseExited(event -> A4.this.maxTF.setEffect(null));
        this.maxTF.setOnKeyPressed(event -> {
            switch (event.getCode()) {
                case ENTER -> {
                    A4.this.setDataAndLeave();
                    if (!canvas.getTabWithCanvas().getText().contains("*"))
                        canvas.getTabWithCanvas().setText(canvas.getTabWithCanvas().getText() + "*");
                }
                case ESCAPE -> A4.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, 2, 1);
        gPane.add(new Label("Minimum:"), 0, 1);
        gPane.add(this.minTF, 1, 1);
        gPane.add(new Label("Maximum:"), 0, 2);
        gPane.add(this.maxTF, 1, 2);


        initStyle(StageStyle.UNDECORATED);
        Scene scene = new Scene(gPane, 130.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.minTF.requestFocus();
        this.minTF.selectPositionCaret(0);
        this.minTF.selectEndOfNextWord();
        sizeToScene();
        showAndWait();
    }


    private void setDataAndLeave() {
        if (this.minTF.getText() == null || this.minTF.getText().length() == 0) this.minTF.setText("m");
        if (this.maxTF.getText() == null || this.maxTF.getText().length() == 0) this.maxTF.setText("n");
        this.thisActionMessageDialog.close();
    }


    private void setNothingAndLeave() {
        this.minTF.setText(this.holdminimum);
        this.maxTF.setText(this.holdmaximum);
        this.doNothing = true;

        this.thisActionMessageDialog.close();
    }


    public TextField getMinTF() {
        return this.minTF;
    }


    public TextField getMaxTF() {
        return this.maxTF;
    }


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


