package com.todoist.mediaparser.mediaparser;

import com.todoist.mediaparser.util.MediaType;
import com.todoist.mediaparser.util.Size;

import java.util.regex.Pattern;

/*
 * See: http://yfrog.com/page/api#a5
 */
public class YfrogParser extends BaseMediaParserWithId {
    private static Pattern sIdPattern;
	private static Size[] sSizes;

    YfrogParser(String url) {
        super(url);
    }

	@Override
	public boolean isThumbnailImmediate(int smallestSide) {
		return true; // Always immediate.
	}

	@Override
	public MediaType getContentMediaType() {
		return MediaType.OTHER; // Can be IMAGE or VIDEO.
	}

	@Override
	protected String createContentUrl() {
		return mUrl;
	}

    @Override
    protected String createThumbnailUrl(int smallestSide) {
        Size size = null;

	    Size[] availableSizes = getAvailableSizes();
        for(Size availableSize : availableSizes) {
            if(availableSize.smallestSide >= smallestSide) {
                size = availableSize;
                break;
            }
        }
        if(size == null)
            size = availableSizes[availableSizes.length - 1];

        return String.format("http://yfrog.com/%1$s:%2$s", mId, size.key);
    }

    @Override
    protected Pattern getIdPattern() {
        if(sIdPattern == null) {
            sIdPattern = Pattern.compile(
                    "https?://(?:www\\.)?(?:\\w+\\.)?yfrog\\.com/(\\w+)/?",
                    Pattern.CASE_INSENSITIVE
            );
        }
        return sIdPattern;
    }

	private Size[] getAvailableSizes() {
		if(sSizes == null) {
			sSizes = new Size[]{
					new Size("small", 100),
					new Size("iphone", 480)
			};
		}
		return sSizes;
	}
}

@@ -129,16 +129,96 @@
 .Size;%0A%0A
+import java.io.IOException;%0Aimport java.net.URL;%0Aimport java.net.URLConnection;%0A
 import j
@@ -406,16 +406,57 @@
 Size%5B%5D s
+PhotoSizes;%0A%09private static Size%5B%5D sVideo
 Sizes;%0A%0A
@@ -516,116 +516,8 @@
  %7D%0A%0A
-%09@Override%0A%09public boolean isThumbnailImmediate(int smallestSide) %7B%0A%09%09return true; // Always immediate.%0A%09%7D%0A%0A
 %09@Ov
@@ -791,16 +791,362 @@
  null;%0A%0A
+%09    boolean isImage = true;%0A%09    try %7B%0A%09%09    URLConnection connection = new URL(String.format(getUrlTemplate(), mId, %22iphone%22)).openConnection();%0A%09%09    String contentType = connection.getHeaderField(%22Content-Type%22);%0A%09%09    isImage = contentType == null %7C%7C contentType.contains(%22image/%22);%0A%09    %7D catch(IOException e) %7B%0A%09%09    /* Ignore. */%0A%09    %7D%0A%0A
 %09    Siz
@@ -1165,16 +1165,26 @@
 eSizes =
+ isImage ?
  getAvai
@@ -1184,24 +1184,56 @@
 getAvailable
+ImageSizes() : getAvailableVideo
 Sizes();%0A   
@@ -1229,20 +1229,17 @@
 izes();%0A
-    
+%09
     for(
@@ -1544,36 +1544,24 @@
 mat(
-%22http://yfrog.com/%251$s:%252$s%22
+getUrlTemplate()
 , mI
@@ -1889,16 +1889,94 @@
 %0A    %7D%0A%0A
+%09private String getUrlTemplate() %7B%0A%09%09return %22http://yfrog.com/%251$s:%252$s%22;%0A%09%7D%0A%0A
 %09private
@@ -1991,24 +1991,29 @@
 getAvailable
+Image
 Sizes() %7B%0A%09%09
@@ -2016,16 +2016,21 @@
 %7B%0A%09%09if(s
+Photo
 Sizes ==
@@ -2042,16 +2042,21 @@
 ) %7B%0A%09%09%09s
+Photo
 Sizes = 
@@ -2115,19 +2115,212 @@
 ze(%22
-iphone%22, 48
+medium%22, 640)%0A%09%09%09%7D;%0A%09%09%7D%0A%09%09return sPhotoSizes;%0A%09%7D%0A%0A%09private Size%5B%5D getAvailableVideoSizes() %7B%0A%09%09if(sVideoSizes == null) %7B%0A%09%09%09sVideoSizes = new Size%5B%5D%7B%0A%09%09%09%09%09new Size(%22small%22, 100),%0A%09%09%09%09%09new Size(%22frame%22, 64
 0)%0A%09
@@ -2338,16 +2338,21 @@
 return s
+Video
 Sizes;%0A%09

Fix yfrog.com video link parsing