/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client;

import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.support.ClassPathXmlApplicationContext;

import fr.tlse.irap.pt.client.controller.RootController;
import fr.tlse.irap.pt.client.controller.common.constant.TOOLTIP;
import fr.tlse.irap.pt.client.services.constant.CONFIG;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class EntryApp extends Application {

    private static final Logger log = LoggerFactory.getLogger(EntryApp.class);

    private final static String PARAMNAME_URL = "pt.server.url";
    private final static String PARAMNAME_NAME = "pt.project.name";
    private final static String PARAMNAME_VERSION = "pt.project.version";
    private final static String PARAMNAME_DATE = "pt.project.date";

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            setConfig();

            RootController root = new RootController();
            RootController.stage = primaryStage;
            RootController.rootLayout = root;

            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("controller/RootComponent.fxml"));
            fxmlLoader.setRoot(root);
            fxmlLoader.setController(root);

            fxmlLoader.load();

            Scene scene = new Scene(root);
            primaryStage.centerOnScreen();
            primaryStage.setScene(scene);
            primaryStage.show();
            primaryStage.setResizable(false);
            
            
            scene.getStylesheets().add(getClass().getResource("styles/Calendar.css").toString());
            scene.getStylesheets().add(getClass().getResource("styles/Chart.css").toString());
            scene.getStylesheets().add(getClass().getResource("styles/StyleBtn.css").toString());

            // set icon
            //primaryStage.getIcons().add(new Image("images/logopt.png"));
            primaryStage.getIcons().add(new Image(getClass().getResource("images/favicon.png").toString()));

            // set title
            primaryStage.setTitle("Propagation Tool");

            // TODO : mise en place d'un controler de threads
            // ExecutorService executorService = Executors.newFixedThreadPool(10);
            // RootController.executorService = executorService;
            // end application
            primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {

                @Override
                public void handle(WindowEvent arg0) {
                    System.out.println("Application is ended");
                    // TODO : mise en place d'un controler de threads
//				try {
//					RootController.executorService.shutdown();
//					// wait for 10s in this case
//					RootController.executorService.awaitTermination(10, TimeUnit.SECONDS);
//				} catch (InterruptedException e) {
//				} 
//				RootController.executorService.shutdownNow();
                    Platform.exit();
                    System.exit(0);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stop() throws Exception {
        System.out.println("I stop");
        System.exit(0);
    }

    private void setConfig() {
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//        CONFIG prop = (CONFIG) context.getBean("conf");

//    <fx:argument>https://storms-tools.irap.omp.eu</fx:argument>
//    <fx:argument>PropagationTool</fx:argument>
//    <fx:argument>2.12</fx:argument>
//    <fx:argument>29/04/2025</fx:argument>
        CONFIG prop = new CONFIG();
        if (prop != null) {

            
            
            prop.setSERVER_URL(System.getProperty(PARAMNAME_URL) + "/" + System.getProperty(PARAMNAME_NAME));
            prop.setPROJECT_VERSION(System.getProperty(PARAMNAME_VERSION));
            prop.setRELEASE_DATE(System.getProperty(PARAMNAME_DATE));

            if (prop.getPROJECT_VERSION()==null) {
                // default values
                if (prop.getSERVER_URL() == null || prop.getSERVER_URL().length() == 0) {
                    prop.setSERVER_URL("https://storms-tools.irap.omp.eu/PropagationTool");
                }
                if (prop.getPROJECT_VERSION() == null || prop.getPROJECT_VERSION().length() == 0) {
                    prop.setPROJECT_VERSION("2.25");
                }
                if (prop.getRELEASE_DATE() == null || prop.getRELEASE_DATE().length() == 0) {
                    prop.setRELEASE_DATE("14/08/2025");
                }
            }
        }

        // set default timezone UTC
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        // Tooltip 
        if (System.getProperty("os.name").startsWith("Mac OS")) {
            TOOLTIP.setDisable(true);
        }

        System.out.println("Properties=" + System.getProperties());
        System.out.println("server_base_url=" + prop.getSERVER_URL());
        System.out.println("project_version=" + prop.getPROJECT_VERSION());
    }

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