/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 17/11/2017 : SCA : V2.2.2
 *  Creation
 * Version : 28/01/2022 : LBE
 * FA-ID : Redmine#9169 : 28/01/2022 : GOES14=>GOES
 * Version : 25/11/2022 : LBE : V2.6
 * DM-ID : Redmine#10630 : AMDA zoom type
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.plot;

import java.util.ArrayList;
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.FluxPlotModel.FluxMeasure;
import fr.tlse.irap.pt.client.controller.tools.plot.FluxPlotModel.FluxType;
import static fr.tlse.irap.pt.client.controller.tools.plot.SpectroPlotItemBuilder.ZOOM_SET;
import fr.tlse.irap.flare.Flare.FlareType;

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

    public static final String ZOOM = "Zoom";
    public static final String ZOOM_SET = "Set zoom";
    public static final String ZOOM_RESET = "Reset";

    private static Map<FluxType, List<FluxMeasure>> fluxTypeMeasures = new HashMap<FluxType, List<FluxMeasure>>();

    /**
     * Init flare flux type measures
     */
    private static void initFluxTypeMeasures() {
        if (fluxTypeMeasures.isEmpty()) {
            // EUV
            List<FluxMeasure> fmEUV = new ArrayList<FluxMeasure>();
            fmEUV.add(FluxMeasure.EUV);
            fluxTypeMeasures.put(FluxType.EUV, fmEUV);
            // Soft
            List<FluxMeasure> fmGOES = new ArrayList<FluxMeasure>();
            fmGOES.add(FluxMeasure.GOES14_05_40A);
            fmGOES.add(FluxMeasure.GOES14_10_80A);
            fluxTypeMeasures.put(FluxType.SOFT_XRAYS, fmGOES);
            // Hard
            List<FluxMeasure> fmH = new ArrayList<FluxMeasure>();
            fmH.add(FluxMeasure.HARDXRAYS);
            fluxTypeMeasures.put(FluxType.HARD_XRAYS, fmH);
            // Gamma
            List<FluxMeasure> fmGAMMA = new ArrayList<FluxMeasure>();
            fmGAMMA.add(FluxMeasure.GAMMA);
            fluxTypeMeasures.put(FluxType.GAMMA, fmGAMMA);
        }
    }

    /**
     * Get list of measures of an flareFlux Type
     */
    public static List<FluxMeasure> getMeasures(FluxType fType) {
        if (fluxTypeMeasures.isEmpty()) {
            initFluxTypeMeasures();
        }
        return fluxTypeMeasures.get(fType);
    }

    /**
     * Define catalogue menu
     *
     * @param map
     * @param flarePlotMenu
     * @return
     */
    public static Item getShowFluxItem(Map<Item, ISelectedItem> map, final IFluxPlotMenu flarePlotMenu) {
        Item catItem = new Item("Show/Hide Flux");
        for (final FluxType fType : FluxType.values()) {
            Item ftItem = new Item(fType.getName());
            List<FluxMeasure> fms = getMeasures(fType);
            if (fms != null) {
                Item showAllItem = new Item("All");
                map.put(showAllItem, new ISelectedItem() {
                    @Override
                    public void run() {
                        flarePlotMenu.runShowAllFlux(fType);
                    }
                });
                ftItem.add(showAllItem);

                int i = 0;
                for (final FluxMeasure fMeasure : fms) {
                    Item fmItem = new Item(fMeasure.getName());
                    final int iMeasure = i;
                    map.put(fmItem, new ISelectedItem() {
                        @Override
                        public void run() {
                            flarePlotMenu.runShowFlux(fType, fMeasure, iMeasure);
                        }
                    });
                    ftItem.add(fmItem);
                    i++;
                }
            }
            catItem.add(ftItem);
        }
        return catItem;
    }

    /**
     * Show flare menu item builder
     */
    public static Item getShowFlaresItem(Map<Item, ISelectedItem> map, final IFluxPlotMenu flarePlotMenu) {
        Item showFlaresItem = new Item("Show/Hide Flares");
        /* Childs */
        Item allItem = new Item("All");
        showFlaresItem.add(allItem);
        map.put(allItem, new ISelectedItem() {
            @Override
            public void run() {
                flarePlotMenu.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() {
                    flarePlotMenu.runShowFlares(ft);
                }
            });
        }

        return showFlaresItem;
    }

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

        return zoomItem;
    }

}
