package com.lianxin.pubqq.setup.adpter;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import java.util.ArrayList;
import java.util.Random;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import com.lianxin.pubqq.R;
import com.lianxin.panqq.list.bean.EMGroup;
import com.lianxin.panqq.utils.ImagesUtils;

public class PickGroupAdpter extends BaseExpandableListAdapter {
    private Context context;  
    private List<EMGroup> Group;  
//    private List<Friend> Child;
    
    public PickGroupAdpter(Context context, List<EMGroup> objects1) {  
        this.context = context;  
        this.Group = objects1;
 //       this.Child = objects2;        
    }  
  
    @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 Child.get(groupPosition).get(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(context).inflate(  
                    R.layout.item_typelayout, 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(context).inflate(  
                    R.layout.item_childlayout, 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();  
        }  
   //     itemHolder.img.setImageResource(R.drawable.head);
        int image = Group.get(groupPosition).members.get(childPosition).imageid;
        if(image>80) image = image%80;
        itemHolder.img.setImageResource(ImagesUtils.images[image]);        
//        itemHolder.name.setText(Child.get(groupPosition).get(childPosition));
        int thisid = (int)getChildId(groupPosition, childPosition);       
        itemHolder.name.setText(Group.get(groupPosition).members.get(childPosition).nickname);        
   //     itemHolder.description.setText("10023");
     //   itemHolder.description.setText(Child.get(thisid).Id); 
    	int userid = Group.get(groupPosition).members.get(childPosition).userId;  
    	itemHolder.description.setText("["+userid+"]");    	
        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;    		
	}


}