/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Creation 
 * Version : 24/03/2016 : SCA : V2.2 (PSWS-H2020)
 * Disable slider when no active tool 
 * Version : 12/09/2023 : LBE : V2.8
 * DM-ID : Redmine#10507 : 12/09/2023 : add vertical slider for small screen
 * Version : 15/05/2024 : LBE : V2.9
 * DM-ID : Redmine#10806 : 15/05/2024 : init scene with current time at start
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller;

import com.sun.glass.ui.Screen;
import fr.tlse.irap.pt.client.controller.eclipticPlane.EclipticPlaneController;
import fr.tlse.irap.pt.client.controller.menu.MenuController;
import fr.tlse.irap.pt.client.controller.timestamp.TimestampController;
import fr.tlse.irap.pt.client.controller.tools.CorotationController;
import fr.tlse.irap.pt.client.controller.tools.IToolController;
import fr.tlse.irap.pt.client.controller.tools.IToolController.JMapTool;
import fr.tlse.irap.pt.client.controller.tools.IToolController.Tool;
import fr.tlse.irap.pt.client.controller.tools.RadialController;
import fr.tlse.irap.pt.client.controller.tools.SepController;
import fr.tlse.irap.pt.client.controller.tools.WelcomeComponent;
import java.util.Date;
import javafx.fxml.FXML;
import javafx.scene.Cursor;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class RootController extends AnchorPane {

	@FXML protected TimestampController timestampComponent;
	@FXML protected EclipticPlaneController eclipticPlaneComponent;
	@FXML protected MenuController menuComponent;
	@FXML protected WelcomeComponent welcomeComponent;
	@FXML protected RadialController radialComponent;
	@FXML protected CorotationController corotationComponent;
	@FXML protected SepController sepComponent;
        @FXML protected ScrollPane scrollPane;
        
	public static Stage stage;
	public static RootController rootLayout;
	//public static ExecutorService executorService;
	private IToolController currentTool;

	public void initialize() {
		menuComponent.initialize(this);
		menuComponent.setDisable(true);
		radialComponent.setVisible(false);
		corotationComponent.setVisible(false);
		sepComponent.setVisible(false);
		eclipticPlaneComponent.initComponentWith(timestampComponent, menuComponent);
		showWelcome();
                timestampComponent.setSelectedDate(new Date());
//                System.out.println("**** update with "+timestampComponent.getSelectedDate());
		//timestampComponent.setStartTime(timestampComponent.getSelectedDate());
                timestampComponent.setResultTime(new Date());//load at init: 10806
                timestampComponent.updateResultDate();
                
                if(rootLayout.getPrefHeight()>com.sun.glass.ui.Screen.getMainScreen().getVisibleHeight()) {
                    int lNewSize = com.sun.glass.ui.Screen.getMainScreen().getVisibleHeight()-50;
                    rootLayout.setPrefHeight(lNewSize);
                    scrollPane.setPrefHeight(lNewSize);
                }
	}

	public void showRadial() {
		eclipticPlaneComponent.initDrawingElements(Tool.RADIAL);
		if (radialComponent.getJMapTool().get() == null) {
			radialComponent.setJMapTool(JMapTool.INSITU);
		}
		showTool(radialComponent);
	}

	public void showCorotation() {
		eclipticPlaneComponent.initDrawingElements(Tool.COROTATION);
		if (corotationComponent.getJMapTool().get() == null) {
			corotationComponent.setJMapTool(JMapTool.INSITU);
		}
		showTool(corotationComponent);
	}

	public void showSEP() {
		eclipticPlaneComponent.initDrawingElements(Tool.SEP);
		showTool(sepComponent);
	}

	public void setJMapTool(JMapTool jMapTool) {
		if (currentTool != null) {
			currentTool.setJMapTool(jMapTool);
		}
	}

	public JMapTool getJMapTool() {
		if (currentTool != null) {
			return currentTool.getJMapTool().get();
		}
		return null;
	}

	private void showTool(IToolController tool) {
		if (timestampComponent.getStartTime() != null) {
			if (currentTool != null) {
				currentTool.setVisible(false);
			} else {
				//welcomeComponent.setVisible(false);
				welcomeComponent.show(false);
			}
			currentTool = tool;
			menuComponent.setJMapMenu(currentTool.getJMapTool().get());
			currentTool.setVisible(true);
			currentTool.initComponentWith(eclipticPlaneComponent, timestampComponent);
		}
	}

	public void showWelcome() {
		if (currentTool != null) {
			currentTool.setVisible(false);
		}
		currentTool = null;
		//welcomeComponent.setVisible(true);
		welcomeComponent.show(true);
		eclipticPlaneComponent.initDrawingElements(null);
		// disable slider
		timestampComponent.getResultTScrollBar().setDisable(true);
	}

	public static void disableComponents() {
		rootLayout.setDisable(true);
	}

	public static void enableComponents() {
		rootLayout.setDisable(false);
	}

	public static void waitCursor() {
		disableComponents();
		stage.getScene().setCursor(Cursor.WAIT);
	}

	public static void endCursor() {
		enableComponents();
		stage.getScene().setCursor(null);
	}

}
