package com.lianxin.pubqq.main.adpter;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.DefineChat;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.utils.PowerUtils;
import com.lianxin.panqq.common.utils.UserheadUtils;
import com.lianxin.panqq.list.bean.EMGroup;
import com.lianxin.panqq.list.bean.Member;
import com.lianxin.panqq.list.adpter.ExpandableLisAdapter;
import com.lianxin.panqq.utils.ImagesUtils;
public class GroupAdpter extends ExpandableLisAdapter { 
    private List<EMGroup> Group;  
    private int nType;
  
    public GroupAdpter(Context context, List<EMGroup> objects1, int type) {  
	    super(context);	
        this.Group = objects1;
        this.nType = type;        
    }  

	@Override
	public void setData(Object object1,Object object2) {
	    this.Group = (List<EMGroup>)object1;
    } 
 
	public void setData(Object object1) {
	    this.Group = (List<EMGroup>)object1;
    }	
	
    @Override  
    public int getGroupCount() {  
        if(Group == null || Group.size() <= 0){  
            return 0;  
        }     	
        return Group.size();  
    }  
  
    @Override  
    public int getChildrenCount(int groupPosition) { 
        if(Group.get(groupPosition).members == null || Group.get(groupPosition).members.size() <= 0){  
            return 0;  
        }     	
    	int count = Group.get(groupPosition).members.size();
        return count;  	    
    }  
  
    @Override  
    public Object getGroup(int groupPosition) {  
        return Group.get(groupPosition);  
    }  
  
    @Override  
    public Object getChild(int groupPosition, int childPosition) {  
 
        return Group.get(groupPosition).members.get(childPosition);      
    }  
  
    @Override  
    public long getGroupId(int groupPosition) {  
        return groupPosition;  
    }  
  
    @Override  
    public long getChildId(int groupPosition, int childPosition) {  
  
 
        return Group.get(groupPosition).members.get(childPosition).userId;	    
    }  
  
    @Override  
    public boolean hasStableIds() {  
        return true;  
    }  
    
    @Override  
    public View getGroupView(int groupPosition, boolean isExpanded,  
                             View convertView, ViewGroup parent) {  
        GroupHolder groupHolder = null;     //我们自己设定的一个简单类，用来存储控件的相关信息  
        if (convertView == null) {          //这里的convertView其实是一个起缓冲作用的，工具，因为当一个item从屏幕中滚出，我们把它放到convertView里                                                 //，这样再滑回来的时候可以直接去取，不用重新创建，这里也推荐一个网址，大家可以详细了解  
            convertView = LayoutInflater.from(mContext).inflate(  
                    R.layout.layout_item_group, null);      //把界面放到缓冲区  
            groupHolder = new GroupHolder();          //实例化我们创建的这个类  
            groupHolder.groupName=(TextView)convertView.findViewById(R.id.groupName);  //实例化类里的TextView  
            groupHolder.groupCount=(TextView)convertView.findViewById(R.id.groupCount);
            convertView.setTag(groupHolder);                                 //给view对象一个标签，告诉计算机我们已经在缓冲区里放了一个view，下回直                                                                               //接来拿就行了  
        } else {  
            groupHolder = (GroupHolder) convertView.getTag();     //然后他就直接来拿  
        }  
//        groupHolder.txt.setText(Group.get(groupPosition));//最后在相应的group里设置他相应的Text  
//		String group=getGroup(groupPosition).toString();
		String groupname = Group.get(groupPosition).nickname;	
		if(TextUtils.isEmpty(groupname))
			groupname = Group.get(groupPosition).groupname;			
		groupHolder.groupName.setText(groupname);
		int count=getChildrenCount(groupPosition);
	//	int count1=Group.get(groupPosition).Id;		
		groupHolder.groupCount.setText("["+count+"]");		
        return convertView; 
    	
         
    } 
    
    @Override  
    public View getChildView(int groupPosition, int childPosition,  
                             boolean isLastChild, View convertView, ViewGroup parent) {  
 
        ItemHolder itemHolder = null;  
        if (convertView == null) {  
            convertView = LayoutInflater.from(mContext).inflate(  
                    R.layout.layout_item_child, null);  
            itemHolder = new ItemHolder();  
            itemHolder.img=(ImageView)convertView.findViewById(R.id.contactIcon);   		
    		itemHolder.name=(TextView)convertView.findViewById(R.id.name);
    		itemHolder.description=(TextView)convertView.findViewById(R.id.description);

            convertView.setTag(itemHolder);  
        } else {  
            itemHolder = (ItemHolder) convertView.getTag();  
        }  
 
		final Member bean = (Member)(Group.get(groupPosition).members.get(childPosition));
		int userid = bean.userId;
		int imageid = bean.imageid%80;		
		itemHolder.img.setImageResource(ImagesUtils.images[imageid]);
        long userhead = bean.userhead;              
        if(userhead>0x10000)
        	UserheadUtils.showUserHead(userid,userhead,itemHolder.img,nType);	
		String strname = bean.getName()+"("+bean.getUserId()+")";
		String strContent = ""+(childPosition+1)+","+PowerUtils.getPowerText(nType,bean.getPower())+","+bean.sex+","+bean.age+"岁,"+bean.index;		
		itemHolder.name.setText(strname);
		itemHolder.description.setText(strContent);	    	
        return convertView;      	

    }  
  
    @Override  
    public boolean isChildSelectable(int groupPosition, int childPosition) {  
        return true;  
    } 
    
	public static class GroupHolder {
		TextView groupName;  
		TextView groupCount;    		
	}    
    
	public static class ItemHolder {
		ImageView img;
		TextView name;
		TextView description;    		
	}


}