package com.lianxin.pubqq.main.adpter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
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.UserheadUtils;
import com.lianxin.panqq.list.bean.Depart;
import com.lianxin.panqq.list.bean.DepartMent;
import com.lianxin.panqq.list.adpter.ExTreeAdapter;
import com.lianxin.panqq.utils.ImagesUtils;
  
public class DepartAdpter extends ExTreeAdapter{  
        
        private static final int VIEW_TYPE_GROUP = 0;  
        private static final int VIEW_TYPE_CHILD = 1;  
 
        private List<Depart> groupList;  
        private List<DepartMent> childList;  
      
        private OnMyGroupClickListener groupListener;  
        private OnMyChildClickListener childListener;
        private OnMyGroupLongClickListener groupLongListener;  
        private OnMyChildLongClickListener childLongListener;        
        private float curTouchPosx;          
        public DepartAdpter(Context context){  
    	    super(context);	            
        } 

        public DepartAdpter(Context context, List<Depart> groupList, List<DepartMent> childList){  
    	    super(context);	
            this.groupList = groupList;  
            this.childList = childList;            
        }      
        
    	@Override
    	public void setData(Object object1, Object object2) {
            this.groupList = (List<Depart>)object1;  
            this.childList = (List<DepartMent>)object2;    
    	} 
   
        @Override  
        public int getCount() {  
            if(groupList == null || groupList.size() <= 0){  
                return 0;  
            }  
            int count = 0;  
   //         for(depart group : groupList){ 
            for(int i=0;i<groupList.size();i++){             	
                Depart group = groupList.get(i);
                if(group.type!=0){  
                     continue;  
                 }             	
                count ++;  
                if(!group.isExpand()){  
                    continue;  
                }
            	int ntype = group.Id;                
                if(true){//第二层
                    for(int m=0;m<groupList.size();m++){             	
                        Depart group1 = groupList.get(m);
                        if(group1.type!=ntype){  
                             continue;  
                         }             	
                        count ++;  
                        if(!group1.isExpand()){  
                            continue;  
                        } 
                    	int mtype = group1.Id;                        
                        if(childList == null){  
                            continue;  
                        } 
                        for(int j=0;j<childList.size();j++){   
                            if(childList.get(j).type == mtype)
                            count++;  
                           }                        
                    }
                }
                if(childList == null){  
                    continue;  
                }  
 
                    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++){             	
            	Depart group = groupList.get(i);
                if(group.type!=0){  
                    continue;  
                }            	
                if(index == count){  
                    return group;  
                }  
                count++;  
                if(!group.isExpand()){  
                    continue;  
                } 
            	int ntype = group.Id;                
                if(true){//第二层
                    for(int m=0;m<groupList.size();m++){             	
                        Depart group1 = groupList.get(m);
                        if(group1.type!=ntype){  
                             continue;  
                         } 
                        if(index == count){  
                            return group1;  
                        }                        
                        count ++;  
                        if(!group1.isExpand()){  
                            continue;  
                        } 
                    	int mtype = group1.Id;                        
                        if(childList == null){  
                            continue;  
                        } 
                        for(int n=0;n<childList.size();n++){   
                           // if(childList.get(j).type == mtype)
                           // count++; 
                            if(childList.get(n).type != mtype)
                                continue;                
                            if(index == count){
                            	DepartMent child = childList.get(n);                     	
                                return child;  
                            } 
              
                            count++;                              
                           }                        
                    }
                }                
                if(childList == null){  
                    continue;  
                } 
 //           	int ntype = group.Id;                
                for(int j=0;j<childList.size();j++){  
                    if(childList.get(j).type != ntype)
                        continue;                
                    if(index == count){
                    	DepartMent child = childList.get(j);                     	
                        return child;  
                    } 
      
                    count++;  
                } 
            }  
            return null;  
        } 
        
