package com.thebluealliance.androidclient.renderers;

import com.google.gson.JsonObject;

import com.thebluealliance.androidclient.Constants;
import com.thebluealliance.androidclient.listitems.ImageListElement;
import com.thebluealliance.androidclient.listitems.ListElement;
import com.thebluealliance.androidclient.models.BasicModel;
import com.thebluealliance.androidclient.models.Media;
import com.thebluealliance.androidclient.types.MediaType;
import com.thebluealliance.androidclient.types.ModelType;

import android.support.annotation.Nullable;
import android.util.Log;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class MediaRenderer implements ModelRenderer<Media, Void> {

    @Inject
    public MediaRenderer() {

    }

    @Override
    public @Nullable ListElement renderFromKey(String key, ModelType type, Void args) {
        /* Not implemented yet */
        return null;
    }

    @Override
    public @Nullable ImageListElement renderFromModel(Media media, Void aVoid) {
        String imageUrl;
        try {
            MediaType mediaType = media.getMediaType();
            String foreignKey = media.getForeignKey();

            /* Build the link of the remote image based on foreign key */
            switch (mediaType) {
                case CD_PHOTO_THREAD:
                    JsonObject details = media.getDetails();
                    imageUrl = String.format(mediaType.getImageUrlPattern(),
                            details.get("image_partial").getAsString().replace("_l.jpg", "_m.jpg"));
                    break;
                case YOUTUBE:
                case IMGUR:
                    imageUrl = String.format(mediaType.getImageUrlPattern(), foreignKey);
                    break;
                default:
                    imageUrl = "";
            }
            Boolean isVideo = mediaType == MediaType.YOUTUBE;
            return new ImageListElement(imageUrl,
              String.format(mediaType.getLinkUrlPattern(), foreignKey), isVideo);
        } catch (BasicModel.FieldNotDefinedException e) {
            Log.w(Constants.LOG_TAG, "Required fields not defined for rendering. \n" +
              "Fields Required: Database.Medias.TYPE, Database.Medias.DETAILS, Database.Medias.FOREIGNKEY");
            return null;
        }
    }
}

@@ -570,16 +570,81 @@
 l.Log;%0A%0A
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 import j
@@ -663,16 +663,16 @@
 Inject;%0A
-
 import j
@@ -770,24 +770,118 @@
 a, Void%3E %7B%0A%0A
+    private static final Pattern YOUTUBE_KEY_PATTERN = Pattern.compile(%22%5E(%5Ba-zA-Z0-9_-%5D*)%22);%0A%0A
     @Inject%0A
@@ -1325,16 +1325,59 @@
 gnKey();
+%0A            String keyForUrl = foreignKey;
 %0A%0A      
@@ -1811,16 +1811,645 @@
 OUTUBE:%0A
+                    /* Need to account for timestamps in youtube foreign key%0A                     * Can be like %3Ckey%3E?start=1h15m3s or %3Ckey%3E?t=time or %3Ckey%3E#t=time%0A                     * Since foreign key is first param in yt.com/watch?v=blah, others need to be &%0A                     */%0A                    keyForUrl = foreignKey.replace('?', '&').replace('#','&');%0A                    Matcher m = YOUTUBE_KEY_PATTERN.matcher(foreignKey);%0A                    String cleanKey = m.find() ? m.group(1) : foreignKey;%0A                    imageUrl = String.format(mediaType.getImageUrlPattern(), cleanKey);%0A                    break;%0A
         
@@ -2737,116 +2737,139 @@
     
-return new ImageListElement(imageUrl,%0A              String.format(mediaType.getLinkUrlPattern(), foreignKey)
+String linkUrl = String.format(mediaType.getLinkUrlPattern(), keyForUrl);%0A            return new ImageListElement(imageUrl, linkUrl
 , is

Improve rendering yt videos with timestamps