
package com.lianxin.pubqq.chat;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ListView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.chat.entity.EMMessage;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.BaseActivity;

import com.lianxin.panqq.list.bean.EMGroup;
import com.lianxin.panqq.list.bean.Friend;
import com.lianxin.panqq.list.bean.Member;
import com.lianxin.panqq.list.utils.FriendUtils;

import com.lianxin.pubqq.main.adpter.RecentAdpter;

import com.lianxin.panqq.main.bean.ListUtil;

import com.lianxin.pubqq.setup.adpter.PickGroupAdpter;
//import com.lianxin.panqq.common.DefineUser;

public class PickGroupsActivity extends BaseActivity implements OnClickListener {

	private ListView listView;
    private ExpandableListView exlistView;	
    private List<EMGroup> PickGroups = GloableParams.MyCrowds;    

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		    setContentView(R.layout.activity_pick_groups);

		ImageView img_back = (ImageView) findViewById(R.id.img_back);	
		img_back.setOnClickListener(this);		

		initView();
		initRecent();		
	}

	protected void initView() {
		TextView txt_title = (TextView) findViewById(R.id.txt_title);
		txt_title.setText("选择我的分类");			
		exlistView = (ExpandableListView) findViewById(R.id.exlist);		
		PickGroupAdpter friendAdpter = new PickGroupAdpter(this, PickGroups);		
		exlistView.setAdapter(friendAdpter); //给他设置adapter，这里getActivity（），如果在Activity中用就是this		
		exlistView.setGroupIndicator(getResources().getDrawable(R.drawable.expander_ic_folder));
		ListUtil.setListViewHeightBasedOnChildren(exlistView);
	//	ListUtil.setListViewHeightBasedOnChildren(FriendView);		
		exlistView.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            	Friend child =	FriendUtils.getChild(groupPosition,  childPosition); 
        		int nChatId = child.getUserId();
        		String strName = child.getName();			
        		int nChatType = EMMessage.ChatType.ChatType_USER;
				onListItemSelected(nChatId,nChatType);  	            	
                return true;
            }
        });
		exlistView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int arg0) {
				ListUtil.setListViewHeightBasedOnChildren(exlistView);
			}
		});			
		exlistView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			@Override
			public void onGroupCollapse(int arg0) {
				ListUtil.setListViewHeightBasedOnChildren(exlistView);
			}
		});	
	}	

	protected void initRecent() {
		//TextView txt_title = (TextView) findViewById(R.id.txt_title);
		//txt_title.setText("转发至");		
		listView = (ListView) findViewById(R.id.list);
		RecentAdpter adpter = new RecentAdpter(this, GloableParams.RecentList);		
		listView.setAdapter(adpter);
		ListUtil.setListViewHeightBasedOnChildren1(listView);
	/*	ScrollView qqScroll = (ScrollView) findViewById(R.id.qq_scroll);
		qqScroll.smoothScrollTo(0, 0);		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//onListItemClick(position);
				RecentItem conversation = GloableParams.RecentList.get(position);
				int nChatType = conversation.getChatType();
				int nChatId = conversation.getUserId();	
				onListItemSelected(nChatId,nChatType);				
			}
		});*/
			
	}		
