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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;
import android.widget.ExpandableListView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.utils.UserheadUtils;
//import com.lianxin.panqq.entity.FileConnect;
//import com.lianxin.panqq.entity.HeadUpNew;
//import com.lianxin.panqq.entity.HeadUpdata;
import com.lianxin.panqq.list.bean.FriendType;
import com.lianxin.panqq.list.bean.Friend;
import com.lianxin.panqq.list.adpter.ExpandableLisAdapter;
import com.lianxin.panqq.main.PathUtil;
//import com.lianxin.panqq.utils.ImageLoader;
import com.lianxin.panqq.utils.ImagesUtils;
import com.lianxin.panqq.utils.PreferenceUtils;

public class FriendAdpter extends ExpandableLisAdapter {  
    private List<FriendType> Group;  
    private List<Friend> Child;
    private ExpandableListView expandableListView;
    
    public FriendAdpter(Context context, List<FriendType> objects1, List<Friend> objects2) {  
       // this.context = context;  
	    super(context);	     
        this.Group = objects1;
        this.Child = objects2;        
    }  

	@Override
	public void setData(Object object1, Object object2) {
		// TODO Auto-generated method stub
	    this.Group = (List<FriendType>)object1;
	    this.Child = (List<Friend>)object2; 	
	}   
 
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
  
            expandableListView.collapseGroup(1);
            //必须重新伸缩之后才能更新数据
            expandableListView.expandGroup(1);        	
            notifyDataSetChanged(); //更新数据
            super.handleMessage(msg);
        }
    };    
 
    public void refresh(ExpandableListView exListView) {  
        this.expandableListView = expandableListView;   	
        handler.sendMessage(new Message());  
    }    
    
    @Override  
    public int getGroupCount() { 
        if(Group == null || Group.size() <= 0){  
            return 0;  
        }    	
        return Group.size();  
    }  
  
    @Override  
    public int getChildrenCount(int groupPosition) {    
    	int ntype = Group.get(groupPosition).typeId;
    	int count = 0;    	
	    for(int i=0;i<Child.size();i++)
	    {
	    	if(Child.get(i).type == ntype )
	    	    count++;	        
	    } 
        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); 
    	int ntype = Group.get(groupPosition).typeId;
    	int count = 0;  
    	int thisid = 0;    	
	    for(int i=0;i<Child.size();i++)
	    {
	    	if(Child.get(i).type == ntype )
	    	{

	    	    if(count == childPosition)
	    	    {
	    	    	thisid = i;
	    	    	break;
	    	    }
	    		count++;	    	    
	    	}
	    } 
        return Child.get(thisid);      
    }  
  
    @Override  
    public long getGroupId(int groupPosition) {  
        return groupPosition;  
    }  
  
    @Override  
    public long getChildId(int groupPosition, int childPosition) {  
  
    	int ntype = Group.get(groupPosition).typeId;
    	int count = 0;  
    	int thisid = 0;    	
	    for(int i=0;i<Child.size();i++)
	    {
	    	if(Child.get(i).type == ntype )
	    	{
	    	    if(count == childPosition)
	    	    {
	    	    	thisid = i;
	    	    	break;
	    	    }
	    		count++;	    	    
	    	}
	    } 
        return thisid;	    
    }  

    public long getUserId(int groupPosition, int childPosition) {  
    	  
    	int ntype = Group.get(groupPosition).typeId;
    	int count = 0;  
    	int thisid = 0;    	
	    for(int i=0;i<Child.size();i++)
	    {
	    	if(Child.get(i).type == ntype )
	    	{
	    	    if(count == childPosition)
	    	    {
	    	    	thisid = i;
	    	    	break;
	    	    }
	    		count++;	    	    
	    	}
	    } 
        return Child.get(thisid).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 group=Group.get(groupPosition).typename;		
		groupHolder.groupName.setText(group);
		int count=getChildrenCount(groupPosition);
		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();  
        }  ;
        itemHolder.img.setImageResource(R.drawable.page);        
        int thisid = (int)getChildId(groupPosition, childPosition);       
        itemHolder.name.setText(Child.get(thisid).nickname); 
        int image = Child.get(thisid).imageid;
        long userhead = Child.get(thisid).userhead;      
        if(image>80) image = image%80;
        itemHolder.img.setImageResource(ImagesUtils.images[image]);        
        //if(userhead>0x10000)
        //	UserheadUtils.showUserHead(thisid,userhead,itemHolder.img,DefineUser.TYPE_FRIEND);
    	int userid = Child.get(thisid).userId;  
    	itemHolder.description.setText("["+userid+"]");
        if(userhead>0x10000)
        	UserheadUtils.showUserHead(userid,userhead,itemHolder.img,DefineUser.TYPE_FRIEND);   	
 	
        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;    		
	}

}