/* ----------------------------------------
 * 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)
 *  Add comets
 *  Swap STEREO-A / STEREO-B FOV after 14/07/2015
 * Version : 12/09/2023 : LBE : V2.8
 * DM-ID : Redmine#10500 : 12/09/2023 : new END points
 * Version : 05/08/2024 : LBE : V2.11
 * DM-ID : Redmine#l0763 : 05/08/2024 : Field lines added
 * VERSION : V2.12
 * DM-ID : redmine#11977 : 05/03/2025 : KHK : bug fix :protect getId() is null
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.drawingPlane;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import fr.tlse.irap.pt.client.component.CustomComponent;
import fr.tlse.irap.pt.client.component.ICustomComponent;
import fr.tlse.irap.pt.client.controller.common.Item;
import fr.tlse.irap.pt.client.controller.common.MapMenu;
import fr.tlse.irap.pt.client.controller.common.ShockCollection;
import fr.tlse.irap.pt.client.controller.common.SpaceElementCollection;
import fr.tlse.irap.pt.client.controller.common.constant.TOOLTIP;
import fr.tlse.irap.pt.client.controller.drawingPlane.DrawHelper.ViewType;
import fr.tlse.irap.pt.client.controller.eclipticPlane.EclipticPlaneItemBuilder;
import fr.tlse.irap.pt.client.controller.eclipticPlane.IEclipticPlane;
import fr.tlse.irap.pt.client.controller.tools.jmap.JMapModel;
import fr.tlse.irap.shock.Shock;
import fr.tlse.irap.spaceElement.Camera;
import fr.tlse.irap.spaceElement.Comet;
import fr.tlse.irap.spaceElement.Planet;
import fr.tlse.irap.spaceElement.Probe;
import fr.tlse.irap.spaceElement.SpaceElement;
import fr.tlse.irap.spaceElement.SpaceElementWrapper;
import fr.tlse.irap.tool.radial.CMEType;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class DrawingPlaneController extends Group implements Initializable, ICustomComponent {

    static final String RECT_BORDER_ID = "rectangleBorder";
    static final int FIRST_NODE = 1;
    protected double rectWidthInitial;
    protected double rectHeightInitial;

    @FXML
    protected Rectangle rectangleBorder;
    @FXML
    protected DrawingPlaneController rootDrawingPlane;
//    @FXML protected ImageView imageView;

    ScrollPane sPaneParent;
    static JMapModel jMapModel = new JMapModel();
    static final MapMenu mapMenu = new MapMenu();
    List<Node> drawingNodes = new ArrayList<Node>();

    CMEType cmeType = CMEType.FIXED_POINT;
    CmeFixPoint cmeFixPoint;
    CmeSSE cmeSSE;
    ViewType viewType;

    public IDrawingCme cme;
    public DrawingSpiral spiral;
    public DrawingFOV jMapFOV;
    public Shockwave shock;
    private DrawingFieldLines fieldlines;

    boolean isVisibleAllPlanets = true;
    boolean isVisibleAllProbes = true;
    // by default comets are not visible
    boolean isVisibleAllComets = false;
    boolean isVisibleAllShocks = false;

    Map<String, Boolean> isVisibleAllFOVs = new HashMap<String, Boolean>();

    boolean isDrawingCME;
    boolean isDrawingSpiral;
    boolean isDrawingJMapFOV;
    boolean isShowingMap;
    String typeMap;
    EventHandler<MouseEvent> filter;

    public DrawingPlaneController() {
        super();
        CustomComponent.initCustomComponent(this);
    }

    /**
     * Initialize component
     *
     * @param viewType
     */
    public void initComponent(Control parent, ViewType viewType) {

        sPaneParent = (ScrollPane) parent;
        this.rectWidthInitial = rectangleBorder.getWidth();
        this.rectHeightInitial = rectangleBorder.getHeight();

        // create tooltip
        if (!TOOLTIP.isDisable()) {
            Tooltip.install(rectangleBorder, new Tooltip(TOOLTIP.ECLIPTIC_RIGHT_CLICK));
        }

        this.viewType = viewType;
        filter = new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                event.consume();
            }
        };
        // init menu
        rootDrawingPlane.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                event.consume();
                if (event.getButton() == MouseButton.SECONDARY) {
                    mapMenu.show(rootDrawingPlane, event.getScreenX(), event.getScreenY());
                    event.consume();
                }
                if (event.getButton() == MouseButton.PRIMARY) {
                    mapMenu.hide();
                }
            }
        });

        if (viewType == ViewType.XY) {
            // initialize specific objects 
            cmeFixPoint = new CmeFixPoint();
            cmeSSE = new CmeSSE();
            cme = cmeFixPoint;
            spiral = new DrawingSpiral();
            jMapFOV = new DrawingFOV();
            fieldlines = new DrawingFieldLines();
            jMapModel.reset();
        }

