package at.fos.ermodel.gui;

import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;

import java.io.File;
import java.util.Optional;


public class U1
        extends Tab {
    public final A3 canvas;
    public final StackPane stackpane;
    public final ScrollPane scrollpane;

    public U1(B3 view, String name, File modelInFile) {
        this.stackpane = new StackPane();
        this.stackpane.setBackground(C2.AppBackground);
        this.canvas = new A3(view, modelInFile, this);

        this.stackpane.getChildren().add(this.canvas);

        this.scrollpane = new ScrollPane(this.stackpane);
        this.stackpane.setFocusTraversable(true);
        this.scrollpane.setFocusTraversable(true);

        this.scrollpane.setVmin(0.0D);
        this.scrollpane.setVmax(this.canvas.getCanvasHeight());
        this.scrollpane.setHmin(0.0D);
        this.scrollpane.setHmax(this.canvas.getCanvasWidth());

        this.stackpane.addEventFilter(KeyEvent.ANY, e -> this.scrollpane.requestFocus());
        this.scrollpane.addEventFilter(KeyEvent.ANY, e -> this.scrollpane.requestFocus());

        setOnCloseRequest(e -> {
            if (getTabPane().getTabs().size() <= 1) {
                e.consume();
                return;
            }
            C1 alert = new C1(null, Alert.AlertType.CONFIRMATION, "Close Tab", "", "Do you really want to close", Boolean.TRUE, C2.applicationImageIconAsICO, "OK", "Cancel", C2.ButtonBackgroundColor, C2.MouseSelectedColor, C2.FocusOnComponentColor);
            Optional<ButtonType> result = alert.getResult();
            if (result.isPresent() && result.get() == ButtonType.OK) {
                view.selectModeBTN.setSelected(true);
            } else {
                e.consume();
            }
        });


        this.scrollpane.setOnKeyPressed(U1.this.canvas::sdfdsfd567657rrt);
        this.stackpane.setOnKeyPressed(U1.this.canvas::sdfdsfd567657rrt);

        this.canvas.setOnKeyPressed(U1.this.canvas::sdfdsfd567657rrt);

        setContent(this.scrollpane);
        if (modelInFile == null) {
            setText(name);
        } else {

            setText(modelInFile.getName());
        }
        this.canvas.requestFocus();
    }
}