package org.ccci.gto.android.common.picasso.transformation;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.squareup.picasso.Transformation;

public class ScaleTransformation implements Transformation {
    private final int mMinWidth;
    private final int mMinHeight;
    private final boolean mOnlyScaleDown;

    @Nullable
    private transient String mKey;

    public ScaleTransformation(final int minWidth, final int minHeight) {
        mMinWidth = minWidth;
        mMinHeight = minHeight;
        mOnlyScaleDown = true;
    }

    @NonNull
    @Override
    public Bitmap transform(@NonNull final Bitmap source) {
        final int inWidth = source.getWidth();
        final int inHeight = source.getHeight();
        if (inWidth <= 0 || inHeight <= 0) {
            return source;
        }

        // calculate target size enforcing minWidth & minHeight
        float scale = ((float) mMinWidth) / inWidth;
        if (scale * inHeight < mMinHeight) {
            scale = ((float) mMinHeight) / inHeight;
        }

        // only scale if necessary
        if (!mOnlyScaleDown || scale < 1) {
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
            final Bitmap target = Bitmap.createBitmap(source, 0, 0, inWidth, inHeight, matrix, false);

            // recycle source if we have a different target
            if (target != source) {
                source.recycle();
            }

            return target;
        }

        // return the source if we aren't scaling
        return source;
    }

    @Override
    public String key() {
        if (mKey == null) {
            mKey = "scale(minWidth=" + mMinWidth + ",minHeight=" + mMinHeight + ",onlyScaleDown=" + mOnlyScaleDown +
                    ")";
        }

        return mKey;
    }
}

@@ -538,32 +538,183 @@
 nt minHeight) %7B%0A
+        this(minWidth, minHeight, true);%0A    %7D%0A%0A    public ScaleTransformation(final int minWidth, final int minHeight, final boolean onlyScaleDown) %7B%0A
         mMinWidt
@@ -784,20 +784,29 @@
 eDown = 
-true
+onlyScaleDown
 ;%0A    %7D%0A

create a constructor that will set onlyScaleDown