//		sPaneParent.heightProperty().addListener(new ChangeListener<Number>() {
//			@Override
//			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
//			}
//		});
//		sPaneParent.vvalueProperty().addListener(new ChangeListener<Number>() {
//			@Override
//			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
//			}
//		});
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    /**
     * load context menu
     */
    public void loadMenu(IEclipticPlane iEclipticPlane) {
        if (SpaceElementCollection.getSpaceElementWrapper() == null) {
            return;
        }

        List<Item> menus = new ArrayList<Item>();
        menus.add(EclipticPlaneItemBuilder.getShowPlanetsItem(mapMenu.getMap(), iEclipticPlane));
        menus.add(EclipticPlaneItemBuilder.getShowProbesItem(mapMenu.getMap(), iEclipticPlane));
        menus.add(EclipticPlaneItemBuilder.getShowCometsItem(mapMenu.getMap(), iEclipticPlane));
        if (isDrawingJMapFOV) {
            menus.add(EclipticPlaneItemBuilder.getShowStartEndItem(mapMenu.getMap(), iEclipticPlane));
        }
        //menus.add(EclipticPlaneItemBuilder.getShowMapEnlilItem(mapMenu.getMap(), iEclipticPlane));
        if (isDrawingCME) {
            menus.add(EclipticPlaneItemBuilder.getShowCMEItem(mapMenu.getMap(), iEclipticPlane));
        }
        if (isDrawingSpiral) {
            menus.add(EclipticPlaneItemBuilder.getShowSpiralItem(mapMenu.getMap(), iEclipticPlane));
        }
        menus.add(EclipticPlaneItemBuilder.getShowFovsItem(mapMenu.getMap(), iEclipticPlane, isDrawingJMapFOV));
        menus.add(EclipticPlaneItemBuilder.getShowShockwaves(mapMenu.getMap(), iEclipticPlane));
        menus.add(EclipticPlaneItemBuilder.getSearchCometsItem(mapMenu.getMap(), iEclipticPlane));
        menus.add(EclipticPlaneItemBuilder.getResetObjectsItem(mapMenu.getMap(), iEclipticPlane));

        mapMenu.getItems().clear();
        mapMenu.addAll(menus);
    }

    /**
     * Add space element nodes to the ecliptic plane. Init visibility of nodes
     */
    public void addSpaceElementNodes() {
        SpaceElementWrapper elements = SpaceElementCollection.getSpaceElementWrapper();
        if (elements == null) {
            return;
        }
        // add sun
        final Node elementNode = DrawHelper.createNodeFrom(elements.getSun(), viewType);
        rootDrawingPlane.getChildren().add(elementNode);

        for (Planet planet : elements.getPlanets()) {
            if (viewType.equals(ViewType.XY) || isInner(planet.getId())) {
                // add planet orbit
                rootDrawingPlane.getChildren().add(DrawHelper.createEllipse(planet, viewType));
            }
        }
        for (Comet comet : elements.getComets()) {
            // add comet orbit
            rootDrawingPlane.getChildren().add(DrawHelper.createEllipse(comet, viewType, Color.DARKORANGE));
        }
        for (Planet planet : elements.getPlanets()) {
            if (viewType.equals(ViewType.XY) || isInner(planet.getId())) {
                rootDrawingPlane.getChildren().add(DrawHelper.createNodeFrom(planet, viewType));
            }
        }
        for (Comet comet : elements.getComets()) {
            rootDrawingPlane.getChildren().add(DrawHelper.createNodeFrom(comet, viewType));
        }
        for (Probe probe : elements.getProbes()) {
            if (probe.isDefined()) {
                rootDrawingPlane.getChildren().add(DrawHelper.createNodeFrom(probe, viewType));
            }
        }
        // init view
        showAllPlanets(isVisibleAllPlanets);
        showAllProbes(isVisibleAllProbes);
        showAllComets(isVisibleAllComets);
    }

    /**
     * Add specific nodes (FOVs, Spiral, CME, Field lines) to the ecliptic
     * plane.
     */
    public void addSpecificNodes() {
        if (SpaceElementCollection.getSpaceElementWrapper() == null) {
            return;
        }
        for (Probe probe : SpaceElementCollection.getSpaceElementWrapper().getProbes()) {
            if (probe.isDefined() && probe.isObserver()) {
                for (Camera camera : SpaceElementCollection.getCameras(probe.getName())) {
                    addFOVToEclipticPlane(probe, camera);
                }
                //isVisibleAllFOVs.put(probe.getName(), false);
            }
        }
        addShocksToEclipticPlan();
        addFOVToEclipticPlan();
        addCMEToEclipticPlan();
        addSpiralToEclipticPlan();
        showAllShocks(isVisibleAllShocks);
        addFLToEclipticPlane();
    }

    private void addCMEToEclipticPlan() {
        if (cme != null) {
            updateCme();
            int iNode = FIRST_NODE;
            for (Node node : cmeFixPoint.getNodes()) {
                rootDrawingPlane.getChildren().add(iNode++, node);
            }
            for (Node node : cmeSSE.getNodes()) {
                rootDrawingPlane.getChildren().add(iNode++, node);
            }
        }
    }

    private void addSpiralToEclipticPlan() {
        if (spiral != null) {
            drawSpiral();
            rootDrawingPlane.getChildren().add(FIRST_NODE, spiral.getSpiral());
        }
    }

    private void addShocksToEclipticPlan() {
        Shock shock = ShockCollection.getShockNearDate(SpaceElementCollection.getCurrentDate());
        if (shock != null) {
            // add drawing shock
            Shockwave sw = new Shockwave(shock);
            //rootDrawingPlane.getChildren().add(FIRST_NODE, sw.getEllipse());
            rootDrawingPlane.getChildren().add(sw.getEllipse());
        }
    }

    private void addFOVToEclipticPlan() {
        if (jMapFOV != null) {
            jMapFOV.setDisable(true);
            jMapFOV.hide();
            if (isDrawingJMapFOV) {
                updateJMapFOV();
            }
            rootDrawingPlane.getChildren().add(FIRST_NODE, jMapFOV.getArc());
        }
    }

    private void addFOVToEclipticPlane(Probe probe, Camera camera) {
        boolean isBackward = probe.isBackward(SpaceElementCollection.getCurrentDate());
        DrawingFOV fOV = new DrawingFOV(probe, camera, isBackward);
        fOV.setDisable(true);
        fOV.hide();
        rootDrawingPlane.getChildren().add(FIRST_NODE, fOV.getArc());
        if (camera.isSymmetric()) {
            fOV = new DrawingFOV(probe, camera, !isBackward);
            fOV.setDisable(true);
            fOV.hide();
            rootDrawingPlane.getChildren().add(FIRST_NODE, fOV.getArc());
        }
    }

    private void addFLToEclipticPlane() {
        if (fieldlines != null) {
            //*************************************
            //TODO: get it from carrington panel
            //*************************************
            rootDrawingPlane.getChildren().add(fieldlines.getNodes());

        }
    }

    /**
     * Save current objects drawing state
     */
    public void saveConfig() {
        drawingNodes.clear();
        for (Node node : rootDrawingPlane.getChildren()) {
            if (node.getId() != null) {
                if (node.getId().contains(Probe.class.getSimpleName())
                        || node.getId().contains(Planet.class.getSimpleName())
                        || node.getId().contains(Comet.class.getSimpleName())
                        || node.getId().contains(DrawingFOV.className)) {
                    drawingNodes.add(node);
                }
            }
        }
    }

    /**
     * Restore last saved objects drawing state
     */
    public void restoreConfig() {
        for (Node saveNode : drawingNodes) {
            Node node = findByName(saveNode.getId());
            if (node != null) {
                node.setVisible(saveNode.isVisible());
                if (isDrawingJMapFOV) {
                    if (node.getId().contains(Probe.class.getSimpleName())) {
                        if (node.getId().contains(jMapModel.getObserver())) {
                            showJMapFOV(node.isVisible());
                        }
                    }
                }
            }
        }
    }

    /**
     * Zoom ecliptic plane scene with all objects
     */
    public void zoom(Double zoomFactor) {
        //zoom control done in EclipticPlaneController
//		if (zoomFactor < 1.0 && DrawHelper.getScale() < MIN_ZOOM) return;
//		if (zoomFactor > 1.0 && DrawHelper.getScale() > 100.0) return;

        rootDrawingPlane.setVisible(false);
        for (Node node : rootDrawingPlane.getChildren()) {
            if (RECT_BORDER_ID.equals(node.getId())) {
                resizeRectangle(zoomFactor);
            } //			else if (node.getId().equals(IMAGE_VIEW_ID)) {
            //				resizeImageView(zoomFactor);
            //			}
            else {
                DrawHelper.zoomNode(node, zoomFactor);
            }
        }
        rootDrawingPlane.setVisible(true);
    }

    /**
     *
     */
    public void resizeRectangle(Double scale) {
        rectangleBorder.setWidth(rectangleBorder.getWidth() * scale);
        rectangleBorder.setHeight(rectangleBorder.getHeight() * scale);
        rectangleBorder.setX(-rectangleBorder.getWidth() / 2);
        rectangleBorder.setY(-rectangleBorder.getHeight() / 2);
    }

