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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lianxin.pubqq.R;

import com.lianxin.panqq.list.bean.Friend;
import com.lianxin.panqq.list.bean.FriendType;

public class MyListAdpter extends BaseAdapter{  
        
        private static final int VIEW_TYPE_GROUP = 0;  
        private static final int VIEW_TYPE_CHILD = 1;  
          
        private Context context;   
        private List<FriendType> groupList;  
        private List<Friend> childList;        
        private OnGroupClickListener groupListener;  
        private OnChildClickListener childListener;  
          
        public MyListAdpter(Context context, List<FriendType> groupList, List<Friend> childList){  
            this.context = context;  
            this.groupList = groupList;  
            this.childList = childList;            
        }  
        @Override  
        public int getCount() {  
            if(groupList == null || groupList.size() <= 0){  
                return 0;  
            }  
            int count = 0;  
            for(FriendType group : groupList){  
                count ++;  
//                List<ChildBean> childList = group.getChildList();  
                if(childList == null){  
                    continue;  
                }  
                if(group.isExpand()){  
                   // count += childList.size();
                	int ntype = group.typeId;
                    for(int j=0;j<childList.size();j++){   
                      if(childList.get(j).type == ntype)
                      count++;  
                     }                 	
                }  
            } 
	          
            return count;              
        }  
        @Override  
        public Object getItem(int index) {  
            if(groupList == null || groupList.size() <= 0){  
                return null;  
            }  
            int count = 0 ;  
            for(int i=0;i<groupList.size();i++){  
            	FriendType group = groupList.get(i);  
                if(index == count){  
                    return group;  
                }  
                count++;  
                if(!group.isExpand()){  
                    continue;  
                }  
//                List<ChildBean> childList = group.getChildList();  
                if(childList == null){  
                    continue;  
                } 
            	int ntype = group.typeId;                
                for(int j=0;j<childList.size();j++){  
                    if(childList.get(j).type != ntype)
                        continue;                
                    if(index == count){
                        Friend child = childList.get(j);                     	
                        return child;  
                    } 
      
                    count++;  
                }  
            }  
            return null;  
        }  
        @Override  
        public long getItemId(int itemid) {  
            return itemid;  
        }  
        @Override  
        public View getView(final int position, View convertView, ViewGroup parent) {  
            ViewHolder holder = null;  
            if(convertView == null){  
                holder = new ViewHolder();  
                int type = getItemViewType(position);  
                if(type == VIEW_TYPE_GROUP){  
                    convertView = new GroupView(context, parent);  
                    holder.groupView = (GroupView)convertView;  
                }else{  
                    convertView = new ChildView(context, parent);  
                    holder.childView = (ChildView)convertView;  
                } 
                convertView.setTag(holder);  
            }else{  
                holder = (ViewHolder)convertView.getTag();  
            }  
            int type = getItemViewType(position);  
            if(type == VIEW_TYPE_GROUP){  
                final GroupView groupView = holder.groupView;  
                final FriendType groupBean = (FriendType)getItem(position);  
                groupView.setName(groupBean.getName());  
                groupView.setExpand(groupBean.isExpand());  
                groupView.setOnClickListener(new View.OnClickListener() {  
                    @Override  
                    public void onClick(View v) {  
                        groupBean.setExpand(!groupBean.isExpand());  
                        notifyDataSetChanged();  
                        if(groupListener != null){  
                            groupListener.onGroupClicked(groupView, groupBean, position);  
                        }  
                    }  
                });  
                return groupView;  
            }else{  
                final ChildView childView = holder.childView;  
                final Friend childBean = (Friend)getItem(position);  
                childView.setName(childBean.getName());  
                childView.setOnClickListener(new View.OnClickListener() {  
                    @Override  
                    public void onClick(View v) {  
                        if(childListener != null){  
                            childListener.onChildClicked(childView, childBean, position);  
                        }  
                    }  
                });  
                return childView;  
            }  
        } 
        
        @Override  
        public int getItemViewType(int position) {  
            Object item = getItem(position);  
            if(item instanceof FriendType){  
                return VIEW_TYPE_GROUP;  
            }else{  
                return VIEW_TYPE_CHILD;  
            }  
        }  
        @Override  
        public int getViewTypeCount() {  
            return 2;  
        }  
          
        public void setGroupListener(OnGroupClickListener groupListener) {  
            this.groupListener = groupListener;  
        }  
          
        public void setChildListener(OnChildClickListener childListener) {  
            this.childListener = childListener;  
        }  
          
        public void destroy(){  
            this.context = null;  
            this.groupList = null;  
            this.groupListener = null;  
            this.childListener = null;  
        }  
          
        private class ViewHolder{  
            GroupView groupView;  
            ChildView childView;  
        }  
 
      
    private static class GroupView extends RelativeLayout {  
        private TextView countView; 
        private TextView nameView;         
        private ImageView arrowView;  
        public GroupView(Context context, ViewGroup parent) {  
            super(context);  
            init(parent);  
        } 

        private void init(ViewGroup parent) {  
            final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext());  
            View v = mLayoutInflater.inflate(R.layout.layout_item_treegroup, parent, false);  
            addView(v);  
            nameView = (TextView)v.findViewById(R.id.groupName);  
            countView = (TextView)v.findViewById(R.id.groupCount); 
            arrowView = (ImageView)v.findViewById(R.id.treeIcon);  
        }  
        public void setName(String name) {  
            this.nameView.setText(name);  
        }  
        public void setExpand(boolean expand){  
            if(expand){  
                arrowView.setBackgroundResource(R.drawable.expand);  
            }else{  
                arrowView.setBackgroundResource(R.drawable.normal);  
            }  
        }  
    }  
      
    private static class ChildView extends RelativeLayout {  
        private TextView nameView;
        private TextView infoView; 
        private ImageView imgView;        
        public ChildView(Context context,  ViewGroup parent) {  
            super(context);  
            init(parent);  
        }  
        private void init(ViewGroup parent) {  
            final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext());  
            View v = mLayoutInflater.inflate(R.layout.layout_item_child, parent, false);  
            addView(v);  
           // nameView = (TextView)v.findViewById(R.id.username); 
            imgView = (ImageView)v.findViewById(R.id.contactIcon);   		
            nameView = (TextView)v.findViewById(R.id.name);
            infoView = (TextView)v.findViewById(R.id.description);
            
        }  
        public void setName(String name) {  
            this.nameView.setText(name);  
        }  
    } 

    
    private static interface OnGroupClickListener{  
        public void onGroupClicked(GroupView view, FriendType group, int position);  
    }  
      
    private static interface OnChildClickListener{  
        public void onChildClicked(ChildView view, Friend child, int position);  
    }    
}  