package id.co.gtc.erhacam;


import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import org.tinylog.Logger;

import static Config.SomeCodes.ValidString;

@SuppressWarnings("unused")
public class MainView {

    private String currentselected = "";

    @FXML
    private Pane mainpane;

    @FXML
    private Button ReviewButton;

    @FXML
    private Button CaptureButton;

    @FXML
    private Button SettingButton;

    @FXML
    private Button LogsButton;

    private Object currentcontroller;

    @FXML
    private void ReviewClick(ActionEvent event){
        if (currentselected.equals("review-view.fxml")) return;
        loadContent("review-view.fxml");
    }

    @FXML
    private void CaptureClick(ActionEvent event){
        if (currentselected.equals("capture-view.fxml")) return;
        loadContent("capture-view.fxml");
    }

    @FXML
    private void SettingClick(ActionEvent event){
        if (currentselected.equals("setting-view.fxml")) return;
        loadContent("setting-view.fxml");
    }

    @FXML
    private void LogsClick(ActionEvent event){
        if (currentselected.equals("logs-view.fxml")) return;
        loadContent("logs-view.fxml");
    }

    @FXML
    private void initialize(){
        CaptureClick(null);
    }

    public void Unload(){
        loadContent("");
    }

    private void loadContent(String fxmlfile){
        if (currentcontroller!=null){
            switch (currentcontroller) {
                case CaptureView captureView -> captureView.Unload();
                case SettingView settingView -> settingView.Unload();
                case ReviewView reviewView -> reviewView.Unload();
                case LogsView logsView -> logsView.Unload();
                default -> {
                }
            }
        }

        if (ValidString(fxmlfile)){
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlfile));
                AnchorPane child = loader.load();
                AnchorPane.setTopAnchor(child, 0.0);
                AnchorPane.setRightAnchor(child, 0.0);
                AnchorPane.setLeftAnchor(child, 0.0);
                AnchorPane.setBottomAnchor(child, 0.0);
                mainpane.getChildren().clear();
                mainpane.getChildren().add(child);
                currentselected = fxmlfile;
                currentcontroller = loader.getController();

            } catch (Exception e) {
                Logger.error("Unable to load " ,fxmlfile, ", exception : ", e.getMessage());
            }
        } else Logger.error("loadContent Not loading empty fxml file");
    }
}