package fr.free.nrw.commons.upload;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.graphics.BitmapCompat;
import android.view.View;
import android.widget.FrameLayout;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import timber.log.Timber;

public class Zoom {

    private View thumbView;
    private ContentResolver contentResolver;
    private FrameLayout flContainer;

    Zoom(View thumbView, FrameLayout flContainer, ContentResolver contentResolver) {
        this.thumbView = thumbView;
        this.contentResolver = contentResolver;
        this.flContainer = flContainer;
    }

    Bitmap createScaledImage(InputStream input, Uri imageUri) {

        Bitmap scaled = null;
        BitmapRegionDecoder decoder = null;
        Bitmap bitmap = null;

        try {
            decoder = BitmapRegionDecoder.newInstance(input, false);
            bitmap = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
        } catch (IOException e) {
            Timber.e(e);
        } catch (NullPointerException e) {
            Timber.e(e);
        }
        try {
            //Compress the Image
            System.gc();
            Runtime rt = Runtime.getRuntime();
            long maxMemory = rt.freeMemory();
            bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri);
            int bitmapByteCount = BitmapCompat.getAllocationByteCount(bitmap);
            long height = bitmap.getHeight();
            long width = bitmap.getWidth();
            long calHeight = (long) ((height * maxMemory) / (bitmapByteCount * 1.1));
            long calWidth = (long) ((width * maxMemory) / (bitmapByteCount * 1.1));
            scaled = Bitmap.createScaledBitmap(bitmap, (int) Math.min(width, calWidth), (int) Math.min(height, calHeight), true);
        } catch (IOException e) {
            Timber.e(e);
        } catch (NullPointerException e) {
            Timber.e(e);
            scaled = bitmap;
        }
        return scaled;
    }

    float adjustStartEndBounds(Rect startBounds, Rect finalBounds, Point globalOffset) {
        // Calculate the starting and ending bounds for the zoomed-in image.
        // The start bounds are the global visible rectangle of the thumbnail,
        // and the final bounds are the global visible rectangle of the container
        // view. Also set the container view's offset as the origin for the
        // bounds, since that's the origin for the positioning animation
        // properties (X, Y).
        thumbView.getGlobalVisibleRect(startBounds);
        flContainer.getGlobalVisibleRect(finalBounds, globalOffset);
        startBounds.offset(-globalOffset.x, -globalOffset.y);
        finalBounds.offset(-globalOffset.x, -globalOffset.y);

        // Adjust the start bounds to be the same aspect ratio as the final
        // bounds using the "center crop" technique. This prevents undesirable
        // stretching during the animation. Also calculate the start scaling
        // factor (the end scaling factor is always 1.0).
        float startScale;
        if ((float) finalBounds.width() / finalBounds.height()
                > (float) startBounds.width() / startBounds.height()) {
            // Extend start bounds horizontally
            startScale = (float) startBounds.height() / finalBounds.height();
            float startWidth = startScale * finalBounds.width();
            float deltaWidth = (startWidth - startBounds.width()) / 2;
            startBounds.left -= deltaWidth;
            startBounds.right += deltaWidth;
        } else {
            // Extend start bounds vertically
            startScale = (float) startBounds.width() / finalBounds.width();
            float startHeight = startScale * finalBounds.height();
            float deltaHeight = (startHeight - startBounds.height()) / 2;
            startBounds.top -= deltaHeight;
            startBounds.bottom += deltaHeight;
        }
        return startScale;
    }
}

@@ -501,16 +501,92 @@
 imber;%0A%0A
+/**%0A * Contains utility methods for the Zoom function in ShareActivity.%0A */%0A
 public c
@@ -921,24 +921,255 @@
 ner;%0A    %7D%0A%0A
+    /**%0A     * Create a scaled bitmap to display the zoomed-in image%0A     * @param input the input stream corresponding to the uploaded image%0A     * @param imageUri the uploaded image's URI%0A     * @return a zoomable bitmap%0A     */%0A
     Bitmap c
@@ -2542,103 +2542,19 @@
     
-float adjustStartEndBounds(Rect startBounds, Rect finalBounds, Point globalOffset) %7B
+/**
 %0A     
+*
  
-  //
  Cal
@@ -2625,34 +2625,190 @@
     
+*
   
- // The start bounds are
+Also set the container view's offset as the origin for the%0A     * bounds, since that's the origin for the positioning animation%0A     * properties (X, Y).%0A     * @param startBounds
  the
@@ -2853,46 +2853,35 @@
 nail
-,%0A        // and the
+%0A     * @param
  final
- b
+B
 ounds 
-are 
 the 
@@ -2925,31 +2925,37 @@
 iner
+ view
 %0A     
-   // view. Also 
+* @param globalOff
 set 
@@ -2985,133 +2985,141 @@
 fset
- as the origin for the%0A        // bounds, since that's the origin for the positioning animation%0A        // properties (X, Y).
+%0A     * @return scaled start bounds%0A     */%0A    float adjustStartEndBounds(Rect startBounds, Rect finalBounds, Point globalOffset) %7B%0A
 %0A   

Add Javadocs to Zoom.java