package info.nightscout.utils;

import android.text.format.DateUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;

/**
 * The Class DateUtil. A simple wrapper around SimpleDateFormat to ease the handling of iso date string &lt;-&gt; date obj
 * with TZ
 */
public class DateUtil {

    /**
     * The date format in iso.
     */
    public static String FORMAT_DATE_ISO = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    /**
     * Takes in an ISO date string of the following format:
     * yyyy-mm-ddThh:mm:ss.ms+HoMo
     *
     * @param isoDateString the iso date string
     * @return the date
     * @throws Exception the exception
     */
    public static Date fromISODateString(String isoDateString)
            throws Exception {
        SimpleDateFormat f = new SimpleDateFormat(FORMAT_DATE_ISO);
        f.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = f.parse(isoDateString);
        return date;
    }

    /**
     * Render date
     *
     * @param date   the date obj
     * @param format - if not specified, will use FORMAT_DATE_ISO
     * @param tz     - tz to set to, if not specified uses local timezone
     * @return the iso-formatted date string
     */
    public static String toISOString(Date date, String format, TimeZone tz) {
        if (format == null) format = FORMAT_DATE_ISO;
        if (tz == null) tz = TimeZone.getDefault();
        DateFormat f = new SimpleDateFormat(format);
        f.setTimeZone(tz);
        return f.format(date);
    }

    public static String toISOString(Date date) {
        return toISOString(date, FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC"));
    }
    public static String toISOString(long date) {
        return toISOString(new Date(date), FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC"));
    }

    public static Date toDate(Integer seconds) {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, seconds / 60 / 60);
        String a = calendar.getTime().toString();
        calendar.set(Calendar.MINUTE, (seconds / 60) % 60);
        String b = calendar.getTime().toString();
        calendar.set(Calendar.SECOND, 0);
        String c = calendar.getTime().toString();
        return calendar.getTime();
    }

    public static int toSeconds(String hh_colon_mm) {
        Pattern p = Pattern.compile("(\\d+):(\\d+)");
        Matcher m = p.matcher(hh_colon_mm);
        int retval = 0;

        if (m.find()) {
            retval = SafeParse.stringToInt(m.group(1)) * 60 * 60 + SafeParse.stringToInt(m.group(2)) * 60;
        }
        return retval;
    }

    public static String dateString(Date date) {
        //return DateUtils.formatDateTime(MainApp.instance(), date.getTime(), DateUtils.FORMAT_SHOW_DATE); this provide month name not number
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
        return df.format(date);
    }

    public static String dateString(long mills) {
        //return DateUtils.formatDateTime(MainApp.instance(), mills, DateUtils.FORMAT_SHOW_DATE); this provide month name not number
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
        return df.format(mills);
    }

    public static String timeString(Date date) {
        return DateUtils.formatDateTime(MainApp.instance(), date.getTime(), DateUtils.FORMAT_SHOW_TIME);
    }

    public static String timeString(long mills) {
        return DateUtils.formatDateTime(MainApp.instance(), mills, DateUtils.FORMAT_SHOW_TIME);
    }

    public static String dateAndTimeString(Date date) {
        return dateString(date) + " " + timeString(date);
    }
    public static String dateAndTimeString(long mills) {
        return dateString(mills) + " " + timeString(mills);
    }

    public static String minAgo(long time) {
        int mins = (int) ((System.currentTimeMillis() - time) / 1000 / 60);
        return String.format(MainApp.sResources.getString(R.string.minago), mins);
    }

}
@@ -85,32 +85,65 @@
 ext.DateFormat;%0A
+import java.text.ParseException;%0A
 import java.text
@@ -153,32 +153,32 @@
 mpleDateFormat;%0A
-
 import java.util
@@ -704,16 +704,96 @@
 :ss'Z'%22;
+%0A    public static String FORMAT_DATE_ISO_MSEC = %22yyyy-MM-dd'T'HH:mm:ss.SSS'Z'%22;
 %0A%0A    /*
@@ -1177,16 +1177,35 @@
 E_ISO);%0A
+        Date date;%0A
         
@@ -1252,28 +1252,237 @@
 ));%0A        
-Date
+try %7B%0A            date = f.parse(isoDateString);%0A        %7D catch (ParseException e) %7B%0A            f = new SimpleDateFormat(FORMAT_DATE_ISO_MSEC);%0A            f.setTimeZone(TimeZone.getTimeZone(%22UTC%22));%0A           
  date = f.pa
@@ -1493,32 +1493,42 @@
 isoDateString);%0A
+        %7D%0A
         return d

allow ISO format with miliseconds