package com.antew.redditinpictures.library.adapter;

import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.antew.redditinpictures.library.reddit.RedditApi.PostData;
import com.antew.redditinpictures.library.utils.ImageFetcher;

public class ImageAdapter extends BaseAdapter {

    private final Context         mContext;
    private int                   mItemHeight = 0;
    private int                   mNumColumns = 0;
    private GridView.LayoutParams mImageViewLayoutParams;
    private List<PostData>        mImages;
    private ImageFetcher          mImageFetcher;

    public ImageAdapter(Context context, ImageFetcher imageFetcher, List<PostData> urls) {
        super();
        mContext = context;
        mImages = urls;
        mImageFetcher = imageFetcher;
        mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    public PostData getPostData(int position) {
        return mImages.get(position);
    }

    public List<PostData> getPostData() {
        return mImages;
    }

    @Override
    public int getCount() {
        if (mImages == null)
            return 0;

        return mImages.size();
    }

    @Override
    public Object getItem(int position) {
        if (mImages == null)
            return null;

        return mImages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public void addItems(List<PostData> images) {
        mImages.addAll(images);
        notifyDataSetChanged();
    }

    public void clear() {
        mImages.clear();
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public void removeNsfwImages() {
        Iterator<PostData> it = mImages.iterator();
        while (it.hasNext()) {
            PostData post = (PostData) it.next();
            if (post.isOver_18())
                it.remove();
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {

        ImageView imageView;
        if (convertView != null && convertView instanceof ImageView) {
            // Otherwise re-use the converted view
            imageView = (ImageView) convertView;
        } else {
            // if it's not recycled, instantiate and initialize
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(mImageViewLayoutParams);
            
        }

        // Check the height matches our calculated column width
        if (imageView.getLayoutParams().height != mItemHeight) {
            imageView.setLayoutParams(mImageViewLayoutParams);
        }

        // Finally load the image asynchronously into the ImageView, this also takes care of
        // setting a placeholder image while the background thread runs
        mImageFetcher.loadImage(mImages.get(position).getUrl(), imageView, null);
        return imageView;
    }

    /**
     * Sets the item height. Useful for when we know the column width so the height can be set to
     * match.
     * 
     * @param height
     */
    public void setItemHeight(int height) {
        if (height == mItemHeight) {
            return;
        }
        mItemHeight = height;
        mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
        mImageFetcher.setImageSize(height);
        notifyDataSetChanged();
    }

    public void setNumColumns(int numColumns) {
        mNumColumns = numColumns;
    }

    public int getNumColumns() {
        return mNumColumns;
    }
}

@@ -331,16 +331,71 @@
 eView;%0A%0A
+import com.antew.redditinpictures.library.logging.Log;%0A
 import c
@@ -566,16 +566,79 @@
 apter %7B%0A
+    public static final String    TAG         = %22ImageAdapter%22;
 %0A    pri
@@ -2634,25 +2634,24 @@
 iner) %7B%0A
-%0A
         
 ImageVie
@@ -2646,150 +2646,190 @@
     
-Ima
+Log.i(TAG, %22
 ge
+t
 View 
-imageView;%0A        if (convertView != null && convertView instanceof ImageView) %7B%0A            // Otherwise re-use the converted view
+position = %22 + position);%0A        ViewHolder viewHolder = null;%0A        if (convertView == null) %7B%0A            // if it's not recycled, instantiate and initialize
 %0A   
@@ -2841,21 +2841,26 @@
     
-image
+convert
 View = 
-(
+new 
 Imag
@@ -2868,103 +2868,119 @@
 View
-) convertView;%0A        %7D else %7B%0A            // if it's not recycled, instantiate and initialize
+(mContext);%0A            Log.i(%22getView%22, %22NOT Recycling imageView%22);%0A            viewHolder = new ViewHolder();
 %0A   
@@ -2980,32 +2980,43 @@
 ();%0A            
+viewHolder.
 imageView = new 
@@ -3007,28 +3007,25 @@
 imageView = 
-new 
+(
 ImageView(mC
@@ -3017,34 +3017,37 @@
 = (ImageView
-(mContext)
+) convertView
 ;%0A          
@@ -3040,32 +3040,43 @@
 ew;%0A            
+viewHolder.
 imageView.setSca
@@ -3120,32 +3120,43 @@
 P);%0A            
+viewHolder.
 imageView.setLay
@@ -3202,16 +3202,327 @@
         
+convertView.setTag(viewHolder);%0A        %7D else %7B%0A        %7D%0A        if (convertView != null && convertView instanceof ImageView) %7B%0A            Log.i(%22getView%22, %22Recycling imageView%22);%0A            // Otherwise re-use the converted view%0A            viewHolder = (ViewHolder) convertView.getTag();%0A        %7D else %7B%0A
 %0A       
@@ -3601,16 +3601,27 @@
     if (
+viewHolder.
 imageVie
@@ -3677,16 +3677,27 @@
         
+viewHolder.
 imageVie
@@ -3979,16 +3979,27 @@
 tUrl(), 
+viewHolder.
 imageVie
@@ -4000,16 +4000,22 @@
 ageView,
+ null,
  null);%0A
@@ -4029,21 +4029,23 @@
  return 
-image
+convert
 View;%0A  
@@ -4686,10 +4686,80 @@
 ;%0A    %7D%0A
+    %0A    static class ViewHolder %7B%0A        ImageView imageView;%0A    %7D%0A
 %7D%0A

Clean ImageAdapter to use a ViewHolder.