/* ----------------------------------------
 * 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)
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.eclipticPlane;

import java.util.Arrays;
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.spaceElement.Camera;
import fr.tlse.irap.spaceElement.Probe;
import fr.tlse.irap.spaceElement.SpaceElement;

public class EclipticPlaneItemBuilder {

	static final String SHOW_ALL = "All";
	static final String JMAP_FOV = "J-Map";
	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_FOVS = "Show/Hide FOVs";
	static final String SHOW_MAP_ENLIL = "Show/Hide Map ENLIL";
	static final String SHOW_SHOCK = "Show/Hide Shock waves";
	static final String RESET_OBJECTS = "Reset objects";
	static final String SEARCH_COMETS = "Search Comets";
	static final String THIN = "Thin";
	static final String SPIRAL_WIDTH_DELTAv = "Spiral width : Δv";
	static final String SPIRAL_WIDTH_DELTAphi = "Spiral width : Δφ";
	static final String SPIRAL_WIDTH_DELTAt = "Spiral width : Δt";
	static final String B_FIELD_MAP = "Show BField Map";
	static final String SPEED_MAP = "Show Speed Map";
	static final String DENSITY_MAP = "Show Density Map";
	static final String NO_MAP = "Hide Map";
	static final String SHOW_CME = "Show/Hide CME";
	static final String SHOW_SPIRAL = "Show/Hide Spiral";

	/**
	 * build the items of PLANETS
	 */
	public static Item getShowPlanetsItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {

		Item showPlanetsItem = new Item(SHOW_PLANETS);
		Item showAllItem = new Item(SHOW_ALL);
		//showPlanetsItem.addAll(Arrays.asList(showAllItem));
		showPlanetsItem.add(showAllItem);

		map.put(showAllItem, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runShowAllPlanets();
			}
		});

		for (final SpaceElement spaceElement : SpaceElementCollection.getSpaceElementWrapper().getPlanets()) {
			Item itemPlanet = new Item(spaceElement.getName());
			showPlanetsItem.add(itemPlanet);

			map.put(itemPlanet, new ISelectedItem() {
				@Override
				public void run() {
					iEclipticPlane.runShowPlanet(spaceElement);
				}
			});
		}

		return showPlanetsItem;
	}

	/**
	 * build the items of PROBES
	 */
	public static Item getShowProbesItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {

		Item showProbesItem = new Item(SHOW_PROBES);
		Item showAllItem = new Item(SHOW_ALL);
		showProbesItem.add(showAllItem);

		map.put(showAllItem, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runShowAllProbes();
			}
		});

		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() {
						iEclipticPlane.runShowProbe(spaceElement);
					}
				});
			}
		}

		return showProbesItem;
	}

	/**
	 * build the items of COMETS
	 */
	public static Item getShowCometsItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {

		Item showCometsItem = new Item(SHOW_COMETS);
		Item showAllItem = new Item(SHOW_ALL);
		showCometsItem.add(showAllItem);

		map.put(showAllItem, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runShowAllComets();
			}
		});

		for (final SpaceElement spaceElement : SpaceElementCollection.getSpaceElementWrapper().getComets()) {
			Item itemComet = new Item(spaceElement.getName());
			showCometsItem.add(itemComet);

			map.put(itemComet, new ISelectedItem() {
				@Override
				public void run() {
					iEclipticPlane.runShowComet(spaceElement);
				}
			});
		}
		return showCometsItem;
	}

	/**
	 * build the items of START_END
	 */
	public static Item getShowStartEndItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {

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

	/**
	 * build the item of Show Image ENLIL
	 */
//	public static Item getShowMapEnlilItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {
//		Item showMapEnlilItem = new Item(SHOW_MAP_ENLIL);
//		/* Childs */
//		Item bFieldMapItem = new Item(B_FIELD_MAP);
//		Item speedMapItem = new Item(SPEED_MAP);
//		Item densityMapItem = new Item(DENSITY_MAP);
//		Item noMapItem = new Item(NO_MAP);
//		showMapEnlilItem.addAll(Arrays.asList(bFieldMapItem, speedMapItem, densityMapItem, noMapItem));
//
//		map.put(bFieldMapItem, new ISelectedItem() {
//			@Override
//			public void run() {
//				iEclipticPlane.runShowMap("BMap");
//			}
//		});
//		map.put(speedMapItem, new ISelectedItem() {
//			@Override
//			public void run() {
//				iEclipticPlane.runShowMap("VMap");
//			}
//		});
//		map.put(densityMapItem, new ISelectedItem() {
//			@Override
//			public void run() {
//				iEclipticPlane.runShowMap("NMap");
//			}
//		});
//		map.put(noMapItem, new ISelectedItem() {
//			@Override
//			public void run() {
//				iEclipticPlane.runShowMap(null);
//			}
//		});
//
//		return showMapEnlilItem;
//	}

	/**
	 * build the item of CME
	 */
	public static Item getShowCMEItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {

		Item showCMEItem = new Item(SHOW_CME);
		map.put(showCMEItem, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runShowCME();
			}
		});

		return showCMEItem;
	}

	/**
	 * build the item of the spiral
	 * 
	 * @param map
	 * @param iEclipticPlane
	 * @return
	 */
	public static Item getShowSpiralItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {

		Item showSpiralItem = new Item(SHOW_SPIRAL);
		// childs
		Item thinItem = new Item(THIN);
		Item spiralDeltaV = new Item(SPIRAL_WIDTH_DELTAv);
		Item spiralDeltaPhi = new Item(SPIRAL_WIDTH_DELTAphi);
		Item spiralDeltaT = new Item(SPIRAL_WIDTH_DELTAt);
		
//		showSpiralItem.addAll(Arrays.asList(thinItem, spiralDeltaV, spiralDeltaPhi, spiralDeltaT));
		showSpiralItem.addAll(Arrays.asList(spiralDeltaPhi));

		map.put(thinItem, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runShowSpiral("thin");
			}
		});
		map.put(spiralDeltaV, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runShowSpiral("spiralDeltaV");
			}
		});
		map.put(spiralDeltaPhi, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runShowSpiral("spiralDeltaPhi");
			}
		});
		map.put(spiralDeltaT, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runShowSpiral("spiralDeltaT");
			}
		});
		return showSpiralItem;
	}

	/**
	 * build items of Show FOVS 
	 */
	public static Item getShowFovsItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane, boolean hasJMap) {

		Item showFOVsItem = new Item(SHOW_FOVS);

		if (hasJMap) {
			Item itemJmap = new Item(JMAP_FOV);
			map.put(itemJmap, new ISelectedItem() {
				@Override
				public void run() {
					iEclipticPlane.runShowJMapFOV();
				}
			});
			showFOVsItem.add(itemJmap);
		}

		for (final Probe probe : SpaceElementCollection.getSpaceElementWrapper().getProbes()) {

			if (probe.isDefined() && probe.isObserver()) {
				Item itemProbe = new Item(probe.getLongName());
				Item showAllItem = new Item(SHOW_ALL);
				itemProbe.add(showAllItem);

				map.put(showAllItem, new ISelectedItem() {
					@Override
					public void run() {
						iEclipticPlane.runShowAllFOVs(probe.getName());
					}
				});
				if (SpaceElementCollection.getCameras(probe.getName()) != null) {
				for (final Camera camera : SpaceElementCollection.getCameras(probe.getName())) {
					Item itemFOV = new Item(camera.getName());
					map.put(itemFOV, new ISelectedItem() {
						@Override
						public void run() {
							iEclipticPlane.runShowFOV(probe.getName(), camera.getName());
						}
					});
					itemProbe.add(itemFOV);
				}
				}
				showFOVsItem.add(itemProbe);
			}
		}
		return showFOVsItem;
	}

	/**
	 * build the item of SHOCK
	 */
	public static Item getShowShockwaves(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {

		Item item = new Item(SHOW_SHOCK);
		map.put(item, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runShowShocks();
			}
		});

		return item;
	}

	/**
	 *  Build Search comets item
	 */
	public static Item getSearchCometsItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {

		Item searchCometsItem = new Item(SEARCH_COMETS);
		map.put(searchCometsItem, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.searchComets();
			}
		});

		return searchCometsItem;
	}

	/**
	 *  Build Reset Object item
	 */
	public static Item getResetObjectsItem(Map<Item, ISelectedItem> map, final IEclipticPlane iEclipticPlane) {

		Item resetObjectsItem = new Item(RESET_OBJECTS);
		map.put(resetObjectsItem, new ISelectedItem() {
			@Override
			public void run() {
				iEclipticPlane.runReset();
			}
		});

		return resetObjectsItem;
	}

}
