/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Version : 14/04/2016 : SCA : V2.2 (PSWS-H2020)
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.jmap;

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.pt.client.controller.tools.jmap.JMapModel.InstrumentFov;
import fr.tlse.irap.pt.client.controller.tools.jmap.JMapModel.SourceType;
import fr.tlse.irap.spaceElement.Probe;
import fr.tlse.irap.spaceElement.SpaceElement;
import fr.tlse.irap.tool.radial.CMEType;

/**
 * 
 * JMap contextual menu builder
 * 
 */
public class JMapItemBuilder {

	static final String CHOOSE_MAP = "Choose map";
	static final String CHOOSE_CATALOG = "Choose Catalogue";
	static final String SELECT_FIT = "Select fit by click";
	static final String ADD_FIT = "Add fit points";
	static final String CLICK_POINTS = "Enter points by click";
    static final String REMOVE_LAST_POINT = "Remove last point";
    static final String CLICK_POINTS_FILE = "Load file of points";
    static final String SAVE_POINTS_FILE = "Save points";
	static final String FIT_POINTS = "Fit points";
	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_ALL = "All";
	static final String ZOOM = "Zoom";
	static final String ZOOM_RESET = "Reset";
	static final String RESET_OBJECTS = "Reset objects";

	public static final String ZOOM_P1 = "P1 : ";
	public static final String ZOOM_P2 = "P2 : ";

	/**
	 * Define choose map menu
 	 * @param map
	 * @param iCarrington
	 * @return
	 */
	public static Item getChooseMapItem( Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		/* Define main Choose map menu */
		Item chooseMapItem = new Item(CHOOSE_MAP);

		for (final InstrumentFov instr : InstrumentFov.values()) {
			Probe obs = SpaceElementCollection.getSpaceElementWrapper().getProbe(instr.getObs());
			if (obs.isDefined()) {
				Item obsItem = new Item(instr.getName());
				Item realItem = new Item(SourceType.REAL.name());
				Item enlilItem = new Item(SourceType.ENLIL.name());
				obsItem.addAll(Arrays.asList(realItem, enlilItem));
	
				map.put(realItem, new ISelectedItem() {
					@Override
					public void run() {
						iJMap.runChooseMap(instr.getName(), SourceType.REAL.name());
					}
				});
				map.put(enlilItem, new ISelectedItem() {
					@Override
					public void run() {
						iJMap.runChooseMap(instr.getName(), SourceType.ENLIL.name());
					}
				});
				chooseMapItem.add(obsItem);
			}
		}
		
		return chooseMapItem;
	}

