package ai.pai.client.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import java.util.List;

import ai.pai.client.R;
import ai.pai.client.beans.PhotoItem;
import ai.pai.client.utils.ImageUtils;
import ai.pai.client.utils.PhotoLoader;

public class GalleryAdapter extends BaseAdapter {

    private Context mContext;
    private List<PhotoItem> values;
    private static DisplayImageOptions options;
    private int width;

    public GalleryAdapter(Context context, List<PhotoItem> values) {
        this.mContext = context;
        this.values = values;
        options = PhotoLoader.getPhotoOptions();
        width = ImageUtils.getCameraAlbumWidth(context);
    }

    @Override
    public int getCount() {
        return values.size();
    }

    @Override
    public Object getItem(int position) {
        return values.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final GalleryHolder holder;

        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            convertView = layoutInflater.inflate(R.layout.item_gallery, null);
            convertView.setLayoutParams(new AbsListView.LayoutParams(width, width));
            holder = new GalleryHolder();
            holder.photo = (ImageView) convertView.findViewById(R.id.gallery_sample_image);
            convertView.setTag(holder);
        } else {
            holder = (GalleryHolder) convertView.getTag();
        }
        final PhotoItem photoItem = (PhotoItem) getItem(position);
        PhotoLoader.displayLocalImage(photoItem.getImageUri(), holder.photo, options);
        return convertView;
    }

    static class GalleryHolder {
        ImageView photo;
    }

}