//	public void resizeImageView(Double scale) {
//		imageView.setFitWidth(imageView.getFitWidth()*scale);
//		imageView.setFitHeight(imageView.getFitHeight()*scale);
//		imageView.setX(-imageView.getFitWidth() / 2 - 24);
//		imageView.setY(-imageView.getFitHeight() / 2 - 72);
//	}
    /**
     *
     */
    public void initRectangleBorder() {
        rectangleBorder.setWidth(rectWidthInitial);
        rectangleBorder.setHeight(rectHeightInitial);
        rectangleBorder.setX(-rectangleBorder.getWidth() / 2);
        rectangleBorder.setY(-rectangleBorder.getHeight() / 2);
        rectangleBorder.setId(RECT_BORDER_ID);
    }

//	public void initImageView() {
//		imageView.setFitWidth(rectangleBorder.getWidth());
//		imageView.setFitHeight(rectangleBorder.getHeight());
//		imageView.setX(-imageView.getFitWidth() / 2 - 24);
//		imageView.setY(-imageView.getFitHeight() / 2 - 72);
//		imageView.setId(IMAGE_VIEW_ID);
//		// TODO - choose a solution to display CME correctly
//		// sol 1 : add transparency
//		imageView.setOpacity(0.4);
//		// sol 2 : color combination : work only with black and white images  
//		//imageView.setBlendMode(BlendMode.MULTIPLY);
//	}
    /**
     * reset default view
     */
    public void resetView() {
        rootDrawingPlane.getChildren().clear();
        rootDrawingPlane.getChildren().add(rectangleBorder);
//		rootDrawingPlane.getChildren().add(imageView);
        initRectangleBorder();
//		initImageView();
        resizeRectangle(200.0);
    }

    /**
     * ************************************************************************
     ****************************** CME actions *******************************
	 *************************************************************************
     */
    /**
     * Update CME with
     *
     * @param longSource : HAE longitude of CME
     * @param extent
     */
    public void updateCmeWith(CMEType cmeType, double longSource, double extent) {
        this.cmeType = cmeType;
        setDrawingCME(true);
        if (cmeType.equals(CMEType.SSE)) {
            cme = cmeSSE;
            cmeSSE.updateViewWith(longSource, 0.0, extent);
            cmeFixPoint.hide();
        } else {
            cme = cmeFixPoint;
            cmeFixPoint.updateViewWith(longSource, extent);
            cmeSSE.hide();
        }
        cme.show();
    }

    public void updateRadiusCme(double radialDistKm, double deltaLong) {
        if (cmeType.equals(CMEType.SSE)) {
            cmeSSE.updateViewWith(radialDistKm, deltaLong);
        } else {
            cmeFixPoint.updateViewWith(radialDistKm);
        }
        cme.show();
    }

    /**
     * Update CME
     */
    public void updateCme() {
        cme.update();
        cme.show();
    }

    /**
     * ************************************************************************
     * ***************************** spiral actions ***************************
	 *************************************************************************
     */
    public DrawingSpiral getSpiral() {
        return spiral;
    }

    /**
     * Draw spiral with
     *
     * @param fptLong : HAE longitude of the spiral footpoint
     * @param rotPeriod
     * @param speed
     * @param spread
     */
    public void drawSpiralWith(double fptLong, double rotPeriod, double speed, double spread) {
        spiral.setFootPointLong(fptLong);
        spiral.setSpread(spread);
        spiral.setRotationPeriod(rotPeriod);
        spiral.setSpeed(speed);
        drawSpiral();
    }

    /**
     * Draw spiral with
     *
     * @param fptLong : footpoint longitude in HAE
     */
    public void drawSpiralWith(double fptLong) {
        spiral.setFootPointLong(fptLong);
        drawSpiral();
    }

    public void drawSpiral() {
        spiral.drawWith(rectangleBorder.getWidth() / 8);
        spiral.show();
    }

    public DrawingFOV getJMapFOV() {
        return jMapFOV;
    }

    public void showJMapFOV(boolean pVisibility) {
        if (isDrawingJMapFOV) {
            if (pVisibility) {
                jMapFOV.show();
            } else {
                jMapFOV.hide();
            }
        }
    }

    public void updateFOV(JMapModel jMapModel) {
        if (isDrawingJMapFOV) {
            DrawingPlaneController.jMapModel = jMapModel;
            updateJMapFOV();
        }
    }

    /**
     * Update Jmap FOV
     */
    public void updateJMapFOV() {
        jMapFOV.setDisable(true);
        jMapFOV.hide();

        Probe probe = SpaceElementCollection.getSpaceElementWrapper().getProbe(jMapModel.getObserver());
        if (probe.isDefined()) {
            jMapFOV.updateSource(probe);
            Node sourceNode = findByName(jMapModel.getObserver() + Probe.class.getSimpleName());
            jMapFOV.setOrientation(
                    jMapModel.getMinElong(),
                    jMapModel.getMaxElong() - jMapModel.getMinElong(),
                    probe.isBackward(SpaceElementCollection.getCurrentDate()));
            jMapFOV.setDisable(false);
            // apply settings 
            jMapFOV.update();
            // show/hide FOV
            showJMapFOV(sourceNode.isVisible());
//			}
        }
        //jMapFOV.show();
    }

    /**
     * Show FOV and cameras
     *
     * @param probeName
     * @param camera
     */
    public void showFOV(String obs, String camera) {
        Node node = findByName(obs + Probe.class.getSimpleName());
        if (node != null && node.isVisible()) {
            for (Node nFov : rootDrawingPlane.getChildren()) {
                if (nFov.getId().contains(DrawingFOV.className + obs + camera)) {
                    nFov.setVisible(!nFov.isVisible());
                }
            }
        }
    }

    /**
     * Show / hide an space element with its orbit
     *
     * @param element
     */
    public void showElement(SpaceElement element) {
        if (element != null) {
            Node node = findByName(element.getName() + element.getClass().getSimpleName());
            if (node != null) {
                node.setVisible(!node.isVisible());
            }
            if ((element instanceof Probe)) {
                if (element.getName().equals(jMapModel.getObserver())) {
                    showJMapFOV(node.isVisible());
                }
                if (isVisibleAllFOVs(element.getName())) {
                    // show/hide ALL FOVs
                    for (Node n : rootDrawingPlane.getChildren()) { 
                        // 11977 bug fix :protect getId() is null
                        if (n.getId() != null) {
                            if (n.getId().contains(DrawingFOV.className + element.getName())) {
                                n.setVisible(node.isVisible());
                            }   
                        }
                    }
                } else if (!node.isVisible()) {
                    // hide FOVs
                    for (Node n : rootDrawingPlane.getChildren()) {
                        // 11977 bug fix :protect getId() is null
                        if (n.getId() != null) {
                            if (n.getId().contains(DrawingFOV.className + element.getName())) {
                                n.setVisible(false);
                            }
                        }
                    }
                }
            } else {
                // display orbits for planets and comets
                Node orbit = findByName(element.getName() + DrawHelper.ORBIT + element.getClass().getSimpleName());
                if (orbit != null) {
                    orbit.setVisible(!orbit.isVisible());
                }
            }
        }
    }

    /**
     * Show/Hide all planets
     *
     * @param pVisibility
     */
    public void showAllPlanets(boolean pVisibility) {
        for (Node node : rootDrawingPlane.getChildren()) {            
            // 11977 bug fix :protect getId() is null
            if(node.getId()!=null){
                if (node.getId().contains(Planet.class.getSimpleName())) {
                    node.setVisible(pVisibility);
                }
            }
        }
        isVisibleAllPlanets = pVisibility;
    }

    /**
     * Show/Hide all probes
     *
     * @param pVisibility
     */
    public void showAllProbes(boolean pVisibility) {
        for (Node probeNode : rootDrawingPlane.getChildren()) {         
            // 11977 bug fix :protect getId() is null
            if (probeNode.getId() != null) {
                if (probeNode.getId().contains(Probe.class.getSimpleName())) {
                    probeNode.setVisible(pVisibility);
                }
            }            
        }
        showJMapFOV(pVisibility);
        for (String obs : isVisibleAllFOVs.keySet()) {
            if (isVisibleAllFOVs.get(obs)) {
                // show/hide ALL FOVs
                for (Node node : rootDrawingPlane.getChildren()) {
                    // 11977 bug fix :protect getId() is null
                    if (node.getId() != null) {
                        if (node.getId().contains(DrawingFOV.className + obs)) {
                            node.setVisible(pVisibility);
                        }
                    }                   
                }
            }
        }
        isVisibleAllProbes = pVisibility;
    }

    /**
     * Show/Hide all comets
     *
     * @param pVisibility
     */
    public void showAllComets(boolean pVisibility) {
        for (Node cometNode : rootDrawingPlane.getChildren()) {
            // 11977 bug fix :protect getId() is null
            if (cometNode.getId() != null) {
                if (cometNode.getId().contains(Comet.class.getSimpleName())) {
                    cometNode.setVisible(pVisibility);
                }
            }      
        }
        isVisibleAllComets = pVisibility;
    }

    public void showAllShocks(boolean pVisibility) {
        for (Node shockNode : rootDrawingPlane.getChildren()) {
            if (shockNode.getId().contains(Shockwave.className)) {
                shockNode.setVisible(pVisibility);
            }
        }
        isVisibleAllShocks = pVisibility;
    }

    /**
     * Show/Hide all cameras of an observer
     *
     * @param pVisibility
     */
    public void showAllFOV(String obs, boolean pVisibility) {
        Node node = findByName(obs + Probe.class.getSimpleName());
        if (node != null && node.isVisible()) {
            for (Node nFov : rootDrawingPlane.getChildren()) {
                // 11977 bug fix :protect getId() is null
                if (nFov.getId() != null) {
                    if (nFov.getId().contains(DrawingFOV.className + obs)) {
                        nFov.setVisible(pVisibility);
                    }   
                }             
            }
            isVisibleAllFOVs.put(obs, pVisibility);
        }
    }

    public void resetAllFOVs() {
        for (Node node : rootDrawingPlane.getChildren()) {
            // 11977 bug fix :protect getId() is null
            if (node.getId() != null) {
                if (node.getId().contains(DrawingFOV.className)) {
                    node.setVisible(false);
                }
            }         
        }

        for (String key : isVisibleAllFOVs.keySet()) {
            isVisibleAllFOVs.put(key, false);
        }
    }

    public void showJMapFOV() {
        Node node = findByName(jMapModel.getObserver() + Probe.class.getSimpleName());
        if (node.isVisible()) {
            showJMapFOV(!jMapFOV.isVisible());
        }
    }

    private Node findByName(String id) {
        return DrawHelper.findByName(rootDrawingPlane.getChildren(), id);
    }

    public void showLabelForAll() {
        for (Node n : rootDrawingPlane.getChildren()) {
            if (n.getId().contains(Label.class.getSimpleName())) {
                if (rootDrawingPlane.contains(n.getLayoutX(), n.getLayoutY())) {
                    n.setVisible(!n.isVisible());
                }
            }
        }
    }

    public void disableRightClick() {
        rootDrawingPlane.addEventFilter(MouseEvent.ANY, filter);
    }

    public void enableRightClick() {
        rootDrawingPlane.removeEventFilter(MouseEvent.ANY, filter);
    }

    public boolean isVisibleAllPlanets() {
        return isVisibleAllPlanets;
    }

    public boolean isVisibleAllProbes() {
        return isVisibleAllProbes;
    }

    public boolean isVisibleAllComets() {
        return isVisibleAllComets;
    }

    public boolean isVisibleAllShocks() {
        return isVisibleAllShocks;
    }

    public boolean isVisibleAllFOVs(String obs) {
        try {
            return isVisibleAllFOVs.get(obs);
        } catch (Exception e) {
            return false;
        }
    }

    public void setVisibleAllPlanets(boolean isVisibleAllPlanets) {
        this.isVisibleAllPlanets = isVisibleAllPlanets;
    }

    public void setVisibleAllComets(boolean isVisibleAllComets) {
        this.isVisibleAllComets = isVisibleAllComets;
    }

    public void setDrawingCME(boolean isDrawingCME) {
        this.isDrawingCME = isDrawingCME;
        if (cme != null) {
            if (cmeType.equals(CMEType.SSE)) {
                cmeFixPoint.setDisable(true);
                cmeSSE.setDisable(!isDrawingCME);
            } else {
                cmeFixPoint.setDisable(!isDrawingCME);
                cmeSSE.setDisable(true);
            }
        }
    }

