package com.dyhpoon.fodex.fullscreen;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dyhpoon.fodex.fodexView.FodexImageContract;
import com.dyhpoon.fodex.util.MediaImage;
import com.ortiz.touchview.TouchImageView;

import java.util.Stack;


/**
 * Created by darrenpoon on 26/1/15.
 */
public abstract class ReusableFullscreenAdapter extends PagerAdapter {

    public TouchImageView currentView = null;

    public abstract Uri imageUriAtPosition(int position);
    public abstract void onLongClick(int position);
    public abstract void onClick(int position);

    private int mWidth, mHeight;
    private Stack<View> mRecycledViews = new Stack<>();

    public ReusableFullscreenAdapter(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        mWidth = metrics.widthPixels;
        mHeight = metrics.heightPixels;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, final Object object) {
        currentView = (TouchImageView)object;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        final ImageView photoView = (ImageView) createOrRecycleView(container.getContext());
        Uri uri = imageUriAtPosition(position);

        // load bitmap in main thread if the image is in local disk.
        if (uri.toString().contains("content")) {
            Bitmap bitmap = MediaImage.loadBitmapSynchronously(photoView.getContext(), uri, mWidth, mHeight);
            photoView.setImageBitmap(bitmap);
        }
        // otherwise, load image asynchronously
        else {
            MediaImage.loadBitmapAsynchronously(photoView, uri, mWidth, mHeight);
        }
        container.addView(photoView, 0);
        photoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ReusableFullscreenAdapter.this.onLongClick(position);
                return true;
            }
        });
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReusableFullscreenAdapter.this.onClick(position);
            }
        });
        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View recycledView = (View) object;
        container.removeView(recycledView);
        cleanView((TouchImageView)recycledView);
        mRecycledViews.push(recycledView);
    }

    private View createOrRecycleView(Context context) {
        View photoView;
        if (mRecycledViews.isEmpty()) {
            photoView = new TouchImageView(context);
            photoView.setBackgroundColor(Color.BLACK);
            photoView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
            ));
            photoView.setMinimumHeight(FodexImageContract.preferredMinimumHeight(context));
        } else {
            photoView = mRecycledViews.pop();
        }
        photoView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        return photoView;
    }

    private void cleanView(TouchImageView v) {
        v.setLayerType(View.LAYER_TYPE_NONE, null);
        v.setImageDrawable(null);
        v.resetZoom();
    }
}

@@ -127,16 +127,108 @@
 .Color;%0A
+import android.graphics.drawable.BitmapDrawable;%0Aimport android.graphics.drawable.Drawable;%0A
 import a
@@ -243,16 +243,16 @@
 et.Uri;%0A
-
 import a
@@ -1721,361 +1721,8 @@
 n);%0A
-%0A        // load bitmap in main thread if the image is in local disk.%0A        if (uri.toString().contains(%22content%22)) %7B%0A            Bitmap bitmap = MediaImage.loadBitmapSynchronously(photoView.getContext(), uri, mWidth, mHeight);%0A            photoView.setImageBitmap(bitmap);%0A        %7D%0A        // otherwise, load image asynchronously%0A        else %7B%0A    
     
@@ -1795,26 +1795,16 @@
 eight);%0A
-        %7D%0A
         
@@ -3437,33 +3437,302 @@
     
-v.setImageDrawable(null);
+Drawable drawable = v.getDrawable();%0A        if (drawable instanceof BitmapDrawable) %7B%0A            Bitmap bitmap = ((BitmapDrawable)v.getDrawable()).getBitmap();%0A            v.setImageDrawable(null);%0A            bitmap.recycle();%0A        %7D else %7B%0A            v.setImageDrawable(null);%0A        %7D
 %0A   

Change to use background thread to load images. Recycle bitmap when item is destroyed to prevent frequent GC calls.