        public int getItemLevel(int index) {  
            if(groupList == null || groupList.size() <= 0){  
                return 0;  
            }  
            int count = 0 ;  
            for(int i=0;i<groupList.size();i++){             	
            	Depart group = groupList.get(i);
                if(group.type!=0){  
                    continue;  
                }            	
                if(index == count){  
                    return 0;  
                }  
                count++;  
                if(!group.isExpand()){  
                    continue;  
                } 
            	int ntype = group.Id;                
                if(true){//第二层
                    for(int m=0;m<groupList.size();m++){             	
                        Depart group1 = groupList.get(m);
                        if(group1.type!=ntype){  
                             continue;  
                         } 
                        if(index == count){  
                            return 1;  
                        }                        
                        count ++;  
                        if(!group1.isExpand()){  
                            continue;  
                        } 
                    	int mtype = group1.Id;                        
                        if(childList == null){  
                            continue;  
                        } 
                        for(int n=0;n<childList.size();n++){   
                           // if(childList.get(j).type == mtype)
                           // count++; 
                            if(childList.get(n).type != mtype)
                                continue;                
                            if(index == count){
                            	//department child = childList.get(n);                     	
                                return 2;  
                            } 
              
                            count++;                              
                           }                        
                    }
                }                
                if(childList == null){  
                    continue;  
                } 
 //           	int ntype = group.Id;                
                for(int j=0;j<childList.size();j++){  
                    if(childList.get(j).type != ntype)
                        continue;                
                    if(index == count){
                    //	department child = childList.get(j);                     	
                        return 1;  
                    } 
      
                    count++;  
                } 
            }  
            return 0;  
        }        
        @Override  
        public long getItemId(int itemid) {  
            return itemid;  
        }  
        @Override  
        public View getView(final int position, View convertView, ViewGroup parent) {  
            ViewHolder holder = null;  
            int type = getItemViewType(position);            
            if(convertView == null){  
                holder = new ViewHolder();  
                //int type = getItemViewType(position);  
                if(type == VIEW_TYPE_GROUP){  
                    convertView = new GroupView(mContext, parent);  
                    holder.groupView = (GroupView)convertView;  
                }else{  
                    convertView = new ChildView(mContext, parent);  
                    holder.childView = (ChildView)convertView;  
                } 
                convertView.setTag(holder);  
            }else{  
                holder = (ViewHolder)convertView.getTag();  
            }   
            int level = getItemLevel(position);             
            if(type == VIEW_TYPE_GROUP){  
                final GroupView groupView = holder.groupView;  
                final Depart groupBean = (Depart)getItem(position);  
                groupView.setName(groupBean.getName());
            	int groupid = groupBean.Id;                 
            	int n1 = getSubCount( groupid); 
            	int n2 = getMemberCount( groupid); 
              	
                groupView.setCount("["+n1+"/"+n2+"]");                
                holder.groupView.arrowView.setPadding(20*level, 0, 0, 0);                 
                groupView.setExpand(groupBean.isExpand()); 
                groupView.setOnTouchListener(new OnTouchListener() {
        			@Override
        			public boolean onTouch(View v, MotionEvent event) {
        			//	Toast.makeText();
        				//获取点击事件相对控件左边的x轴坐标，即点击事件距离控件左边的距离
        				curTouchPosx = event.getX();//获得触摸x坐标
        			//	curTouchPosy = event.getY();//获得触摸y坐标				
        				return false;
        			}
        		});                
                groupView.setOnClickListener(new View.OnClickListener() {  
                    @Override  
                    public void onClick(View v) { 
                        if(curTouchPosx<150){
                            groupBean.setExpand(!groupBean.isExpand());  
                            notifyDataSetChanged(); 
                            return;
                        }                    	
                        boolean ret = false; 
                        if(groupListener != null){ 
                        	if(curTouchPosx<300)                        	
                        	    ret = groupListener.onGroupClicked1(groupView, groupBean, position); 
                        	else                       	
                        	    ret = groupListener.onGroupClicked(groupView, groupBean, position);                          	
                        } 
                        if(!ret){
                            groupBean.setExpand(!groupBean.isExpand());  
                            notifyDataSetChanged();  
                        }
                    }  
                }); 
   			    //长按头像，弹出菜单
                groupView.setOnLongClickListener(new OnLongClickListener() {
				    @Override
				    public boolean onLongClick(View v) {
	                       if(groupLongListener != null){  
	                    	   groupLongListener.onGroupLongClicked(groupView, groupBean, position);  
	                        }
					    return true;
				    }
			    });                  
                return groupView;  
            }else{  
                final ChildView childView = holder.childView;  
                final DepartMent childBean = (DepartMent)getItem(position);  
                childView.setName(childBean.getName()); 
            	int userid = childBean.userId;  
            	holder.childView.infoView.setText("["+userid+"]"); 
   
                int image = childBean.imageid;
                if(image>80) image = image%80;
                holder.childView.imgView.setImageResource(ImagesUtils.images[image]);            	
                long userhead = childBean.userhead;              
                if(userhead>0x10000)
                	UserheadUtils.showUserHead(userid,userhead,holder.childView.imgView,DefineUser.TYPE_DEPART);
                holder.childView.setPadding(20*level, 0, 0, 0);
                childView.setOnTouchListener(new OnTouchListener() {
        			@Override
        			public boolean onTouch(View v, MotionEvent event) {
        			//	Toast.makeText();
        				//获取点击事件相对控件左边的x轴坐标，即点击事件距离控件左边的距离
        				curTouchPosx = event.getX();//获得触摸x坐标
        			//	curTouchPosy = event.getY();//获得触摸y坐标				
        				return false;
        			}
        		});              
                childView.setOnClickListener(new View.OnClickListener() {  
                    @Override  
                    public void onClick(View v) {  
                        if(childListener != null){  
                        	if(curTouchPosx<300)                        	
                                childListener.onChildClicked1(childView, childBean, position); 
                        	else                        	
                                childListener.onChildClicked(childView, childBean, position);                         	
                        }  
                    }                     
                }); 
   			    //长按头像，移入黑名单
			    childView.setOnLongClickListener(new OnLongClickListener() {
				    @Override
				    public boolean onLongClick(View v) {
	                       if(childLongListener != null){  
	                            childLongListener.onChildLongClicked(childView, childBean, position);  
	                        }
					    return true;
				    }
			    });        
                return childView;  
            }  
        } 
        
