package com.loosli.christian.popularmovieapp.android.app.util;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * Created by ChristianL on 30.11.15.
 */
public class Util {

    private static final Uri TMDB_IMAGE_BASE_URI = Uri.parse("http://image.tmdb.org/t/p/");

    private interface TMDbImageWidth {
        String getWidthString();

        int getMaxWidth();
    }

    public enum TMDbPosterWidth implements TMDbImageWidth {
        W92(92), W154(154), W185(185), W342(342), W500(500), W780(780), ORIGINAL(Integer.MAX_VALUE);

        public final int maxWidth;

        TMDbPosterWidth(int maxWidth) {
            this.maxWidth = maxWidth;
        }

        public int getMaxWidth() {
            return this.maxWidth;
        }

        public String getWidthString() {
            return (this == ORIGINAL) ? "original" : "w" + this.maxWidth;
        }
    }

    public enum TMDbBackdropWidth implements TMDbImageWidth {
        W300(300), W780(780), W1280(1280), ORIGINAL(Integer.MAX_VALUE);

        public final int maxWidth;

        TMDbBackdropWidth(int maxWidth) {
            this.maxWidth = maxWidth;
        }

        public int getMaxWidth() {
            return this.maxWidth;
        }

        public String getWidthString() {
            return (this == ORIGINAL) ? "original" : "w" + this.maxWidth;
        }
    }

    public static String buildPosterUrl(String posterPath, int posterWidth) {
        return buildImageUrl(posterPath, computeNextLowestPosterWidth(posterWidth));
    }

    public static String buildPosterUrl(String posterPath, TMDbPosterWidth tmdbPosterWidth) {
        return buildImageUrl(posterPath, tmdbPosterWidth);
    }

    public static String buildBackdropUrl(String backdropPath, int backdropWidth) {
        return buildImageUrl(backdropPath, computeNextLowestBackdropWidth(backdropWidth));
    }

    public static String buildBackdropUrl(String backdropPath, TMDbBackdropWidth tmdbBackdropWidth) {
        return buildImageUrl(backdropPath, tmdbBackdropWidth);
    }

    private static <T extends TMDbImageWidth> String buildImageUrl(String imagePath, T tmdbImageWidth) {
//        if (BuildConfig.DEBUG) {
//            Log.d("Picasso", "Loading image of width " + tmdbImageWidth.getMaxWidth() + "px");
//        }
        String relativePath = tmdbImageWidth.getWidthString() + "/" + imagePath;
        return Uri.withAppendedPath(TMDB_IMAGE_BASE_URI, relativePath).toString();
    }

    public static int getScreenWidth(@NonNull Context context) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }

    public static int multiplyColor(int srcColor, float factor) {
        int alpha = Color.alpha(srcColor);
        int red = (int) (Color.red(srcColor) * factor);
        int green = (int) (Color.green(srcColor) * factor);
        int blue = (int) (Color.blue(srcColor) * factor);
        return Color.argb(alpha, red, green, blue);
    }


    // private methods

    // 50 => W92, 92 => W92, 93 => W185, 999 => ORIGINAL
    private static TMDbPosterWidth computeNextLowestPosterWidth(int posterWidth) {
        for (TMDbPosterWidth enumWidth : TMDbPosterWidth.values()) {
            if (0.8 * posterWidth <= enumWidth.maxWidth) {
                return enumWidth;
            }
        }
        return TMDbPosterWidth.ORIGINAL;
    }

    private static TMDbBackdropWidth computeNextLowestBackdropWidth(int backdropWidth) {
        for (TMDbBackdropWidth enumWidth : TMDbBackdropWidth.values()) {
            if (0.8 * backdropWidth <= enumWidth.maxWidth) {
                return enumWidth;
            }
        }
        return TMDbBackdropWidth.ORIGINAL;
    }
}

@@ -89,16 +89,54 @@
 ontext;%0A
+import android.content.res.Resources;%0A
 import a
@@ -158,16 +158,16 @@
 .Color;%0A
-
 import a
@@ -261,16 +261,41 @@
 etrics;%0A
+import android.util.Log;%0A
 import a
@@ -322,16 +322,86 @@
 nager;%0A%0A
+import com.loosli.christian.popularmovieapp.android.app.BuildConfig;%0A%0A
 /**%0A * C
@@ -2417,26 +2417,24 @@
 ageWidth) %7B%0A
-//
         if (
@@ -2454,18 +2454,16 @@
 EBUG) %7B%0A
-//
         
@@ -2549,18 +2549,16 @@
  %22px%22);%0A
-//
         
@@ -3401,16 +3401,1269 @@
 %0A    %7D%0A%0A
+    /**%0A     * This method converts dp unit to equivalent pixels, depending on device density.%0A     *%0A     * @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels%0A     * @param context Context to get resources and device specific display metrics%0A     * @return A float value to represent px equivalent to dp depending on device density%0A     */%0A    public static float convertDpToPixel(float dp, Context context)%7B%0A        Resources resources = context.getResources();%0A        DisplayMetrics metrics = resources.getDisplayMetrics();%0A        float px = dp * (metrics.densityDpi / 160f);%0A        return px;%0A    %7D%0A%0A    /**%0A     * This method converts device specific pixels to density independent pixels.%0A     *%0A     * @param px A value in px (pixels) unit. Which we need to convert into db%0A     * @param context Context to get resources and device specific display metrics%0A     * @return A float value to represent dp equivalent to px value%0A     */%0A    public static float convertPixelsToDp(float px, Context context)%7B%0A        Resources resources = context.getResources();%0A        DisplayMetrics metrics = resources.getDisplayMetrics();%0A        float dp = px / (metrics.densityDpi / 160f);%0A        return dp;%0A    %7D%0A
 %0A    // 

add method to convert px to dp and reverse