package com.lianxin.pubqq.ulive.adpter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.list.adpter.BaseLiveLocalAdapter;
//import com.lianxin.pubqq.ulive.PhoneLiveActivity;
import com.lianxin.panqq.main.bean.LivePerson;
import com.lianxin.panqq.utils.ImagesUtils;


public class LiveLocalAdapter extends BaseLiveLocalAdapter{

	//private List<LivePerson> mList;
	//private LayoutInflater mInflater; 
	//private Context mContext;	
	//private ListView              mListView;	

	 public LiveLocalAdapter(Context context, List<LivePerson> userList) {
		super(context, userList);
		mInflater = LayoutInflater.from(context);
		mContext=context;
		this.mList=userList;
	}

	//设置listview对象   
    public void setListView(ListView lisv)  
    {  
        this.mListView = lisv;  
    }	
	
	@Override
	public int getCount() {
		return mList==null?0:mList.size();
	}

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

 
	public String getUser(int position) {
		return mList.get(position).getNickName();
	}
	
	 @Override
	public long getItemId(int position) {

		return position;		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.layout_item_live, null);
			holder.imageView = (ImageView)convertView.findViewById(R.id.img_item_gridview);
			holder.roomState = (TextView)convertView.findViewById(R.id.tv_roomname);
			holder.nickName = (TextView)convertView.findViewById(R.id.tv_nickname);			
			holder.onlineNum = (TextView) convertView.findViewById(R.id.tv_onlinenum);
		//	holder.rl_liveicon = (RelativeLayout) convertView.findViewById(R.id.rl_liveicon);		
		//	holder.command = (Button) convertView.findViewById(R.id.command);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
	//	if(position<80){
		final LivePerson person = getItem(position);
	  //  holder.img_gridview.setImageURI(person.getPictureUrl());
	    holder.roomState.setText(person.getRoomName());
	    holder.nickName.setText(person.getNickName());
	    String str = ""+person.getNickName()+",女,18岁,云南大理";
	    holder.nickName.setText(str);	    
	    holder.onlineNum.setText("观看人数:"+person.getOnlineNum());
	    int userid = person.getSendId();
	    int image = person.getImagePos();
	    setImageView(holder.imageView, ""+userid,image);	    
		//setImageView(imageView, url);	    
	  /*  holder.rl_liveicon.setBackgroundResource(R.drawable.live_header_live_type_pc);
        if (person.getMode() == 201) {
            holder.rl_liveicon.setBackgroundResource(R.drawable.live_header_live_type_mobile);
        }	*/    

		// 设置点击事件
		/*final int pos = position;	
		convertView.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
	               //  颜值栏目 竖屏播放
             if (person.getMode() == 201) {

         //   	 mContext.startActivity(new Intent(mContext, PhoneLiveActivity.class));             	 
             } else if (person.getMode() == 207) {
 
            //	 mContext.startActivity(new Intent(mContext, PhoneLiveActivity.class));            	 
             } else {
 
           // 	 mContext.startActivity(new Intent(mContext, PhoneLiveActivity.class));             	 
             }             
            }  
        });		*/


