package cn.net.caoyao.cpzshandset.ui.adapter;

import java.util.List;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.bitmap.MBitmapService;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.cpzslibs.prot.handset.bean.Product;

public class ProductImageAdapter extends BaseAdapter{
	private List<Product> pL;
	private MBitmapService bitmapService;
	private Bitmap defaultBitmap;
	public ProductImageAdapter(List<Product> pL, MBitmapService bitmapService,Bitmap defaultBitmap) {
		this.pL = pL;
		this.bitmapService = bitmapService;
		this.defaultBitmap = defaultBitmap;
	}

	public void refreshData(List<Product> pL){
		this.pL = pL;
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return pL.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return pL.get(position);
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_product_image_item, parent,false);
			holder = new ViewHolder();
			holder.image_p_image_item = (ImageView) convertView.findViewById(R.id.image_p_image_item);
			holder.tv_p_name = (TextView) convertView.findViewById(R.id.tv_p_name);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		LogUtil.d(TConfig.Echo, "position:"+position +" url:" + pL.get(position).getImage());
//		holder.image_p_image_item.setBackgroundResource(R.drawable.product_image_bg_default);
		holder.image_p_image_item.setImageBitmap(defaultBitmap);
		bitmapService.display(holder.image_p_image_item, pL.get(position).getImage(),defaultBitmap,defaultBitmap);
//		Glide.with(parent.getContext()).load(pL.get(position).getImage()).placeholder(R.drawable.product_image_bg_default).into(holder.image_p_image_item);
		holder.tv_p_name.setText(pL.get(position).getName()+"");
		
		return convertView;
	}

	private class ViewHolder{
		ImageView image_p_image_item;
		TextView tv_p_name;
	}
}