	public static Item getChooseCatalogItem(Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		Item chooseCatalog = new Item(CHOOSE_CATALOG);
		Item wp3Item = new Item("HELCATS HIGeoCAT");
		chooseCatalog.add(wp3Item);
		for (final CMEType cmeType : CMEType.values()) {
			//Item item = new Item("HELCATS " + cmeType.getName());
			Item item = new Item(cmeType.getName());
			map.put(item, new ISelectedItem() {
				@Override
				public void run() {
					iJMap.runChooseCatalog(false, cmeType);
				}
			});
			wp3Item.add(item);
		}
		Item wp4Item = new Item("HELCATS LINKCAT");
		chooseCatalog.add(wp4Item);
		Item item = new Item(CMEType.SSE.getName());
		map.put(item, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runChooseCatalog(true, CMEType.SSE);
			}
		});
		wp4Item.add(item);
		//chooseCatalog.add(item);
		return chooseCatalog;
	}

	/** 
	 * Build Select Fit points items 
	 */
	public static Item getSelectFitItem(Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		Item selectFitItem = new Item(SELECT_FIT);
		map.put(selectFitItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runSelectFit();
			}
		});

		return selectFitItem;
	}

	/** 
	 * Build Add Fit items 
	 */
	public static Item getAddFitItem(Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		Item addFitItem = new Item(ADD_FIT);
		/* Childs */
		Item clickPointsItem = new Item(CLICK_POINTS);
		addFitItem.add(clickPointsItem);
		map.put(clickPointsItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runClickPoints();
			}
		});
		Item removeLastPointItem = new Item(REMOVE_LAST_POINT);
		addFitItem.add(removeLastPointItem);
		map.put(removeLastPointItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runRemoveLastPoint();
			}
		});
		Item clickPointsFileItem = new Item(CLICK_POINTS_FILE);
        addFitItem.add(clickPointsFileItem);
        map.put(clickPointsFileItem, new ISelectedItem() {
            @Override
            public void run() {
                iJMap.runClickPointsFile();
            }
        });
		Item savePointsFileItem = new Item(SAVE_POINTS_FILE);
        addFitItem.add(savePointsFileItem);
        map.put(savePointsFileItem, new ISelectedItem() {
            @Override
            public void run() {
                iJMap.runSavePoints();
            }
        });
		Item fitPointsItem = new Item(FIT_POINTS);
		addFitItem.add(fitPointsItem);
		map.put(fitPointsItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runFitPoints();
			}
		});

		return addFitItem;
	}

	/** 
	 * Build Show Planets items 
	 */
	public static Item getShowPlanetsItem(Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		Item showPlanetsItem = new Item(SHOW_PLANETS);
		Item showAllItem = new Item(SHOW_ALL);
		showPlanetsItem.add(showAllItem);
		map.put(showAllItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runShowAllPlanets();
			}
		});
		for (final SpaceElement spaceElement : SpaceElementCollection.getPlanets()) {
			Item itemPlanet = new Item(spaceElement.getName());
			showPlanetsItem.add(itemPlanet);

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

	/**
	 * build the items of PROBES
	 */
	public static Item getShowProbesItem(Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		Item showProbesItem = new Item(SHOW_PROBES);
		// childs
		Item showAllItem = new Item(SHOW_ALL);
		showProbesItem.add(showAllItem);

		map.put(showAllItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runShowAllProbes();
			}
		});
		//for (final SpaceElement spaceElement : SpaceElementCollection.getProbes()) {
		for (final Probe probe : SpaceElementCollection.getSpaceElementWrapper().getProbes()) {
			if (probe.isDefined()) {
				Item itemProbe = new Item(probe.getLongName());
				showProbesItem.add(itemProbe);
				map.put(itemProbe, new ISelectedItem() {
					@Override
					public void run() {
						iJMap.runShowProbe(probe);
					}
				});
			}
		}
		return showProbesItem;
	}

	/**
	 * build the items of COMETS
	 */
	public static Item getShowCometsItem(Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		Item showCometsItem = new Item(SHOW_COMETS);
		Item showAllItem = new Item(SHOW_ALL);
		showCometsItem.add(showAllItem);
		map.put(showAllItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runShowAllComets();
			}
		});
		for (final SpaceElement spaceElement : SpaceElementCollection.getComets()) {
			Item itemComet = new Item(spaceElement.getName());
			showCometsItem.add(itemComet);
			map.put(itemComet, new ISelectedItem() {
				@Override
				public void run() {
					iJMap.runShowComet(spaceElement);
				}
			});
		}
		return showCometsItem;
	}

	/**
	 * Define Show Hide Start/End menu
	 */
	public static Item getShowStartEndItem(Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		Item showStartEndItem = new Item(SHOW_START_END);
		map.put(showStartEndItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runShowStartEnd();
			}
		});
		return showStartEndItem;
	}

	/**
	 * Build items of Zoom
	 */
	public static Item getZoomItem(Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		Item zoomItem = new Item(ZOOM);
		Item selectP1Item = new Item(ZOOM_P1);
		zoomItem.add(selectP1Item);
		map.put(selectP1Item, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runZoomP1();
			}
		});
		Item selectP2Item = new Item(ZOOM_P2);
		zoomItem.add(selectP2Item);
		map.put(selectP2Item, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runZoomP2();
			}
		});
		Item ResetItem = new Item(ZOOM_RESET);
		zoomItem.add(ResetItem);
		map.put(ResetItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runResetZoom();
			}
		});

		return zoomItem;
	}

	/**
	 * Build item Reset objects
	 */
	public static Item getShowResetObjectsItem(Map<Item, ISelectedItem> map, final IJMapMenu iJMap) {
		Item showResetObjectsItem = new Item(RESET_OBJECTS);
		map.put(showResetObjectsItem, new ISelectedItem() {
			@Override
			public void run() {
				iJMap.runReset();
			}
		});

		return showResetObjectsItem;
	}

}
