package models;

import java.io.IOException;

import org.joda.time.LocalDate;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

public class Ressource {

    public RessourceType type;
    public int montant;
    public RessourcePeriode periode;

    @JsonDeserialize(using=RessourceTypeDeserializer.class)
    public static enum RessourceType {

        REVENUS_SALARIES("revenusSalarie"),
        REVENUS_STAGE("stage"),
        REVENUS_STAGE_FORMATION_PRO("revenusStageFormationPro"),
        ALLOCATIONS_CHOMAGE("allocationsChomage"),
        ALLOCATION_SECURISATION_PRO("allocationSecurisationPro"),
        PRIME_REPRISE_ACTIVITE("primeRepriseActivite"),
        ALLOCATION_LOGEMENT("allocationLogement"),
        RSA("rsa"),
        ASF("asf"),
        ASPA("aspa"),
        ASS("ass"),
        AAH("aah"),
        INDEMNITES_MATERNITE("indJourMaternite"),
        INDEMNITES_MALADIE("indJourMaladie"),
        INDEMNITES_MALADIE_PRO("indJourMaladieProf"),
        INDEMNITES_ACCIDENT_TRAVAIL("indJourAccidentDuTravail"),
        INDEMNITES_CHOMAGE_PARTIEL("indChomagePartiel"),
        INDEMNITES_VOLONTARIAT("indVolontariat"),
        DEDOMMAGEMENT_AMIANTE("dedommagementAmiante"),
        PENSIONS_ALIMENTAIRES("pensionsAlimentaires"),
        PRESTATION_COMPENSATOIRE("prestationCompensatoire"),
        RETRAITES_RENTES("pensionsRetraitesRentes"),
        RETRAITE_COMBATTANT("retraiteCombattant"),
        PENSION_INVALIDITE("pensionsInvalidite"),
        BOURSE_ENSEIGNEMENT_SUP("bourseEnseignementSup"),
        BOURSE_RECHERCHE("bourseRecherche"),
        GAINS_EXCEPTIONNELS("gainsExceptionnels"),
        CA_MICRO_ENTREPRISE("caMicroEntreprise"),
        AUTRES_TNS("autresRevenusTns");

        public final String jsonValue;

        RessourceType(String jsonValue) {
            this.jsonValue = jsonValue;
        }
    }

    public static class RessourceTypeDeserializer extends JsonDeserializer<RessourceType> {

        @Override
        public RessourceType deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            for (RessourceType type : RessourceType.values()) {
                if (type.jsonValue.equals(jp.getText())) {
                    return type;
                }
            }

            throw new RuntimeException(String.format("Type de ressource inconnu : %s", jp.getText()));
        }
    }

    @JsonDeserialize(using=RessourcePeriodeDeserializer.class)
    public static enum RessourcePeriode {

        MOIS_1(3),
        MOIS_2(2),
        MOIS_3(1);

        public final int minusCurrentMonth;

        RessourcePeriode(int minusCurrentMonth) {
            this.minusCurrentMonth = minusCurrentMonth;
        }
    }

    public static class RessourcePeriodeDeserializer extends JsonDeserializer<RessourcePeriode> {

        @Override
        public RessourcePeriode deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            for (RessourcePeriode periode : RessourcePeriode.values()) {
                String period = LocalDate.now().withDayOfMonth(1).minusMonths(periode.minusCurrentMonth).toString("YYYY-MM");
                if (period.equals(jp.getText())) {
                    return periode;
                }
            }

            throw new RuntimeException(String.format("Période invalide : %s", jp.getText()));
        }
    }
}

@@ -2791,16 +2791,62 @@
 OIS_3(1)
+,%0A        ANNEE_MOINS_1,%0A        ANNEE_MOINS_2
 ;%0A%0A     
@@ -2992,32 +2992,113 @@
 Month;%0A        %7D
+%0A%0A        RessourcePeriode() %7B%0A            this.minusCurrentMonth = -1;%0A        %7D
 %0A    %7D%0A%0A    publ
@@ -3200,290 +3200,885 @@
     
-@Override%0A        public RessourcePeriode deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException %7B%0A            for (RessourcePeriode periode : RessourcePeriode.values()) %7B%0A                String period = LocalDate.now().withDayOfMonth(1)
+private static String anneeMoins1 = String.valueOf(LocalDate.now().getYear());%0A        private static String anneeMoins2 = String.valueOf(LocalDate.now().minusYears(2).getYear());%0A%0A        @Override%0A        public RessourcePeriode deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException %7B%0A            String text = jp.getText();%0A            if (anneeMoins1.equals(text)) %7B%0A                return RessourcePeriode.ANNEE_MOINS_1;%0A            %7D else if (anneeMoins2.equals(text)) %7B%0A                return RessourcePeriode.ANNEE_MOINS_2;%0A            %7D%0A%0A            for (RessourcePeriode periode : RessourcePeriode.values()) %7B%0A                if (periode.minusCurrentMonth != -1) %7B%0A                    String period = LocalDate%0A                            .now()%0A                            .withDayOfMonth(1)%0A                            
 .min
@@ -4112,16 +4112,45 @@
 ntMonth)
+%0A                            
 .toStrin
@@ -4171,32 +4171,36 @@
                 
+    
 if (period.equal
@@ -4201,33 +4201,29 @@
 .equals(
-jp.getT
+t
 ext
-(
 ))
-)
  %7B%0A
+    
         
@@ -4250,16 +4250,38 @@
 eriode;%0A
+                    %7D%0A
         
@@ -4379,36 +4379,28 @@
 lide : %25s%22, 
-jp.getT
+t
 ext
-()
 ));%0A        

fix déserialisation des périodes de ressources