/**
 * 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.protocol.p05;

import fr.cenra.rhomeo.api.data.DataContext;
import fr.cenra.rhomeo.api.data.Protocol;
import fr.cenra.rhomeo.api.data.Site;
import fr.cenra.rhomeo.api.data.TrackingPoint;
import fr.cenra.rhomeo.api.process.AbstractIndicator;
import fr.cenra.rhomeo.api.process.ProcessContext;
import fr.cenra.rhomeo.api.result.IndexSpi;
import fr.cenra.rhomeo.core.data.reference.OrthopteraIndicatorReference;
import fr.cenra.rhomeo.core.data.reference.OrthopteraIndicatorReferenceDescription;
import fr.cenra.rhomeo.core.data.reference.TaxrefDescription;
import fr.cenra.rhomeo.core.list.OrthoptereZoneBio;

import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import org.apache.sis.util.ArgumentChecks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * An abstraction of {@link I05} and {@link I09} indicators.
 *
 * @author Samuel Andrés (Geomatys) <samuel.andres at geomatys.com>
 */
public abstract class AbstractP05Indicator extends AbstractIndicator {

    @Autowired
    @Qualifier(P05.NAME)
    private Protocol protocol;

    @Autowired
    protected OrthopteraIndicatorReferenceDescription referenceDescription;

    @Autowired
    protected TaxrefDescription taxref;

    public AbstractP05Indicator(String name, String remarks) {
        super(name, remarks);
    }

    @Override
    public Protocol getProtocol() {
        return protocol;
    }

    /**
     *
     * @param reference
     *
     * @return The correct value of the reference according to the indicator.
     */
    abstract protected int getIndicatorValue(final OrthopteraIndicatorReference reference);

    /**
     *
     * @return The correct {@link IndexSpi} according to the indicator for the annual index.
     */
    abstract protected IndexSpi getAnnualIndexSpi();

    /**
     *
     * @return The correct {@link IndexSpi} according to the indicator for the annual index by {@link TrackingPoint}.
     */
    abstract protected IndexSpi getAnnualPointIndexSpi();


    /**
     * Checks if the statement is compatible to the specified zone.
     *
     * WHERE […]
     * AND
     * (
     * ortho_indicateurs.mediterraneen1_ml = 1 AND ortho_zbio.zbio::text = 'mediterraneen1_ml'::text
     * OR
     * ortho_indicateurs.mediterraneen1_ma = 1 AND ortho_zbio.zbio::text = 'mediterraneen1_ma'::text
     * OR
     * ortho_indicateurs.mediterraneen2_ma = 1 AND ortho_zbio.zbio::text = 'mediterraneen2_ma'::text
     * OR
     * ortho_indicateurs.mediterraneen2_tb = 1 AND ortho_zbio.zbio::text = 'mediterraneen2_tb'::text
     * OR
     * ortho_indicateurs.alpin_ma = 1 AND ortho_zbio.zbio::text = 'alpin_ma'::text
     * OR
     * ortho_indicateurs.alpin_tb = 1 AND ortho_zbio.zbio::text = 'alpin_tb'::text
     * OR
     * ortho_indicateurs.continental_ma = 1 AND ortho_zbio.zbio::text = 'continental_ma'::text
     * OR
     * ortho_indicateurs.continental_tb = 1 AND ortho_zbio.zbio::text = 'continental_tb'::text
     * )
     *
     * (Source : CCTP)
     *
     * Note : les options 'continental_*' ne sont pas supportées car non spécifiées dans la liste
     * {@link OrthoptereZoneBio}.
     *
     * @param reference
     * @param zone
     * @return true if there is a compatibility found between the {@link OrthopteraIndicatorReference}
     * and the given {@link OrthoptereZoneBio}.
     */
    public static boolean checkZoneConsistency(final OrthopteraIndicatorReference reference, final OrthoptereZoneBio zone) {
        switch(zone){
            case MED1_MARAIS_LITTORAL:
                return reference.getMediterraneen1_ml()==1;
            case MED1_MARAIS_ALLUVIAL:
                return reference.getMediterraneen1_ma()==1;
            case MED2_MARAIS_ALLUVIAL:
                return reference.getMediterraneen2_ma()==1;
            case MED2_TOURBIERE:
                return reference.getMediterraneen2_tb()==1;
            case ALPIN_MARAIS_ALLUVIAL:
                return reference.getAlpin_ma()==1;
            case ALPIN_TOURBIERE:
                return reference.getAlpin_tb()==1;
            default:
                return false;
        }
    }

    @Override
    public Set<fr.cenra.rhomeo.api.process.Process> createProcesses(final ProcessContext ctx) {
        ArgumentChecks.ensureNonNull("processContext", ctx);
        final String zoneCode = Optional.ofNullable(ctx.getDataCtx())
                .map(DataContext::getSite)
                .map(Site::getZoneType)
                .orElseThrow(() -> new IllegalStateException("Le contexte est invalide : impossible de déterminer le type de zone pour le site de travail"));
        if (getZoneTypeCodes().contains(zoneCode)) {
            return Collections.singleton(new Processor(ctx, this));
        } else return Collections.EMPTY_SET;
    }
}