		return convertView;
	}

	private static class ViewHolder {

        //        图片
     //   public SimpleDraweeView img_item_gridview;
		public ImageView imageView;		
        //        房间名称
        public TextView roomState;
        //        在线人数
        public TextView onlineNum;
        //        昵称
        public TextView nickName;
        //        Icon
 
	}

	private void setImageView(ImageView imageView, String key, int image) {
		Bitmap bitmap = getBitmapFromMemCache(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]);			
		}
	}
	

	public int getItemPosition(int userid) {
	    for(int i=0;i<mList.size();i++)
	    {
	    	if(mList.get(i).sendid == userid )
	    	    return i;	        
	    } 		
		return -1;
	}	

    
    public void updateItemData( int userid, Bitmap bitmap )  
    {  
    	int position = getItemPosition(userid); 
    	if(position<0)
    		return;
        Message msg = Message.obtain();  
      /*  int ids = -1;  
        // 进行数据对比获取对应数据在list中的位置  
        for (int i = 0; i < mDataList.size(); i++)  
        {  
            if (mDataList.get(i).getPosition() == item.getPosition())  
            {  
                ids = i;  
            }  
        }  */
        msg.arg1 = position; 
        msg.arg1 = userid;        
        // 更新mDataList对应位置的数据  
      //  mList.set(position, item);  
        // handle刷新界面  
        han.sendMessage(msg);  
    }  
  
  //  @SuppressLint("HandlerLeak")  
	private Handler han = new Handler() {
		public void handleMessage(android.os.Message msg) {
			updateItem3(msg.arg1);
		};
	};

    /** 
     * 刷新指定item 
     *  
     * @param index item在listview中的位置 
     */  
    private void updateItem(int position)  
    {  
        if (mListView == null)  
        {  
            return;  
        } 
      
        if (position < mListView.getFirstVisiblePosition() || position > mListView.getLastVisiblePosition())  
        {  
            return;  
        }  
        // 获取当前可以看到的item位置  
        int visiblePosition = mListView.getFirstVisiblePosition();  
        // 如添加headerview后 firstview就是hearderview  
        // 所有索引+1 取第一个view  
        // View view = listview.getChildAt(index - visiblePosition + 1);  
        // 获取点击的view  
        View view = mListView.getChildAt(position - visiblePosition);  
        /*TextView txt = (TextView) view.findViewById(R.id.textView1);  
        // 获取mDataList.set(ids, item);更新的数据  
        MyListItem data = (MyListItem) getItem(index);  
        // 重新设置界面显示数据  
        txt.setText(data.getData());  */
        LivePerson person = getItem(position); 
  	    TextView nickName = (TextView)view.findViewById(R.id.tv_nickname); 
  	    nickName.setText(person.getNickName());  	    
    } 
    
    private void updateItem3(int userid)  
    {  
        if (mListView == null)  
        {  
            return;  
        } 
       	int position = getItemPosition(userid);         
        if (position < mListView.getFirstVisiblePosition() || position > mListView.getLastVisiblePosition())  
        {  
            return;  
        }  
        // 获取当前可以看到的item位置  
        int visiblePosition = mListView.getFirstVisiblePosition();  
        // 如添加headerview后 firstview就是hearderview  
        // 所有索引+1 取第一个view  
        // View view = listview.getChildAt(index - visiblePosition + 1);  
        // 获取点击的view  
        View view = mListView.getChildAt(position - visiblePosition);  
        /*TextView txt = (TextView) view.findViewById(R.id.textView1);  
        // 获取mDataList.set(ids, item);更新的数据  
        MyListItem data = (MyListItem) getItem(index);  
        // 重新设置界面显示数据  
        txt.setText(data.getData());  */
        LivePerson person = getItem(position); 
  	    TextView nickName = (TextView)view.findViewById(R.id.tv_nickname); 
  	    nickName.setText(person.getNickName());
  	    
		Bitmap bitmap = getBitmapFromMemCache(""+userid);
		if (bitmap == null)	
			return;		
		ImageView imageView = (ImageView) view.findViewById(R.id.img_item_gridview);
		imageView.setImageBitmap(bitmap);  	    
    }  
    
    public void updateItemThumb(int userid)  
    {  
        if (mListView == null)  
        {  
            return;  
        } 
       	int position = getItemPosition(userid);         
        if (position < mListView.getFirstVisiblePosition() || position > mListView.getLastVisiblePosition())  
        {  
            return;  
        }  
        // 获取当前可以看到的item位置  
        int visiblePosition = mListView.getFirstVisiblePosition();   
        View view = mListView.getChildAt(position - visiblePosition);  
 
		Bitmap bitmap = getBitmapFromMemCache(""+userid);
		if (bitmap == null)	
			return;		
		ImageView imageView = (ImageView) view.findViewById(R.id.img_item_gridview);
		imageView.setImageBitmap(bitmap);  	    
    }

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