//	public void setImage(java.awt.Image awtImage) {
//		if (awtImage == null) {
//			imageView.setImage(null);
//		}
//		else {
//			imageView.setImage(SwingFXUtils.toFXImage((BufferedImage) awtImage, null));
//		}
//	}
    public boolean isDrawingCME() {
        return isDrawingCME;
    }

    public void setDrawingSpiral(boolean isDrawingSpiral) {
        this.isDrawingSpiral = isDrawingSpiral;
        if (spiral != null) {
            spiral.setDisable(!isDrawingSpiral);
        }
    }

    public boolean isDrawingSpiral() {
        return isDrawingSpiral;
    }

    public boolean isDrawingJMapFOV() {
        return isDrawingJMapFOV;
    }

    public void setDrawingJMapFOV(boolean isDrawingJMapFOV) {
        this.isDrawingJMapFOV = isDrawingJMapFOV;
    }

    public boolean isShowingMap() {
        return isShowingMap;
    }

    public void setShowingMap(boolean isShowingMap) {
        this.isShowingMap = isShowingMap;
    }

    public String getTypeMap() {
        return typeMap;
    }

    public void setTypeMap(String typeMap) {
        this.typeMap = typeMap;
    }

    public boolean isInner(int naifId) {
        return (naifId >= 199 && naifId <= 499);
    }

    /**
     * @return the fieldlines
     */
    public DrawingFieldLines getFieldlines() {
        return fieldlines;
    }

}
