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 A5
        extends Stage {
    private final A5 thisActionMessageDialog;
    private final TextField nameTF;
    private final String holdName;
    private boolean doNothing;

    public A5(final A3 canvas, double xPos, double yPos, String name) {

        String title = "Enter Name";


        this.holdName = name;
        this.doNothing = false;

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


        this.nameTF = new TextField(name);
        this.nameTF.setOnMouseEntered(event -> T3.fdssdf4354(A5.this.nameTF, C2.MouseSelectedColor));
        this.nameTF.setOnMouseExited(event -> A5.this.nameTF.setEffect(null));
        this.nameTF.setOnKeyPressed(event -> {
            switch (event.getCode()) {
                case ENTER -> {
                    A5.this.setDataAndLeave();
                    if (!canvas.getTabWithCanvas().getText().contains("*"))
                        canvas.getTabWithCanvas().setText(canvas.getTabWithCanvas().getText() + "*");
                }
                case ESCAPE -> A5.this.setNothingAndLeave();
            }

        });
        GridPane fPane = new GridPane();
        fPane.setPadding(new Insets(5.0D, 5.0D, 5.0D, 5.0D));

        fPane.setVgap(2.0D);
        fPane.setHgap(2.0D);

        fPane.add(new Label("ESC->Cancel"), 0, 0, 2, 1);
        fPane.add(new Label("Name:"), 0, 1);
        fPane.add(this.nameTF, 1, 1);


        Scene scene = new Scene(fPane, 220.0D, 75.0D);

        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.nameTF.requestFocus();
        this.nameTF.selectPositionCaret(0);
        this.nameTF.selectEndOfNextWord();
        initStyle(StageStyle.UNDECORATED);

        setScene(scene);
        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.doNothing = true;
        this.thisActionMessageDialog.close();
    }


    public TextField getNameTF() {
        return this.nameTF;
    }


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


