package org.tndata.android.compass.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.widget.ImageView;

import org.tndata.android.compass.R;


public class ImageHelper {
    public final static int SELECTED = 1;
    public final static int ADD = 2;
    public final static int CHOOSE = 3;

    public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    public static void setupImageViewButton(final Resources resources, ImageView imageView,
                                            int style) {
        GradientDrawable buttonDrawable = (GradientDrawable) imageView.getBackground();
        int color = -1;
        switch (style) {
            case SELECTED:
                color = resources.getColor(R.color.grow_primary_dark);
                imageView.setImageResource(R.drawable.ic_check);
                break;
            case ADD:
                color = resources.getColor(R.color.grow_accent);
                imageView.setImageResource(R.drawable.ic_add_white_24dp);
                break;
            case CHOOSE:
                color = resources.getColor(android.R.color.transparent);
                imageView.setImageResource(R.drawable.ic_more_vert);
                break;
        }
        if (color != -1) {
            buttonDrawable.setColor(color);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            imageView.setBackground(buttonDrawable);
        } else {
            imageView.setBackgroundDrawable(buttonDrawable);
        }
    }

    public static Bitmap getCircleBitmap(Bitmap bmp, int size){
        Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bmp, size, size);
        Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        int color = Color.RED;
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, size, size);
        RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(thumbnail, rect, rect, paint);

        //thumbnail.recycle();
        return output;
    }

    public static Bitmap cropOutBottom(Bitmap bmp){
        int width = bmp.getWidth();
        int height = width/2;

        return Bitmap.createBitmap(bmp, 0, 0, width, height);
    }
}

@@ -565,16 +565,94 @@
 ss.R;%0A%0A%0A
+/**%0A * Image utility class.%0A *%0A * @author Ismael Alonso%0A * @version 1.0.0%0A */%0A
 public c
@@ -667,17 +667,16 @@
 geHelper
- 
 %7B%0A    pu
@@ -797,35 +797,28 @@
     
-public static int c
+/**%0A     * C
 alculate
 InSa
@@ -817,95 +817,359 @@
 late
-InS
+s the s
 ample
-Size(BitmapFactory.Options options,%0A                              
+ size given a bitmap's parameters and requested width and height.%0A     *%0A     * @param options the bitmap's parameters.%0A     * @param reqWidth the minimum width.%0A     * @param reqHeight the minimum height.%0A     * @return the appropriate inSampleSize value.%0A
      
+*/%0A
     
-    
+public static int calculateInSampleSize(BitmapFactory.Options options,
  int
@@ -1193,17 +1193,16 @@
 qHeight)
- 
 %7B%0A      
@@ -1408,19 +1408,17 @@
 eqWidth)
- %7B%0A
+%7B
 %0A       
@@ -1451,19 +1451,17 @@
 = height
- / 
+/
 2;%0A     
@@ -1494,19 +1494,17 @@
  = width
- / 
+/
 2;%0A%0A    
@@ -1513,17 +1513,16 @@
       //
- 
 Calculat
@@ -1578,31 +1578,16 @@
 of 2 and
-%0A            //
  keeps b
@@ -1604,16 +1604,18 @@
       //
+  
  height 
@@ -1698,19 +1698,17 @@
 lfHeight
- / 
+/
 inSample
@@ -1724,36 +1724,16 @@
 eqHeight
-%0A                   
  && (hal
@@ -1742,11 +1742,9 @@
 idth
- / 
+/
 inSa
@@ -1764,17 +1764,16 @@
 eqWidth)
- 
 %7B%0A      
@@ -1854,32 +1854,69 @@
 pleSize;%0A    %7D%0A%0A
+    @SuppressWarnings(%22deprecation%22)%0A
     public stati
@@ -1994,52 +1994,8 @@
 iew,
-%0A                                           
  int
@@ -1997,25 +1997,24 @@
 , int style)
- 
 %7B%0A        Gr
@@ -3002,32 +3002,229 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Creates a circle bitmap from the provided bitmap.%0A     *%0A     * @param bmp the source bitmap.%0A     * @param size the size of the result.%0A     * @return the processed bitmap.%0A     */%0A
     public stati
@@ -3961,24 +3961,193 @@
 put;%0A    %7D%0A%0A
+    /**%0A     * Crops out the bottom section of a bitmap to turn it a 2:1 ratio bitmap.%0A     * @param bmp the source bitmap.%0A     * @return the processed bitmap.%0A     */%0A
     public s

Document more utility