package id.co.gtc.erhacam;

import Config.SomeCodes;
import SecureDongle.SecureDongle;
import SecureDongle.SecureDongleEvent;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.tinylog.Logger;

import java.io.IOException;

import static Config.SomeCodes.ShowAlert;
import static Config.SomeCodes.config;


public class MainApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        SecureDongle sd = new SecureDongle((short)0x4B30, (short)0xA66C, (short)0x3109, (short)0x37B1);
        if (sd.Find()){
            if (sd.Open()){
                String UserID = Integer.toHexString(sd.ReadUserID()) ;
                sd.Close();

                if (UserID.equals("14022025")){
                    Logger.info("Secure Dongle UserID valid");
                    FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("main-view.fxml"));
                    Screen screen = Screen.getPrimary();
                    Rectangle2D screenbound = screen.getVisualBounds();
                    Scene scene = new Scene(fxmlLoader.load(), screenbound.getWidth(), screenbound.getHeight());
                    stage.setTitle("MultiCam Capture App for ERHA 08042025-003");
                    stage.setScene(scene);
                    stage.setResizable(true);
                    stage.setMaximized(true);
                    stage.setOnCloseRequest(e->{
                        sd.StopMonitor();
                        config.Save();
                        MainView mainView = fxmlLoader.getController();
                        mainView.Unload();
                        Logger.info("Application closed");
                    });
                    SomeCodes.LoadQRReader();
                    Detectors.LoadAllDetectors();

                    stage.show();

                    AutoCloseAlert.init();

                    Logger.info("Application started");
                    sd.setEvent(new SecureDongleEvent() {
                        @Override
                        public void onDongleMissing() {
                            Logger.error("Secure Dongle Missing");
                            Platform.runLater(()->{
                                ShowAlert(Alert.AlertType.ERROR, "Secure Dongle Missing", "Secure Dongle Missing", "Secure Dongle Missing");
                                Platform.exit();
                            });
                        }

                        @Override
                        public void onDongleError(String function, int errorCode) {

                        }
                    });
                    sd.StartMonitor();

                } else {
                    ShowAlert(Alert.AlertType.ERROR, "Secure Dongle UserID not valid", "Secure Dongle UserID not valid", "Secure Dongle UserID not valid");
                    Logger.error("Secure Dongle UserID not valid");
                    Platform.exit();
                }

            } else {
                ShowAlert(Alert.AlertType.ERROR, "Secure Dongle cannot be opened", "Secure Dongle cannot be opened", "Secure Dongle cannot be opened");
                Logger.error("Secure Dongle cannot be opened");
                Platform.exit();
            }
        } else {
            ShowAlert(Alert.AlertType.ERROR, "Secure Dongle not found", "Secure Dongle not found", "Secure Dongle not found");
            Logger.error("Secure Dongle not found");
            Platform.exit();
        }


    }

    public static void main(String[] args) {
        SomeCodes.ExtractResource("/tinylog.properties");
        launch();


    }
}