package com.ruigoncalo.marvin.ui;

import android.content.Context;
import android.widget.ImageView;

import com.ruigoncalo.marvin.model.raw.Thumbnail;
import com.squareup.picasso.Picasso;

/**
 * ImageLoaderManager that wraps up an image loader lib
 *
 * Currently using picasso
 *
 * Created by ruigoncalo on 20/04/16.
 */
public class ImageLoaderManager {

    public static final int IMAGE_STANDARD = 0;
    public static final int IMAGE_LANDSCAPE = 1;
    public static final int IMAGE_PORTRAIT = 2;

    private static final String IMAGE_FORMAT_STANDARD = "standard_fantastic";
    private static final String IMAGE_FORMAT_LANDSCAPE = "landscape_amazing";
    private static final String IMAGE_FORMAT_PORTRAIT = "portrait_fantastic";


    private ImageLoaderManager(){

    }

    public static void load(Context context, String url, ImageView target) {
        Picasso.with(context).load(url).into(target);
    }

    public static void fetch(Context context, String url){
        Picasso.with(context).load(url).fetch();
    }

    /**
     * Build image url path
     * Font: http://developer.marvel.com/documentation/images
     *
     * @param thumbnail image url info
     * @return full url to download image
     */
    public static String buildImageUrl(Thumbnail thumbnail, int imageFormat){
        String path = thumbnail.getPath();
        String extension = thumbnail.getExtension();
        String imageSize = getImageSize(imageFormat);

        return path + "/" + imageSize + "." + extension;
    }


    private static String getImageSize(int formart){
        String result;
        switch (formart){
            case IMAGE_LANDSCAPE:
                result = IMAGE_FORMAT_LANDSCAPE;
                break;

            case IMAGE_PORTRAIT:
                result = IMAGE_FORMAT_PORTRAIT;
                break;

            default: // standard
                result = IMAGE_FORMAT_STANDARD;
        }

        return result;
    }
}

@@ -60,16 +60,84 @@
 ontext;%0A
+import android.net.Uri;%0Aimport android.support.annotation.Nullable;%0A
 import a
@@ -567,30 +567,29 @@
  = 2;%0A%0A    p
-rivate
+ublic
  static fina
@@ -640,38 +640,37 @@
 antastic%22;%0A    p
-rivate
+ublic
  static final St
@@ -721,30 +721,106 @@
 zing%22;%0A    p
-rivate
+ublic static final String IMAGE_FORMAT_PORTRAIT = %22portrait_fantastic%22;%0A    public
  static fina
@@ -841,32 +841,37 @@
 _FORMAT_PORTRAIT
+_BEST
  = %22portrait_fan
@@ -867,28 +867,203 @@
 ortrait_
-fantastic%22;%0A
+incredible%22;%0A%0A    private static final String PLACEHOLDER_URL = %22http://i.annihil.us/u/prod/marvel/i/mg/b/40/image_not_available%22;%0A    private static final String PLACEHOLDER_EXT = %22jpg%22;
 %0A%0A    pr
@@ -1584,16 +1584,26 @@
 mageUrl(
+@Nullable 
 Thumbnai
@@ -1668,46 +1668,122 @@
 nail
-.getPath();%0A        String extension =
+ == null ? PLACEHOLDER_URL : thumbnail.getPath();%0A        String extension =  thumbnail == null? PLACEHOLDER_EXT :
  thu
@@ -2360,10 +2360,255 @@
 ;%0A    %7D%0A
+%0A%0A    public static String updateUrlImageRatio(String url, String newRatio)%7B%0A        Uri uri = Uri.parse(url);%0A        String imageRatio = uri.getLastPathSegment();%0A        return url.replace(imageRatio, newRatio + %22.%22 + PLACEHOLDER_EXT);%0A    %7D%0A
 %7D%0A

Add util methods to image loader