/* ----------------------------------------
 * 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
 * Version : 06/11/2017 : SCA : V2.2.1 
 *  Redmine #5403 - DMPT3 
 * Version : 09/04/2018 : SCA : V2.2.4
 *  Redmine DMPT4
 * Version : 29/06/2022 : LBE : V2.5
 * DM-ID : Redmine 9229: PFSS mode
 * Version : 30/11/2022 : LBE : V2.6
 * DM-ID : Redmine #l0481 : 30/11/2022 : HCS and PFSS FL added to carrington map
 * Version : 05/08/2024 : LBE : V2.11
 * DM-ID : Redmine#l0763 : 05/08/2024 : Field lines menu replaced by CBX
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.carrington;

import java.util.Map;

import fr.tlse.irap.pt.client.controller.common.ISelectedItem;
import fr.tlse.irap.pt.client.controller.common.Item;
import fr.tlse.irap.pt.client.controller.common.SpaceElementCollection;
import fr.tlse.irap.flare.Flare.FlareType;
import fr.tlse.irap.map.CarringtonMapType;
import fr.tlse.irap.spaceElement.SpaceElement;
import javafx.scene.control.CheckMenuItem;

public class CarringtonItemBuilder {

	static final String SHOW_PLANETS = "Show/Hide Planets";
	static final String SHOW_PROBES = "Show/Hide Probes";
	static final String SHOW_COMETS = "Show/Hide Comets";
	static final String SHOW_START_END = "Show/Hide Start/End";
	static final String SHOW_FLARES = "Show/Hide Flares";
	static final String SHOW_SHOCKS = "Show/Hide Shock waves";
	static final String SHOW_HCS = "Show/Hide HCS";
	static final String RESET_OBJECTS = "Reset objects";
	static final String SOURCE = "Click source";
	static final String EXTENT = "Click extent";
	static final String SHOW_ALL = "All";

	public static final String DEFINE_CME = "Define CME";
	public static final String DEFINE_SPIRAL= "Define Spiral";
	
	/**
	 * Define Carrington map menu
	 * @param map
	 * @param iCarrington
	 * @return choose map menu item
	 */
	public static Item getCarringtonMapItem(Map<Item, ISelectedItem> map, final ICarringtonMenu carringtonMenu) {
		return getCMapItem(CarringtonModel.getTreeCMaps(), map, carringtonMenu);
	}

	/**
	 * Define Carrington map menu from a tree nodes 
	 * @param tree nodes
	 * @param map
	 * @param carringtonMenu
	 * @return
	 */
    private static Item getCMapItem(final MTreeNode node, Map<Item, ISelectedItem> map, final ICarringtonMenu carringtonMenu) {

        if (node.getLevel() < 4) {
            final Item menu = new Item(node.getName());
            for (MTreeNode subNode : node.getChildren()) {
                menu.add(getCMapItem(subNode, map, carringtonMenu));
            }
            return menu;
            
        } else {
            final Item menuItem = new Item(node.getName());
			map.put(menuItem, new ISelectedItem() {
				@Override
				public void run() {
					CarringtonMapType cmType = new CarringtonMapType();
					cmType.setProbe(node.getParent().getParent().getName());
					cmType.setInstrument(node.getParent().getName());
					cmType.setMeasurement(node.getName());
					if (node.hasChild()) {
						cmType.getRadialDistances().add(node.getChildren().get(0).getName());
		            }
					carringtonMenu.runCarringtonMap(cmType);
				}
			});
            return menuItem;
        }
    }

	/**
	 * Define Field lines menu
	 * @param map
	 * @param iCarrington
	 * @return Field lines menu item
	 */
//	public static Item getFieldLinesItem(Map<Item, ISelectedItem> map, final ICarringtonMenu carringtonMenu) {
//		return getFieldLinesItem(CarringtonCMEModel.getTreeFieldLines(), map, carringtonMenu);
//	}

	/**
	 * Define Field lines menu from a tree nodes 
	 * @param tree nodes
	 * @param map
	 * @param carringtonMenu
	 * @return
	 */
