/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Version : 29/06/2022 : LBE : V2.5
 * DM-ID : Redmine 9582: Catalogs range directly selected in menu
 * Version : 25/11/2022 : LBE : V2.6
 * DM-ID : Redmine#10630 : AMDA zoom type
 * Version : 12/09/2023 : LBE : V2.8
 * DM-ID : Redmine#10629 : 12/09/2023 : SEP plot improvments
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.plot;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
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.tools.plot.VPlotModel.Catalog;
import fr.tlse.irap.pt.client.controller.tools.plot.VPlotModel.CatalogType;
import fr.tlse.irap.pt.client.controller.tools.plot.VPlotModel.DataSource;

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

    public static final String DATA_SOURCE = "Choose data source";
    public static final String ZOOM = "Zoom";
    public static final String ZOOM_SET = "Set";
    //public static final String ZOOM_T2 = "T2 : ";
    public static final String ZOOM_RESET = "Reset";

    private static Map<String, List<CatalogType>> catalogTypes = new HashMap<String, List<CatalogType>>();

    /**
     * Define choose data source menu
     *
     * @param map
     * @param iCarrington
     * @return
     */
    public static Item getChooseDataSourceItem(Map<Item, ISelectedItem> map, final IVPlotMenu vPlotMenu) {
        Item chooseItem = new Item(DATA_SOURCE);
        Item realItem = new Item(DataSource.REAL.name());
        Item simulatedItem = new Item(DataSource.SIMULATED.name());
        chooseItem.addAll(Arrays.asList(realItem, simulatedItem));

        map.put(realItem, new ISelectedItem() {
            @Override
            public void run() {
                vPlotMenu.runChooseData(DataSource.REAL);
            }
        });

        map.put(simulatedItem, new ISelectedItem() {
            @Override
            public void run() {
                vPlotMenu.runChooseData(DataSource.SIMULATED);
            }
        });

        return chooseItem;
    }

    /**
     * Define In Situ catalogue menu
     *
     * @param map
     * @param iCarrington
     * @return
     */
    public static Item getInSituCatalogItem(Map<Item, ISelectedItem> map, final IVPlotMenu vPlotMenu) {
        Item catItem = new Item("In situ catalogue");
        for (Catalog catalog : Catalog.values()) {
            catItem.add(getCatalogItem(catalog, map, vPlotMenu));
        }
        //vPlotMenu.runInSituCatalogDataRange(catItem,Catalog.values());
        return catItem;
    }

    private static Item getCatalogItem(final Catalog catalog, Map<Item, ISelectedItem> map, final IVPlotMenu vPlotMenu) {
        Item catItem = new Item(catalog.getName());
        for (final CatalogType catType : getCatalogTypes(catalog)) {
            Item ctItem = new Item(catType.getName());
//			map.put(ctItem, new ISelectedItem() {
//				@Override
//				public void run() {
//					vPlotMenu.runInSituCatalog(catalog.name(), catType.name());
//					}
//				});
            catItem.add(ctItem);
            //System.out.println("==>>> ************ VPlotItemBuilder get ranges for " + catalog.name() + "/" + catType.name());
            //vPlotMenu.runInSituCatalogDataRange(ctItem,catalog.name(), catType.name());
        }
        return catItem;
    }

    /**
     * Get types list of a catalog
     */
    public static List<CatalogType> getCatalogTypes(Catalog catalog) {
        if (catalogTypes.isEmpty()) {
            initCatalogTypes();
        }
        return catalogTypes.get(catalog.name());
    }

    /**
     * Init spacecraft wave lengths
     */
    private static void initCatalogTypes() {
        for (Catalog catalog : Catalog.values()) {
            List<CatalogType> catTypes = new ArrayList<CatalogType>();
            if (catalog.equals(Catalog.SHOCK)) {
                // specific case of SHOCKS catalog 
                catTypes.add(CatalogType.KASPER);
            } else {
                // CME, CIR, SEP catalogs
                catTypes.add(CatalogType.HELCATS);
                catTypes.add(CatalogType.JIAN);
                catTypes.add(CatalogType.LEPPING);
                catTypes.add(CatalogType.RICHARDSON);
            }
            catalogTypes.put(catalog.name(), catTypes);
        }
    }

    /**
     * Build items of Zoom
     */
    public static Item getZoomItem(Map<Item, ISelectedItem> map, final IVPlotMenu vPlotMenu) {
        Item zoomItem = new Item(ZOOM);
        Item selectT1Item = new Item(ZOOM_SET);
        zoomItem.add(selectT1Item);
        map.put(selectT1Item, new ISelectedItem() {
            @Override
            public void run() {
                vPlotMenu.runSetZoom();
            }
        });
//		Item selectT2Item = new Item(ZOOM_T2);
//		zoomItem.add(selectT2Item);
//		map.put(selectT2Item, new ISelectedItem() {
//			@Override
//			public void run() {
//				vPlotMenu.runZoomT2();
//			}
//		});
        Item ResetItem = new Item(ZOOM_RESET);
        zoomItem.add(ResetItem);
        map.put(ResetItem, new ISelectedItem() {
            @Override
            public void run() {
                vPlotMenu.runResetZoom();
            }
        });

        return zoomItem;
    }

}
