package com.instructure.canvasapi.utilities;

import android.content.Context;
import android.text.format.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Copyright (c) 2014 Instructure. All rights reserved.
 */
public class DateHelpers {

    /**
     * Date Format Standards for Android
     * SHORT: 12/31/2000 or 1/3/2000
     * MEDIUM: Jan 3, 2000
     * LONG: Monday, January 3, 2000
     */

    public static Format getPreferredDateFormat(Context context) {
        if(context == null) {
            return null;
        }
        return android.text.format.DateFormat.getMediumDateFormat(context);
    }

    public static Format getShortDateFormat(Context context){
        if(context == null) {
            return null;
        }
        return android.text.format.DateFormat.getDateFormat(context);
    }

    public static String getShortDate(Context context, Date date){
        if(context == null) {
            return null;
        }
        Format format = getShortDateFormat(context);
        String sFormat = format.format(date.getTime());
        return sFormat;
    }

    public static String getFormattedDate(Context context, Date date) {
        if(context == null) {
            return null;
        }
        Format format = getPreferredDateFormat(context);
        return format.format(date.getTime());
    }

    /**
     * @param context
     * @param date
     * @return first 3 letters of month with day of the month if >24hr ago, else time of day
     */
    public static String getDayMonthDateString(Context context, Date date) {
        if(context == null) {
            return null;
        }
        Format format = getDayMonthDateFormat(context);
        return format.format(date.getTime());
    }

    /**
     * @param context
     * @param date
     * @return abbreviated day format and time, with an '@' symbol if >24hr ago, otherwise time of day
     *         example: "Mon @ 3:12pm"
     */
    public static String getDayHourDateString(Context context, Date date) {
        if(context == null) {
            return null;
        }
        Format format = getDayAbbreviationFormat(context);
        return format.format(date.getTime());
    }

    public static SimpleDateFormat getPreferredTimeFormat(Context context) {
        if(DateFormat.is24HourFormat(context)) {
            return new SimpleDateFormat("HH:mm", Locale.getDefault());
        }
        return new SimpleDateFormat("hh:mm a", Locale.getDefault());
    }

    public static SimpleDateFormat getDayMonthDateFormat(Context context) {
        if(DateFormat.is24HourFormat(context)) {
            return new SimpleDateFormat("HH:mm", Locale.getDefault());
        }
        return new SimpleDateFormat("MMM d", Locale.getDefault());
    }

    public static SimpleDateFormat getDayAbbreviationFormat(Context context) {
        if(DateFormat.is24HourFormat(context)) {
            return new SimpleDateFormat("HH:mm", Locale.getDefault());
        }
        return new SimpleDateFormat("EEE '@' h:mma", Locale.getDefault());
    }

    public static String getFormattedTime(Context context, Date date) {
        if(context == null) {
            return null;
        }
        return DateHelpers.getPreferredTimeFormat(context).format(date);
    }

    public static String createPrefixedDateString(Context context, String prefix, Date date) {
        if(context == null) {
            return null;
        }
        return prefix + ": " + getFormattedDate(context, date);
    }

    public static String createPrefixedDateString(Context context, int prefixResId,Date date) {
        if(context == null) {
            return null;
        }
        return createPrefixedDateString(context, context.getResources().getString(prefixResId), date);
    }

    public static String createPrefixedTimeString(Context context, String prefix, Date date) {
        if(context == null) {
            return null;
        }
        return prefix + ": " + getFormattedTime(context, date);
    }

    public static String createPrefixedTimeString(Context context, int prefixResId, Date date) {
        if(context == null) {
            return null;
        }
        return createPrefixedTimeString(context, context.getResources().getString(prefixResId), date);
    }

    public static String createPrefixedDateTimeString(Context context, String prefix, Date date) {
        if(context == null) {
            return null;
        }
        return prefix + ": " + DateHelpers.getFormattedDate(context, date) + " " + getFormattedTime(context, date);
    }

    public static String createPrefixedDateTimeString(Context context, int prefixResId, Date date) {
        if(context == null) {
            return null;
        }
        return createPrefixedDateTimeString(context, context.getResources().getString(prefixResId), date);
    }

    public static String getDateTimeString(Context context, Date date) {
        if(context == null) {
            return null;
        }
        return getFormattedDate(context, date) + " " + getFormattedTime(context, date);
    }
}

@@ -102,32 +102,71 @@
 mat.DateFormat;%0A
+import android.text.format.DateUtils;%0A%0A
 import java.text
@@ -166,32 +166,32 @@
 va.text.Format;%0A
-
 import java.text
@@ -201,32 +201,59 @@
 mpleDateFormat;%0A
+import java.util.Calendar;%0A
 import java.util
@@ -1867,32 +1867,473 @@
 Time());%0A    %7D%0A%0A
+    public static String getMessageDateString(Context context, Date date) %7B%0A        if(context == null) %7B%0A            return null;%0A        %7D%0A        if (!DateUtils.isToday(date.getTime())) %7B%0A            return DateUtils.getRelativeTimeSpanString(context, date.getTime()).toString() + %22, %22 + getPreferredTimeFormat(context).format(date);%0A        %7D%0A        return DateUtils.getRelativeTimeSpanString(context, date.getTime()).toString();%0A    %7D%0A
     /**%0A     * @
@@ -3013,17 +3013,16 @@
 ormat(%22h
-h
 :mm a%22, 
@@ -3579,20 +3579,317 @@
 mat(%22EEE
- '@'
+, h:mma%22, Locale.getDefault());%0A    %7D%0A%0A    public static SimpleDateFormat getDayMonthTimeAbbreviationFormat(Context context) %7B%0A        if(DateFormat.is24HourFormat(context)) %7B%0A            return new SimpleDateFormat(%22HH:mm%22, Locale.getDefault());%0A        %7D%0A        return new SimpleDateFormat(%22MMM dd,
  h:mma%22,

add message date format