//    private static Item getFieldLinesItem(final MTreeNode node, Map<Item, ISelectedItem> map, final ICarringtonMenu carringtonMenu) {
//
//        
////        final Item menu = new Item("PFSS");
////        CheckMenuItem lCmi = new CheckMenuItem("BEST_OF");
////        menu.add(lCmi);
//        
//    	if (node.getLevel() < 3) {
//            final Item menu = new Item(node.getName());
//            for (MTreeNode subNode : node.getChildren()) {
//                menu.add(getFieldLinesItem(subNode, map, carringtonMenu));
//            }
//            return menu;
//            
//        } else {
//	        final Item menuItem = new Item(node.getName());
//			map.put(menuItem, new ISelectedItem() {
//				@Override
//				public void run() {
//					String model = node.getParent().getName();
//					String source = node.getName();
//					carringtonMenu.runFieldLines(model, source);
//				}
//			});
//	        return menuItem;
//        }
//    }

	/**
	 * Define CME menu
	 */
	public static Item getDefineCMEItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {
		Item defineCMEItem = new Item(DEFINE_CME);
		Item clickSourceItem = new Item(SOURCE);
		defineCMEItem.add(clickSourceItem);
		map.put(clickSourceItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runCMEClickSource();
			}
		});
		Item clickExtentItem = new Item(EXTENT);
		defineCMEItem.add(clickExtentItem);
		map.put(clickExtentItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runCMEClickExtent();
			}
		});

		return defineCMEItem;
	}

	/**
	 * Define CIR menu
	 */
	public static Item getDefineSpiralItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {
		Item defineCIRItem = new Item(DEFINE_SPIRAL);
		Item clickSourceItem = new Item(SOURCE);
		defineCIRItem.add(clickSourceItem);
		map.put(clickSourceItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runCMEClickSource();
			}
		});
		Item clickExtentItem = new Item(EXTENT);
		defineCIRItem.add(clickExtentItem);
		map.put(clickExtentItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runCMEClickExtent();
			}
		});

		return defineCIRItem;
	}

	/**
	 * Define Show Hide planets menu 
	 */
	public static Item getShowPlanetsItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {
		Item showPlanetsItem = new Item(SHOW_PLANETS);
		Item showAllItem = new Item(SHOW_ALL);
		showPlanetsItem.add(showAllItem);
		map.put(showAllItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runShowAllPlanets();
			}
		});
		// sub-menus
		for (final SpaceElement spaceElement : SpaceElementCollection.getPlanets()) {
			Item itemPlanet = new Item(spaceElement.getName());
			showPlanetsItem.add(itemPlanet);
			map.put(itemPlanet, new ISelectedItem() {
				@Override
				public void run() {
					iCarrington.runShowPlanet(spaceElement);
				}
			});
		}
		return showPlanetsItem;
	}

	/**
	 * Define Show Hide probes menu 
	 */
	public static Item getShowProbesItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {
		Item showProbesItem = new Item(SHOW_PROBES);
		Item showAllItem = new Item(SHOW_ALL);
		showProbesItem.add(showAllItem);
		map.put(showAllItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runShowAllProbes();
			}
		});
		// sub-menus
		//for (final SpaceElement spaceElement : SpaceElementCollection.getProbes()) {
		for (final SpaceElement spaceElement : SpaceElementCollection.getSpaceElementWrapper().getProbes()) {
			if (spaceElement.isDefined()) {
				Item itemProbe = new Item(spaceElement.getLongName());
				showProbesItem.add(itemProbe);
				map.put(itemProbe, new ISelectedItem() {
					@Override
					public void run() {
						iCarrington.runShowProbe(spaceElement);
					}
				});
			}
		}
		return showProbesItem;
	}

	/**
	 * Define Show Hide comets menu 
	 */
	public static Item getShowCometsItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {
		Item showCometsItem = new Item(SHOW_COMETS);
		Item showAllItem = new Item(SHOW_ALL);
		showCometsItem.add(showAllItem);
		map.put(showAllItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runShowAllComets();
			}
		});
		// sub-menus
		for (final SpaceElement spaceElement : SpaceElementCollection.getComets()) {
			Item itemComet = new Item(spaceElement.getName());
			showCometsItem.add(itemComet);
			map.put(itemComet, new ISelectedItem() {
				@Override
				public void run() {
					iCarrington.runShowComet(spaceElement);
				}
			});
		}
		return showCometsItem;
	}

	/**
	 * Define Show Hide Start/End menu
	 */
	public static Item getShowStartEndItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {

		Item showStartEndItem = new Item(SHOW_START_END);
		map.put(showStartEndItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runShowStartEnd();
			}
		});
		return showStartEndItem;
	}

	/**
	 * Show flare menu item builder 
	 */
	public static Item getShowFlaresItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {
		Item showFlaresItem = new Item(SHOW_FLARES);
		/* Childs */
		Item allItem = new Item("All");
		showFlaresItem.add(allItem);
		map.put(allItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runShowAllFlares();
			}
		});
		for (final FlareType ft : FlareType.values()) {
			Item ftItem = new Item(ft + "-Class");
			showFlaresItem.add(ftItem);
			map.put(ftItem, new ISelectedItem() {
				@Override
				public void run() {
					iCarrington.runShowFlares(ft);
				}
			});
		}
		return showFlaresItem;
	}

	/**
	 * Show shocks menu item builder 
	 */
	public static Item getShowShocksItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {
		Item showShocksItem = new Item(SHOW_SHOCKS);
		/* Childs */
		map.put(showShocksItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runShowShocks();
			}
		});
		return showShocksItem;
	}
        
	/**
	 * Show HCS menu item builder 
	 */
	public static Item getShowHcsItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {
		Item lShowItem = new Item(SHOW_HCS);
		/* Childs */
		map.put(lShowItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runShowHcs();
			}
		});
		return lShowItem;
	}

	/**
	 * Reset objetcs item builder 
	 */
	public static Item getResetObjectsItem(Map<Item, ISelectedItem> map, final ICarringtonMenu iCarrington) {
		Item resetObjectsItem = new Item(RESET_OBJECTS);
		map.put(resetObjectsItem, new ISelectedItem() {
			@Override
			public void run() {
				iCarrington.runReset();
			}
		});

		return resetObjectsItem;
	}

}
