package org.gdg.frisbee.android.view;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import com.squareup.picasso.Transformation;

public class BitmapBorderTransformation implements Transformation {
    private int mBorderSize;
    private int mCornerRadius = 0;
    private int mColor;

    public BitmapBorderTransformation(int borderSize, int cornerRadius, int color) {
        this.mBorderSize = borderSize;
        this.mCornerRadius = cornerRadius;
        this.mColor = color;
    }

    @Override
    public Bitmap transform(Bitmap source) {
        int width = source.getWidth();
        int height = source.getHeight();

        Bitmap image = Bitmap.createBitmap(width, height, source.getConfig());
        Canvas canvas = new Canvas(image);
        canvas.drawARGB(0, 0, 0, 0);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Rect rect = new Rect(0, 0, width, height);


        if (this.mCornerRadius == 0) {
            canvas.drawRect(rect, paint);
        } else {
            canvas.drawRoundRect(new RectF(rect),
                    this.mCornerRadius, this.mCornerRadius, paint);
        }

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

        Bitmap output;

        if (this.mBorderSize == 0) {
            output = image;
        } else {
            width = width + this.mBorderSize * 2;
            height = height + this.mBorderSize * 2;

            output = Bitmap.createBitmap(width, height, source.getConfig());
            canvas.setBitmap(output);
            canvas.drawARGB(0, 0, 0, 0);

            rect = new Rect(0, 0, width, height);

            paint.setXfermode(null);
            paint.setColor(this.mColor);
            paint.setStyle(Paint.Style.FILL);

            canvas.drawRoundRect(new RectF(rect), this.mCornerRadius, this.mCornerRadius, paint);

            canvas.drawBitmap(image, this.mBorderSize, this.mBorderSize, null);
        }

        if (source != output) {
            source.recycle();
        }

        return output;
    }

    @Override
    public String key() {
        return "bitmapBorder("
                + "borderSize="
                + this.mBorderSize + ", "
                + "cornerRadius=" + this.mCornerRadius + ", "
                + "color=" + this.mColor + ")";
    }
}

@@ -729,24 +729,104 @@
 p source) %7B%0A
+        if (source.getConfig() == null) %7B%0A            return source;%0A        %7D%0A%0A
         int 

transform bitmap only when configuration available