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

import fr.cenra.rhomeo.api.annotations.RefersTo;
import fr.cenra.rhomeo.api.data.Statement;
import fr.cenra.rhomeo.api.data.Warning;
import fr.cenra.rhomeo.core.data.reference.FloreBassinReference;
import fr.cenra.rhomeo.protocol.p02.list.AbundanceFlore;
import fr.cenra.rhomeo.protocol.p02.list.PhysionomyFlore;
import fr.cenra.rhomeo.protocol.p02.validation.DeterminedFloreSpecies;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import java.time.LocalDate;
import java.util.Objects;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import org.hibernate.validator.constraints.NotBlank;

/**
 * @author Cédric Briançon (Geomatys)
 */
public class FloreStatement extends Statement {

    private final StringProperty physionomyProperty = new SimpleStringProperty();
    private final IntegerProperty cd_nomProperty = new SimpleIntegerProperty();
    private final StringProperty abundanceProperty = new SimpleStringProperty();

    public FloreStatement() {
    }

    public FloreStatement(String trackingPoint, LocalDate date, String remarks, String physionomy, int speciesName, String abundance) {
        super(trackingPoint, date);
        this.remarksProperty.set(remarks);
        this.physionomyProperty.set(physionomy);
        this.cd_nomProperty.set(speciesName);
        this.abundanceProperty.set(abundance);
    }


    public StringProperty physionomyProperty() {
        return physionomyProperty;
    }

    public void setPhysionomy(final String physionomy) {
        this.physionomyProperty.set(physionomy);
    }

    public IntegerProperty cd_nomProperty() {
        return cd_nomProperty;
    }

    public void setCd_nom(final int cd_nom) {
        this.cd_nomProperty.set(cd_nom);
    }

    public StringProperty abundanceProperty() {
        return abundanceProperty;
    }

    public void setAbundance(final String abundance) {
        this.abundanceProperty.set(abundance);
    }

    /**
     * PHYSIONOMIE
     *
     * (Source : CCTP)
     *
     * @return
     */
    @NotBlank
    @RefersTo(type = PhysionomyFlore.class, property = "code")
    public String getPhysionomy() {
        return physionomyProperty.get();
    }

    /**
     * NOM_ESPECES
     * (Source : CCTP)
     *
     * @return
     */
    @RefersTo(type=FloreBassinReference.class, property = "cd_nom")
    @DeterminedFloreSpecies(payload=Warning.class)
    public int getCd_nom() {
        return cd_nomProperty.get();
    }

    /**
     * ABONDANCE.
     *
     * Codes :
     * De + à 5
     *
     * (Source : Boîte à outils complète)
     *
     * D'après le SQL :
     * CASE
     *   WHEN obs_especes.abondance = '+'::text THEN '0.5'::text
     *   WHEN obs_especes.abondance = '1'::text THEN '3'::text
     *   WHEN obs_especes.abondance = '2'::text THEN '15'::text
     *   WHEN obs_especes.abondance = '3'::text THEN '37.5'::text
     *   WHEN obs_especes.abondance = '4'::text THEN '67.5'::text
     *   WHEN obs_especes.abondance = '5'::text THEN '87.5'::text
     *   ELSE '0'::text
     * END
     *
     * TODO: Il y a des valeurs "5+" dans cette table pour abondance or d'après le sql, ça rentrerait dans le cas ELSE '0', je trouve ça bizarre.
     *
     * @return
     */
    @NotBlank
    @RefersTo(type = AbundanceFlore.class, property = "code")
    public String getAbundance() {
        return abundanceProperty.get();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final FloreStatement other = (FloreStatement) obj;

        return
                // Compare first elements which are most likely to differ.
                getCd_nom() == other.getCd_nom() &&
                Objects.equals(getAbundance(), other.getAbundance()) &&
                Objects.equals(getPhysionomy(), other.getPhysionomy()) &&
                Objects.equals(getTrackingPoint(), other.getTrackingPoint()) &&
                Objects.equals(getDate(), other.getDate());
    }

    @Override
    public int hashCode() {
        Integer hash = 7;
        hash = 37 * hash + Objects.hashCode(this.getDate());
        hash = 37 * hash + Objects.hashCode(this.getTrackingPoint());
        hash = 37 * hash + Objects.hashCode(getAbundance());
        hash = 37 * hash + Objects.hashCode(getCd_nom());
        return hash;
    }
}
