package com.expidev.gcmapp.model.measurement;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.expidev.gcmapp.model.Base;
import com.expidev.gcmapp.model.Ministry;

import org.joda.time.YearMonth;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Measurement extends Base implements Serializable
{
    private static final long serialVersionUID = 0L;

    @Nullable
    private MeasurementType type;
    @Nullable
    private MinistryMeasurement ministryMeasurement;
    @Nullable
    private PersonalMeasurement personalMeasurement;
    @NonNull
    private YearMonth period = YearMonth.now();
    @NonNull
    private String ministryId = Ministry.INVALID_ID;
    @NonNull
    private Ministry.Mcc mcc = Ministry.Mcc.UNKNOWN;

    @NonNull
    public static List<Measurement> listFromJson(@NonNull final JSONArray json, @NonNull final String guid,
                                                 @NonNull final String ministryId, @NonNull final Ministry.Mcc mcc,
                                                 @NonNull final YearMonth period) throws JSONException {
        final List<Measurement> measurements = new ArrayList<>();
        for (int i = 0; i < json.length(); i++) {
            measurements.add(Measurement.fromJson(json.getJSONObject(i), guid, ministryId, mcc, period));
        }
        return measurements;
    }

    @NonNull
    public static Measurement fromJson(@NonNull final JSONObject json, @NonNull final String guid,
                                       @NonNull final String ministryId, @NonNull final Ministry.Mcc mcc,
                                       @NonNull final YearMonth period) throws JSONException {
        final Measurement measurement = new Measurement();
        measurement.ministryId = ministryId;
        measurement.mcc = mcc;
        measurement.period = period;

        measurement.type = MeasurementType.fromJson(json);
        if (json.has(MinistryMeasurement.JSON_VALUE)) {
            measurement.ministryMeasurement = MinistryMeasurement.fromJson(json, ministryId, mcc, period);
        }
        if (json.has(PersonalMeasurement.JSON_VALUE)) {
            measurement.personalMeasurement = PersonalMeasurement.fromJson(json, guid, ministryId, mcc, period);
        }

        return measurement;
    }

    @Nullable
    public MeasurementType getType() {
        return type;
    }

    public void setType(@Nullable final MeasurementType type) {
        this.type = type;
    }

    @Nullable
    public MinistryMeasurement getMinistryMeasurement() {
        return ministryMeasurement;
    }

    public void setMinistryMeasurement(@Nullable final MinistryMeasurement ministryMeasurement) {
        this.ministryMeasurement = ministryMeasurement;
    }

    @Nullable
    public PersonalMeasurement getPersonalMeasurement() {
        return personalMeasurement;
    }

    public void setPersonalMeasurement(@Nullable final PersonalMeasurement personalMeasurement) {
        this.personalMeasurement = personalMeasurement;
    }

    @NonNull
    public YearMonth getPeriod() {
        return period;
    }

    public void setPeriod(@NonNull final YearMonth period) {
        this.period = period;
    }

    @NonNull
    public String getMinistryId()
    {
        return ministryId;
    }

    public void setMinistryId(@NonNull final String ministryId) {
        this.ministryId = ministryId;
    }

    @NonNull
    public Ministry.Mcc getMcc() {
        return this.mcc;
    }

    public void setMcc(@Nullable final String mcc) {
        setMcc(Ministry.Mcc.fromRaw(mcc));
    }

    public void setMcc(@NonNull final Ministry.Mcc mcc) {
        this.mcc = mcc;
    }
}

@@ -332,37 +332,8 @@
 t;%0A%0A
-import java.io.Serializable;%0A
 impo
@@ -422,87 +422,9 @@
 ase 
-implements Serializable%0A%7B%0A    private static final long serialVersionUID = 0L;%0A
+%7B
 %0A   
@@ -605,201 +605,8 @@
 ent;
-%0A    @NonNull%0A    private YearMonth period = YearMonth.now();%0A    @NonNull%0A    private String ministryId = Ministry.INVALID_ID;%0A    @NonNull%0A    private Ministry.Mcc mcc = Ministry.Mcc.UNKNOWN;
 %0A%0A  
@@ -1588,121 +1588,8 @@
 t();
-%0A        measurement.ministryId = ministryId;%0A        measurement.mcc = mcc;%0A        measurement.period = period;
 %0A%0A  
@@ -2118,105 +2118,8 @@
  %7D%0A%0A
-    public void setType(@Nullable final MeasurementType type) %7B%0A        this.type = type;%0A    %7D%0A%0A
     
@@ -2237,1078 +2237,110 @@
     
-public void setMinistryMeasurement(@Nullable final MinistryMeasurement ministryMeasurement) %7B%0A        this.ministryMeasurement = ministryMeasurement;%0A    %7D%0A%0A    @Nullable%0A    public PersonalMeasurement getPersonalMeasurement() %7B%0A        return personalMeasurement;%0A    %7D%0A%0A    public void setPersonalMeasurement(@Nullable final PersonalMeasurement personalMeasurement) %7B%0A        this.personalMeasurement = personalMeasurement;%0A    %7D%0A%0A    @NonNull%0A    public YearMonth getPeriod() %7B%0A        return period;%0A    %7D%0A%0A    public void setPeriod(@NonNull final YearMonth period) %7B%0A        this.period = period;%0A    %7D%0A%0A    @NonNull%0A    public String getMinistryId()%0A    %7B%0A        return ministryId;%0A    %7D%0A%0A    public void setMinistryId(@NonNull final String ministryId) %7B%0A        this.ministryId = ministryId;%0A    %7D%0A%0A    @NonNull%0A    public Ministry.Mcc getMcc() %7B%0A        return this.mcc;%0A    %7D%0A%0A    public void setMcc(@Nullable final String mcc) %7B%0A        setMcc(Ministry.Mcc.fromRaw(mcc));%0A    %7D%0A%0A    public void setMcc(@NonNull final Ministry.Mcc mcc) %7B%0A        this.mcc = mcc
+@Nullable%0A    public PersonalMeasurement getPersonalMeasurement() %7B%0A        return personalMeasurement
 ;%0A  

simplify measurement object which is only used for parsing a server response