package fabrik.simulator.pic16f84;

import fabrik.simulator.pic16f84.frontendspecifics.VBox;
import fabrik.simulator.pic16f84.interfaces.*;
import javafx.application.Application;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;

import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Objects;

public class CreateWindow extends Application  implements WindowManagement {
    private static PICComponentLocator picComponents; // Subjekt
    public static GridPane grid = new GridPane();
    private TableInterface table;
    private static VBox tableBox;

    @Override
    public void start(Stage primaryStage) throws IOException {
        picComponents.registerComponent(ToggleButtonInterface.class, new ToggleButtonGroupExt());
        picComponents.registerComponent(IOPortInterface.class, new IOPorts());
        picComponents.registerComponent(TableInterface.class, new Table());
        picComponents.registerComponent(FrontendControllerInterface.class, new Controller_Frontend());
        picComponents.initAll();

        table = picComponents.getComponent(TableInterface.class);

        FXMLLoader codewindow = new FXMLLoader(CreateWindow.class.getResource("frontend.fxml"));
        codewindow.setController(picComponents.getComponent(FrontendControllerInterface.class));
        Parent code = codewindow.load();

        tableBox = (VBox) table.getTable();
        grid.add(tableBox, 1, 1);
        grid.add(code, 0, 1);

        grid.relocate(0, 0);

        grid.getStylesheets().add(Objects.requireNonNull(getClass().getResource("styles.css")).toExternalForm());
        Scene scene = new Scene(grid, 1845, 800);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Simulator");
        primaryStage.show();
    }

    public void startFromMain(String[] args){
        launch(args);
    }

    public static void main(String[] args) {
        launch(args);
    }

    public static void refreshTable() {
        TableInterface table = picComponents.getComponent(TableInterface.class);
        grid.getChildren().remove(table.getTable());
        table.refresh();
        tableBox = (VBox) table.getTable();
        grid.add(tableBox, 1, 1);
    }

    @Override
    public void initialize(PICComponentLocator picComponents) {
        CreateWindow.picComponents = picComponents;
    }

    public CreateWindow (){
    }
}


