package com.expidev.gcmapp.model.measurement;

import android.support.annotation.NonNull;

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

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

public class MinistryMeasurement extends Base {
    static final String JSON_VALUE = "local";

    @NonNull
    private final String measurementId;
    @NonNull
    private final String ministryId;
    @NonNull
    private final Ministry.Mcc mcc;
    @NonNull
    private final YearMonth period;
    private int value = 0;

    public MinistryMeasurement(@NonNull final String measurementId, @NonNull final String ministryId,
                               @NonNull final Ministry.Mcc mcc, @NonNull final YearMonth period) {
        this.measurementId = measurementId;
        this.ministryId = ministryId;
        this.mcc = mcc;
        this.period = period;
    }

    @NonNull
    public static MinistryMeasurement fromJson(@NonNull final JSONObject json, @NonNull final String ministryId,
                                               @NonNull final Ministry.Mcc mcc, @NonNull final YearMonth period)
            throws JSONException {
        final MinistryMeasurement measurement =
                new MinistryMeasurement(json.getString(MeasurementType.JSON_MEASUREMENT_ID), ministryId, mcc, period);
        measurement.setValue(json.getInt(JSON_VALUE));
        return measurement;
    }

    @NonNull
    public String getMeasurementId() {
        return measurementId;
    }

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

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

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

    public int getValue() {
        return value;
    }

    public void setValue(final int value) {
        this.value = value;
    }
}

@@ -635,26 +635,23 @@
 String m
-easurement
+inistry
 Id, @Non
@@ -661,33 +661,32 @@
 l final 
-String ministryId
+Ministry.Mcc mcc
 ,%0A      
@@ -725,32 +725,36 @@
 l final 
-Ministry.Mcc mcc
+String measurementId
 , @NonNu
@@ -786,52 +786,8 @@
 ) %7B%0A
-        this.measurementId = measurementId;%0A
     
@@ -840,24 +840,68 @@
 .mcc = mcc;%0A
+        this.measurementId = measurementId;%0A
         this
@@ -1287,16 +1287,33 @@
 urement(
+ministryId, mcc, 
 json.get
@@ -1360,25 +1360,8 @@
 ID),
- ministryId, mcc,
  per

rearrange constructor params to match db primary key