/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * ----------------------------------------
 */
package fr.tlse.irap.spectro;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class SpectroWrapper {

    /**
     * list of spectro energy ranges
     */
    private List<Float[]> energyList;
    /**
     * list of spectro energies
     */
    private List<Spectro> timedFluxes;
    /** Min value of all fluxes */
    private Float minValue = Float.MAX_VALUE;
    /** Max value of all fluxes */
    private Float maxValue = Float.MIN_VALUE;

    public SpectroWrapper() {
        energyList = new ArrayList<Float[]>();
        timedFluxes = new ArrayList<Spectro>();
    }

    public SpectroWrapper(List<Float[]> pNrjTable, List<Spectro> pTimedFluxes) {
        energyList = pNrjTable;
        timedFluxes = pTimedFluxes;
        computeMinMax();
    }

    private void computeMinMax() {
        minValue = Float.MAX_VALUE;
        maxValue = Float.MIN_VALUE;
        for (Spectro lSp : timedFluxes) {
            if (lSp.getFlux() != null) {
                for (Float lVal : lSp.getFlux()) {
                    if (lVal > 0) {
                        if (lVal > maxValue) {
                            maxValue = lVal;
                        }
                        if (lVal < minValue) {
                            minValue = lVal;
                        }
                    }
                }
            }
        }
        //System.out.println("*******************SpectroWrapper:computeMinMax "+minValue+" "+maxValue);
    }

    public Spectro getNearestFromTime(Calendar calendar) {
        Spectro previous = (timedFluxes.isEmpty() ? null : timedFluxes.get(0));
        Spectro next = null;

        for (Spectro flux : this.timedFluxes) {
            if (flux.getEventTime().after(calendar)) {
                next = flux;
                break;
            }
            previous = flux;
        }

        if (next == null) {
            return previous;
        } else {
            Long deltaPrevious = Math.abs(previous.getEventTime().getTimeInMillis() - calendar.getTimeInMillis());
            Long deltaNext = Math.abs(next.getEventTime().getTimeInMillis() - calendar.getTimeInMillis());
            return (deltaPrevious < deltaNext ? previous : next);
        }
    }

    /**
     * Get energy ranges
     *
     * @return the energyList
     */
    @XmlElement
    public List<Float[]> getEnergyList() {
        return energyList;
    }

    /**
     * set energy ranges
     *
     * @param energyList the energyList to set
     */
    public void setEnergyList(List<Float[]> energyList) {
        this.energyList = energyList;
    }

    /**
     * @return the timedFluxes
     */
    @XmlElement
    public List<Spectro> getTimedFluxes() {
        return timedFluxes;
    }

    /**
     * @param timedFluxes the timedFluxes to set
     */
    public void setTimedFluxes(List<Spectro> timedFluxes) {
        this.timedFluxes = timedFluxes;
        computeMinMax();
    }

    /**
     * @return the minValue
     */
    @XmlElement
    public Float getMinValue() {
        if(minValue == Float.MAX_VALUE) computeMinMax();
        return minValue;
    }

    /**
     * @return the maxValue
     */
    @XmlElement
    public Float getMaxValue() {
        if(maxValue == Float.MIN_VALUE) computeMinMax();
        return maxValue;
    }
}
