package org.commcare.android.view.c3;

import org.commcare.android.util.InvalidStateException;
import org.commcare.suite.model.graph.Graph;
import org.commcare.suite.model.graph.GraphData;
import org.javarosa.core.model.utils.DateUtils;
import org.javarosa.core.util.OrderedHashtable;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Date;

/**
 * Base class for helper classes that build C3 graph configuration.
 * This class itself is not meant to be instantiated. For subclasses,
 * the bulk of the work is done in the constructor. The instantiator
 * can then call getConfiguration and getVariables to get at the JSON
 * configuration and any JavaScript variables that configuration depends on.
 *
 * Created by jschweers on 11/16/2015.
 */
public class Configuration {
    private final Calendar mCalendar = Calendar.getInstance();

    protected GraphData mData;
    protected JSONObject mConfiguration;
    protected OrderedHashtable<String, String> mVariables;

    public Configuration(GraphData data) {
        mData = data;
        mConfiguration = new JSONObject();
        mVariables = new OrderedHashtable<>();
    }

    public JSONObject getConfiguration() {
        return mConfiguration;
    }

    public OrderedHashtable<String, String> getVariables() {
        return mVariables;
    }

    /**
     * Parse given double time value into string acceptable to C3.
     *
     * @param days The time, measured in days since the epoch.
     */
    protected String convertTime(double days) {
        mCalendar.setTimeInMillis((long)(days * 24 * 60 * 60 * 1000));
        String time = mCalendar.get(Calendar.YEAR) + "-" + mCalendar.get(Calendar.MONTH)
                + "-" + mCalendar.get(Calendar.DAY_OF_MONTH) + " "
                + mCalendar.get(Calendar.HOUR_OF_DAY) + ":"
                + mCalendar.get(Calendar.MINUTE) + ":"
                + mCalendar.get(Calendar.SECOND);
        return time;
    }

    /**
     * Attempt to parse a double, but fail on NumberFormatException.
     *
     * @param description Something to identify the kind of value, used to augment any error message.
     */
    protected double parseDouble(String value, String description) throws InvalidStateException {
        try {
            Double numeric = Double.valueOf(value);
            if (numeric.isNaN()) {
                throw new InvalidStateException("Could not understand '" + value + "' in " + description);
            }
            return numeric;
        } catch (NumberFormatException nfe) {
            throw new InvalidStateException("Could not understand '" + value + "' in " + description);
        }
    }
}

@@ -138,107 +138,12 @@
 raph
-;%0D%0Aimport org.commcare.suite.model.graph.GraphData;%0D%0Aimport org.javarosa.core.model.utils.DateUtils
+Data
 ;%0D%0Ai
@@ -237,21 +237,29 @@
 ava.
-util.Calendar
+text.SimpleDateFormat
 ;%0D%0Ai
@@ -748,50 +748,81 @@
 nal 
-Calendar mCalendar = Calendar.getInstance(
+SimpleDateFormat mDateFormat = new SimpleDateFormat(%22yyyy-MM-dd HH:mm:ss%22
 );%0D%0A
@@ -1530,33 +1530,25 @@
     
-mCalendar.setTimeInMillis
+Date d = new Date
 ((lo
@@ -1594,345 +1594,36 @@
     
-String time = mCalendar.get(Calendar.YEAR) + %22-%22 + mCalendar.get(Calendar.MONTH)%0D%0A                + %22-%22 + mCalendar.get(Calendar.DAY_OF_MONTH) + %22 %22%0D%0A                + mCalendar.get(Calendar.HOUR_OF_DAY) + %22:%22%0D%0A                + mCalendar.get(Calendar.MINUTE) + %22:%22%0D%0A                + mCalendar.get(Calendar.SECOND);%0D%0A        return time
+return mDateFormat.format(d)
 ;%0D%0A 

Fix OB1 month error