package com.lianxin.pubqq.ulive.adpter;

import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.list.adpter.BaseRoomAdapter;
import com.lianxin.panqq.list.bean.Member;
import com.lianxin.panqq.utils.ImagesUtils;
import com.lianxin.panqq.widget.ExpandGridView;


public class VideoHeadGridAdapter extends BaseRoomAdapter{

	protected List<Member> mList;	
	protected GridView  mGridView;	
	protected LayoutInflater mInflater;
	protected Context mContext;		
    private OnMyHeadClickListener clickListener;	

	public  VideoHeadGridAdapter(Context context, List<Member> objects) {
		super(context, objects);		
		mInflater = LayoutInflater.from(context);
		this.mList = objects;
		this.mContext = context;
	}

	 //设置gridview对象   
    public void setGridView(ExpandGridView griv)  
    {  
        this.mGridView = griv;  
    }
    
    @Override
	public void setData(Object object1) {
	    this.mList = (List<Member>)object1;
    }    
    
	@Override
	public int getCount() {
		return mList.size();
	}

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

	@Override
	public long getItemId(int position) {
		return position;
	}	
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
	    final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_grouphead_gridview, null);
			holder.imageView = (ImageView)convertView.findViewById(R.id.iv_avatar);
			holder.nickName = (TextView)convertView.findViewById(R.id.tv_username);			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}		

			final Member user = mList.get(position);
		    holder.nickName.setText(user.getName());			
		    final int userid = user.getUserId();
			int imageid = user.getHeadImg();
		if(userid<=1000){
			holder.imageView.setImageResource(R.drawable.empty_photo);	
			holder.nickName.setText(""+userid);				
		}else{
			if(imageid<0||imageid>80)
				imageid = imageid%80;
			final String useravatar = user.getHeadUrl();
			holder.nickName.setText(user.getName());

			//holder.imageView.setImageResource(ImagesUtils.images[imageid]);							
			Bitmap bitmap = getHeadFromMemCache(""+userid);	//查找缩略图		
			if (bitmap != null) {
				holder.imageView.setImageBitmap(bitmap);
			} else {
		        holder.imageView.setImageResource(ImagesUtils.images[imageid]);			
			}			
			holder.imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//deleteMemberFromPlayers(user);
                    if(clickListener != null){                        	
                            //clickListener.onItemClicked(convertView, user, position); 
                            clickListener.onItemClicked( userid, user, position);                            
                    }
				}
			});
		}
		return convertView;
	}	

	private static class ViewHolder {
		public ImageView imageView;	//缩略图	     
        public TextView nickName; //昵称
	}	

    public void setMyItemClickListener(OnMyHeadClickListener childListener) {  
        this.clickListener = childListener;  
    }	
	
    public static interface OnMyHeadClickListener{  
       // public void onItemClicked(View view, Member user, int position); 
        public void onItemClicked( int userid, Member user, int position);        
    }	
	
	public int getItemPosition( int userid ) {
	    for(int i=0;i<mList.size();i++)
	    {
	    	if(mList.get(i).userId == userid )
	    	    return i;	        
	    } 		
		return -1;
	}	

	private void setImageView(ImageView imageView, String key, int image) {
		Bitmap bitmap = getHeadFromMemCache(key);	//查找缩略图		
		if (bitmap != null) {
			imageView.setImageBitmap(bitmap);
		} else {
			//imageView.setImageResource(R.drawable.empty_photo);
	        if(image>80) image = image%80;
	        imageView.setImageResource(ImagesUtils.images[image]);			
		}
	}	

	//添加Bitmap到内存缓存
	/*private void addBitmaptoMemCache(String key, Bitmap bitmap) {
		if (getBitmapFromMemCache(key) == null && bitmap != null) {
			playBitmapCaches.put(key, bitmap);
		}
	}
	//将Bitmap置换入内存缓存中
	public static void putBitmaptoMemCache(String key, Bitmap bitmap) {
		playBitmapCaches.put(key, bitmap);
	}	
	//从内存缓存中获取Bitmap
	private Bitmap getBitmapFromMemCache(String key) {
		return  playBitmapCaches.get(key);		
	}	*/

	/*@Override
	public void setData(Object object) {
		// TODO Auto-generated method stub
		
	}	*/
	
}