        @Override  
        public int getItemViewType(int position) {  
            Object item = getItem(position);  
            if(item instanceof Depart){  
                return VIEW_TYPE_GROUP;  
            }else{  
                return VIEW_TYPE_CHILD;  
            }  
        }  
        @Override  
        public int getViewTypeCount() {  
            return 2;  
        }  

        public  int getSubCount(int type) {  
            if(groupList == null || groupList.size() <= 0){  
                return 0;  
            }  
            int count = 0;  
   //         for(depart group : groupList){ 
            for(int i=0;i<groupList.size();i++){             	
                Depart group = groupList.get(i);
                if(group.type != type){  
                     continue;  
                 }             	
                count ++;  
            	int ntype = group.Id;                
                if(true){//第二层
                    for(int m=0;m<groupList.size();m++){             	
                        Depart group1 = groupList.get(m);
                        if(group1.type!=ntype){  
                             continue;  
                         }             	
                        count ++;                    
                    }
                }
            }       
            return count;  
           // return 4;              
        }        

        public int getMemberCount(int type) {  
            if(groupList == null || groupList.size() <= 0){  
                return 0;  
            }  
            int count = 0;  
   //         for(depart group : groupList){ 
            for(int i=0;i<groupList.size();i++){             	
                Depart group = groupList.get(i);
                if(group.type!=type){  
                     continue;  
                 }             	
             //   count ++;  
            	int ntype = group.Id;                
                if(true){//第二层
                    for(int m=0;m<groupList.size();m++){             	
                        Depart group1 = groupList.get(m);
                        if(group1.type!=ntype){  
                             continue;  
                         }             	
                       // count ++;   
                    	int mtype = group1.Id;                        
                        if(childList == null){  
                            continue;  
                        } 
                        for(int j=0;j<childList.size();j++){   
                            if(childList.get(j).type == mtype)
                            count++;  
                           }                        
                    }
                }
                if(childList == null){  
                    continue;  
                }  
                for(int j=0;j<childList.size();j++){   
                    if(childList.get(j).type == ntype)
                        count++;  
                }                 	 
            } 
            for(int j=0;j<childList.size();j++){   
                if(childList.get(j).type == type)
                    count++;  
            }            
        
            return count;  
           // return 4;              
        }        
        
        public void setGroupListener(OnMyGroupClickListener groupListener) {  
            this.groupListener = groupListener;  
        }  
          
        public void setChildListener(OnMyChildClickListener childListener) {  
            this.childListener = childListener;  
        } 
        
        public void setGroupLongListener(OnMyGroupLongClickListener groupLongListener) {  
            this.groupLongListener = groupLongListener;  
        }  
          
        public void setChildLongListener(OnMyChildLongClickListener childLongListener) {  
            this.childLongListener = childLongListener;  
        }          
        
        public void destroy(){  
            this.mContext = null;  
            this.groupList = null;  
            this.groupListener = null;  
            this.childListener = null;  
        }  
          
        private class ViewHolder{  
            GroupView groupView;  
            ChildView childView;  
        }  
 //   }  
      
      
    public 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 setCount(String count) {  
            this.countView.setText(count);  
        }        
        public void setExpand(boolean expand){  
            if(expand){  
             //   arrowView.setBackgroundResource(R.drawable.expand);  
                arrowView.setImageResource(R.drawable.expand);                 
               
            }else{  
                arrowView.setImageResource(R.drawable.normal);  
            }  
        }  
    }  
      
    public 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.childlayout, parent, false); 
            View v = mLayoutInflater.inflate(R.layout.layout_item_treechild, 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);  
        }  
    } 

    public static interface OnMyGroupClickListener{  
        public boolean onGroupClicked(GroupView view, Depart group, int position);  
        public boolean onGroupClicked1(GroupView view, Depart group, int position);        
    }  
      
    public static interface OnMyChildClickListener{  
        public void onChildClicked(ChildView view, DepartMent child, int position);  
        public void onChildClicked1(ChildView view, DepartMent child, int position);        
    }  
    
    public static interface OnMyGroupLongClickListener{  
        public void onGroupLongClicked(GroupView view, Depart group, int position);  
    }  
      
    public static interface OnMyChildLongClickListener{  
        public void onChildLongClicked(ChildView view, DepartMent child, int position);  
    }

   
}  