package com.alwaysallthetime.adnlibutils;

import android.util.Log;

import com.alwaysallthetime.adnlib.Annotations;
import com.alwaysallthetime.adnlib.data.Annotation;
import com.alwaysallthetime.adnlib.data.Message;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by brambley on 10/9/13.
 */
public class AnnotationUtility {
    private static final String TAG = "ADNLibUtils_AnnotationUtility";

    public static final String OEMBED_TYPE_PHOTO = "photo";
    public static final String OEMBED_TYPE_HTML5VIDEO = "html5video";

    private static SimpleDateFormat mIso8601Format;

    public static Date getJournalEntryDate(Message message) {
        Annotation displayDate = message.getFirstAnnotationOfType(Annotations.OHAI_DISPLAY_DATE);
        if(displayDate != null) {
            String date = (String) displayDate.getValue().get("date");
            if(date != null) {
                return getDateFromIso8601String(date);
            }
        }
        return message.getCreatedAt();
    }

    public static Date getDateFromIso8601String(String date) {
        if(mIso8601Format == null) {
            mIso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        }
        try {
            return mIso8601Format.parse(date);
        } catch(ParseException e) {
            Log.d(TAG, e.getMessage(), e);
        }
        return null;
    }

    public static Annotation getFirstOEmbedPhotoAnnotation(Message message) {
        Annotation oEmbedAnnotation = message.getFirstAnnotationOfType(Annotations.OEMBED);
        if(oEmbedAnnotation != null && OEMBED_TYPE_PHOTO.equals(oEmbedAnnotation.getValue().get("type"))) {
            return oEmbedAnnotation;
        }
        return null;
    }

    public static String getFirstOEmbedPhotoUrl(Message message) {
        Annotation oEmbedAnnotation = getFirstOEmbedPhotoAnnotation(message);
        if(oEmbedAnnotation != null) {
            return (String) oEmbedAnnotation.getValue().get("url");
        }
        return null;
    }

    public static String getFirstHTML5VideoSource(Message message) {
        Annotation oEmbedAnnotation = message.getFirstAnnotationOfType(Annotations.OEMBED);
        if(OEMBED_TYPE_HTML5VIDEO.equals(oEmbedAnnotation.getValue().get("type"))) {
            List<Map<String, String>> sources = (List<Map<String, String>>) oEmbedAnnotation.getValue().get("sources");
            if(sources != null && sources.size() > 0) {
                Map<String, String> firstSource = sources.get(0);
                String url = firstSource.get("url");
                if(url != null) {
                    return url;
                }
            }
        }
        return null;
    }
}

@@ -303,16 +303,42 @@
 l.Date;%0A
+import java.util.HashMap;%0A
 import j
@@ -352,16 +352,16 @@
 l.List;%0A
-
 import j
@@ -1204,124 +1204,23 @@
    i
-f(mIso8601Format == null) %7B%0A            mIso8601Format = new SimpleDateFormat(%22yyyy-MM-dd'T'HH:mm:ss'Z'%22);%0A        %7D
+nitFormatter();
 %0A   
@@ -1386,32 +1386,551 @@
 rn null;%0A    %7D%0A%0A
+    public static String getIso8601StringfromDate(Date date) %7B%0A        initFormatter();%0A        return mIso8601Format.format(date);%0A    %7D%0A%0A    public static Annotation newDisplayDateAnnotation(Date date) %7B%0A        Annotation displayDateAnnotation = new Annotation(Annotations.OHAI_DISPLAY_DATE);%0A        HashMap%3CString, Object%3E value = new HashMap%3CString, Object%3E(1);%0A        value.put(%22date%22, getIso8601StringfromDate(date));%0A        displayDateAnnotation.setValue(value);%0A        return displayDateAnnotation;%0A    %7D%0A%0A
     public stati
@@ -3197,32 +3197,32 @@
     %7D%0A        %7D%0A
-
         return n
@@ -3231,11 +3231,186 @@
 l;%0A    %7D
+%0A%0A    private static void initFormatter() %7B%0A        if(mIso8601Format == null) %7B%0A            mIso8601Format = new SimpleDateFormat(%22yyyy-MM-dd'T'HH:mm:ss'Z'%22);%0A        %7D%0A    %7D
 %0A%7D%0A

add utility methods for getting a date string, display date annotation