/**
 * Copyright © CENRA (2016)
 *
 * remi.clement@espaces-naturels.fr
 * 
 * This software is a multi-platform and portable application based on 
 * the scientifical toolbox to monitor wetlands. With this toolbox it’s 
 * possible to calculate indicators «RhoMéO» to monitor quality, 
 * fonctionalities and urban and agricultural pressures of wetlands. 
 * Indicators are calculated using naturalist data entered and/or imported,
 * and geographic data downloaded from a geographic server.
 * 
 * This software is governed by the CeCILL license under French law and
 * abiding by the rules of distribution of free software.  You can  use, 
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info". 
 * 
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability. 
 * 
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or 
 * data to be ensured and,  more generally, to use and operate it in the 
 * same conditions as regards security. 
 * 
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
*/
package fr.cenra.rhomeo.api.result;

import java.util.HashMap;
import java.util.Map;

/**
 * POJO regrouping indicator values for each year.
 *
 * @author Cédric Briançon (Geomatys)
 */
public class IndicatorValuesByYearItem implements Comparable<IndicatorValuesByYearItem> {
    private final int year;

    private final Map<String, PublishedValue> indicatorValues = new HashMap<>();

    public IndicatorValuesByYearItem(final int year) {
        this.year = year;
    }

    public int getYear() {
        return year;
    }

    /**
     * Get the value for the given indicator. May return {@code null} if not found.
     *
     * @param indicator Indicator name.
     * @return The value or {@code null} if not defined for this indicator
     */
    public PublishedValue getValueForIndicator(final String indicator) {
        return indicatorValues.get(indicator);
    }

    public void addValueForIndicator(final String indicator, final Double value, final boolean published) {
        indicatorValues.put(indicator, new PublishedValue(value, published));
    }
    
    @Override
    public int compareTo(IndicatorValuesByYearItem o) {
        if (o == null) {
            return -1;
        }
        return year < o.getYear() ? -1 : year == o.getYear() ? 0 : 1;
    }

    /**
     * Try to find if the value for this indicator is published.
     *
     * @param indicatorName
     * @return True or false depending if the value has been published or not for this indicator,
     *         or {@code null} if not defined for this indicator.
     */
    public Boolean isPublished(final String indicatorName) {
        final PublishedValue pubVal = indicatorValues.get(indicatorName);
        if (pubVal == null) {
            return null;
        }
        return pubVal.published;
    }

    public static final class PublishedValue {
        
        private Double value;
        private boolean published;

        PublishedValue(final Double value, final boolean published) {
            this.value = value;
            this.published = published;
        }

        public Double getValue() {
            return value;
        }

        public boolean isPublished() {
            return published;
        }

        public void setPublished(boolean published) {
            this.published = published;
        }
    }
}