/*	protected void LookFromRecent() {
		TextView txt_title = (TextView) findViewById(R.id.txt_title);
		txt_title.setText("转发至");		
		listView = (ListView) findViewById(R.id.list);
		RecentAdpter adpter = new RecentAdpter(this, GloableParams.RecentList);		
		listView.setAdapter(adpter);
		ListUtil.setListViewHeightBasedOnChildren1(listView);
		ScrollView qqScroll = (ScrollView) findViewById(R.id.qq_scroll);
		qqScroll.smoothScrollTo(0, 0);		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//onListItemClick(position);
				RecentItem conversation = GloableParams.RecentList.get(position);
				int nChatType = conversation.getChatType();
				int nChatId = conversation.getUserId();	
				onListItemSelected(nChatId,nChatType);				
			}
		});
		RelativeLayout lay_pick_friend = (RelativeLayout) findViewById(R.id.rl_pick_friend);
		RelativeLayout lay_pick_depart = (RelativeLayout) findViewById(R.id.rl_pick_depart);
		RelativeLayout lay_pick_crowd = (RelativeLayout) findViewById(R.id.rl_pick_crowd);
		RelativeLayout lay_pick_class = (RelativeLayout) findViewById(R.id.rl_pick_class);		
		lay_pick_friend.setOnClickListener(this); 
		lay_pick_depart.setOnClickListener(this);
		lay_pick_crowd.setOnClickListener(this); 
		lay_pick_class.setOnClickListener(this);		
	}	

	protected void LookFromClass() {
		TextView txt_title = (TextView) findViewById(R.id.txt_title);
		txt_title.setText("同学列表");			
		exlistView = (ExpandableListView) findViewById(R.id.exlist);		
		GroupAdpter classAdpter = new GroupAdpter(this, GloableParams.MyClasss);		
		exlistView.setAdapter(classAdpter); //给他设置adapter，这里getActivity（），如果在Activity中用就是this		
		exlistView.setGroupIndicator(getResources().getDrawable(R.drawable.expander_ic_folder));
		ListUtil.setListViewHeightBasedOnChildren(exlistView);
		exlistView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int arg0) {
				ListUtil.setListViewHeightBasedOnChildren(exlistView);
			}
		});			
		exlistView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			@Override
			public void onGroupCollapse(int arg0) {
				ListUtil.setListViewHeightBasedOnChildren(exlistView);
			}
		});	
		exlistView.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        		Member person = GloableParams.MyClasss.get(groupPosition).members.get(childPosition);;
        		int nChatId = person.getUserId();
        		String strName = person.getName();			
        		int nChatType = EMMessage.ChatType.ChatType_USER;
				onListItemSelected(nChatId,nChatType);        		
        		return true;
            }
        });	
		exlistView.setOnGroupClickListener(new OnGroupClickListener() {
            int ret = 0; 			
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            //    if(curTouchPosx < 250)
                if(v.getLeft() < 250)
                     return false;
        		EMGroup person = GloableParams.MyClasss.get(groupPosition);
        		int nChatId = person.getId();
        		String nChatName = person.getName();
        		int nChatType = EMMessage.ChatType.ChatType_CLASS;                
				onListItemSelected(nChatId,nChatType);      
                return true;                             
            }
        });			
	}	

	protected void LookFromDepart() {
		TextView txt_title = (TextView) findViewById(R.id.txt_title);
		txt_title.setText("部门列表");			
		MyListView treeView = (MyListView) findViewById(R.id.treeview);		
		TreeAdpter departAdpter = new TreeAdpter(this, GloableParams.Departs, GloableParams.DepartMents);		
		treeView.setAdapter(departAdpter); //给他设置adapter，这里getActivity（），如果在Activity中用就是this		
		//listView.setGroupIndicator(getResources().getDrawable(R.drawable.expander_ic_folder));
		ListUtil.setListViewHeightBasedOnChildren(treeView);
		departAdpter.setGroupListener(new OnMyGroupClickListener(){  			 
			@Override
			public boolean  onGroupClicked(GroupView view, Depart group,int position) {
        		int nChatId = group.getId();
        		String strName = group.getName();			
        		int nChatType = EMMessage.ChatType.ChatType_DEPART;
				onListItemSelected(nChatId,nChatType);					
	            return true;					
			}
			@Override
			public boolean  onGroupClicked1(GroupView view, Depart group,int position) {
        		int nChatId = group.getId();
        		String strName = group.getName();			
        		int nChatType = EMMessage.ChatType.ChatType_DEPART;
				onListItemSelected(nChatId,nChatType);					
	            return true;					
			}			

        });  
		departAdpter.setChildListener(new OnMyChildClickListener(){  
			@Override
			public void onChildClicked(ChildView view, DepartMent child,int position) {
        		int nChatId = child.getId();
        		String strName = child.getName();			
        		int nChatType = EMMessage.ChatType.ChatType_USER;
				onListItemSelected(nChatId,nChatType);  				
			} 
			@Override
			public void onChildClicked1(ChildView view, DepartMent child,int position) {
        		int nChatId = child.getId();
        		String strName = child.getName();			
        		int nChatType = EMMessage.ChatType.ChatType_USER;
				onListItemSelected(nChatId,nChatType);
			}  			
        }); 
	}	
	
	protected void LookFromCrowd() {
		TextView txt_title = (TextView) findViewById(R.id.txt_title);
		txt_title.setText("群友列表");			
		exlistView = (ExpandableListView) findViewById(R.id.exlist);		
		GroupAdpter crowdAdpter = new GroupAdpter(this, GloableParams.MyCrowds);		
		exlistView.setAdapter(crowdAdpter); //给他设置adapter，这里getActivity（），如果在Activity中用就是this		
		exlistView.setGroupIndicator(getResources().getDrawable(R.drawable.expander_ic_folder));
		ListUtil.setListViewHeightBasedOnChildren(exlistView);
		exlistView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int arg0) {
				ListUtil.setListViewHeightBasedOnChildren(exlistView);
			}
		});			
		exlistView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			@Override
			public void onGroupCollapse(int arg0) {
				ListUtil.setListViewHeightBasedOnChildren(exlistView);
			}
		});	
		exlistView.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        		Member person = GloableParams.MyCrowds.get(groupPosition).members.get(childPosition);;
        		int nChatId = person.getUserId();
        		String strName = person.getName();			
        		int nChatType = EMMessage.ChatType.ChatType_USER;
				onListItemSelected(nChatId,nChatType);        		
        		return true;
            }
        });	
		exlistView.setOnGroupClickListener(new OnGroupClickListener() {
            int ret = 0; 			
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            //    if(curTouchPosx < 250)
                if(v.getLeft() < 250)
                     return false;
        		EMGroup person = GloableParams.MyCrowds.get(groupPosition);
        		int nChatId = person.getId();
        		String nChatName = person.getName();
        		int nChatType = EMMessage.ChatType.ChatType_CROWD;                
				onListItemSelected(nChatId,nChatType);      
                return true;                             
            }
        });			
	}	*/
	
	protected void onListItemClick(int position) {
	/*	if (position != 0) {
			setResult(RESULT_OK, new Intent().putExtra("username", contactAdapter.getItem(position)
					.getUsername()));
			finish();
		}*/
	}

	protected void onListItemSelected(int destid,int type) {
	/*	if (position != 0) {
			setResult(RESULT_OK, new Intent().putExtra("username", contactAdapter.getItem(position)
					.getUsername()));
			finish();
		}*/
	}	
	
	public void back(View view) {
		finish();
	}

	@Override
	public void onClick(View arg0) {
		Intent intent;		
		if (arg0.getId() == R.id.img_back) {
			finish(this);
			/*	case R.id.rl_pick_friend:
			intent = new Intent(this, PickFriendActivity.class);
			intent.putExtra("type", LOOKTYPE_FRIEND);
			startActivity(intent);
			break;
		case R.id.rl_pick_depart:
			intent = new Intent(this, PickFriendActivity.class);
			intent.putExtra("type", LOOKTYPE_DEPART);
			//intent.putExtra("type", 2);
			startActivity(intent);
			break;
		case R.id.rl_pick_crowd:
			intent = new Intent(this, PickFriendActivity.class);
			intent.putExtra("type", LOOKTYPE_CROWD);
			startActivity(intent);
			break;
		case R.id.rl_pick_class:
			intent = new Intent(this, PickFriendActivity.class);
			intent.putExtra("type", LOOKTYPE_CLASS);
			startActivity(intent);
			break;		*/
		}
	}

}
