
package com.lianxin.pubqq.chat;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
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.DefineUser;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.list.bean.Depart;
import com.lianxin.panqq.list.bean.DepartMent;
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.FriendAdpter;
import com.lianxin.pubqq.main.adpter.GroupAdpter;
import com.lianxin.pubqq.main.adpter.RecentAdpter;
import com.lianxin.pubqq.main.adpter.TreeAdpter;
import com.lianxin.pubqq.main.adpter.TreeAdpter.ChildView;
import com.lianxin.pubqq.main.adpter.TreeAdpter.GroupView;
import com.lianxin.pubqq.main.adpter.TreeAdpter.OnMyChildClickListener;
import com.lianxin.pubqq.main.adpter.TreeAdpter.OnMyGroupClickListener;
import com.lianxin.panqq.main.bean.ListUtil;
import com.lianxin.panqq.main.bean.RecentItem;
import com.lianxin.panqq.main.widght.MyListView;

public class PickFriendActivity extends BaseActivity implements OnClickListener {

	private ListView listView;
    private ExpandableListView exlistView;	
    protected static final int LOOKTYPE_RECENT = 1;
    protected static final int LOOKTYPE_FRIEND = 2;	

    protected static final int LOOKTYPE_CONTACT = 3;   //来自通讯簿
    protected static final int LOOKTYPE_NEARBY = 4;   //来自附近的人	
    protected static final int LOOKTYPE_DEPART = 5;
    protected static final int LOOKTYPE_PARTY = 6;
    protected static final int LOOKTYPE_CROWD = 7;
    protected static final int LOOKTYPE_CLASS = 8;
    protected static final int LOOKTYPE_GROUP = 9;
    public String deaultTitle;// 消息来自    
    protected int mType = 0;
	private Button btn_input;	
	private EditText txt_input;	    
   // private RelativeLayout lay_pick_friend;
	//private RelativeLayout lay_pick_depart;	
	public String strUserId;// 用户登录ID	
	private int UserId = 0;// 用户登录ID	
	private int bakUserId = 0;// 用户登录ID	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int type = getIntent().getIntExtra("type", 0);	
		mType = type;
		if (type == 0) {
		    setContentView(R.layout.activity_pick_recent);
		    LookFromRecent();		
		}else if (type == LOOKTYPE_RECENT) {
		    setContentView(R.layout.activity_pick_recent);
		    LookFromRecent();		    
		} else if (type == LOOKTYPE_FRIEND) {
		    setContentView(R.layout.activity_pick_friend);
		    LookFromFriend();		    
		} else if (type == LOOKTYPE_DEPART) {
		    setContentView(R.layout.activity_pick_friend);
		    LookFromDepart();		    
		} else if (type == LOOKTYPE_CROWD) {
		    setContentView(R.layout.activity_pick_friend);
		    LookFromCrowd();		    
		} else if (type == LOOKTYPE_CLASS) {
		    setContentView(R.layout.activity_pick_friend);
		    LookFromClass();		    
		} else if (type == LOOKTYPE_CONTACT) {
		    setContentView(R.layout.activity_pick_contact);
		}
		ImageView img_back = (ImageView) findViewById(R.id.img_back);	
		img_back.setOnClickListener(this);		

	}

	protected void LookFromFriend() {
		TextView txt_title = (TextView) findViewById(R.id.txt_title);
		txt_title.setText("好友列表");			
		exlistView = (ExpandableListView) findViewById(R.id.exlist);		
		FriendAdpter friendAdpter = new FriendAdpter(this, GloableParams.FriendTypes, GloableParams.Friends);		
		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 LookFromRecent() {
		TextView txt_title = (TextView) findViewById(R.id.txt_title);
		txt_title.setText(deaultTitle);	
		btn_input = (Button) findViewById(R.id.btn_input);		
		txt_input = (EditText) findViewById(R.id.tv_user_input);		
		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);
		//btn_input.setOnClickListener(this); 
		btn_input.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v) {
        		strUserId = txt_input.getText().toString().trim();
        		if (TextUtils.isEmpty(strUserId)){
        			Utils.showShortToast(PickFriendActivity.this, "请填写用户账号！");
        			txt_input.requestFocus();			
                    return;
        	    }  
        		if (strUserId.length() < 5){
        			Utils.showShortToast(PickFriendActivity.this, "错误的用户账号！");
        			txt_input.requestFocus();	
                    return;
        	    } 
            	if(!Utils.isNumber(strUserId)){
        			Utils.showShortToast(PickFriendActivity.this, "错误的用户账号！");
        			txt_input.requestFocus();	
                    return;
        	    }
                //m_strUserId =  strUserId;      		
            	UserId=Integer.parseInt(strUserId); 
            	if( UserId < 10000 || UserId > 99999999){
        			Utils.showShortToast(PickFriendActivity.this, "错误的用户账号！");
        			txt_input.requestFocus();	
                    return;
        	    }
            	if( UserId == bakUserId ){
        			Utils.showShortToast(PickFriendActivity.this, "重复的用户账号！");
        			txt_input.requestFocus();	
                    return;
        	    }           	
            	bakUserId = UserId;
            	onUseridTextInput( UserId );            	
		   }
        });		
	}	

	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, DefineUser.TYPE_MYCLASS);		
		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		
		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, DefineUser.TYPE_CROWD);		
		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();
		}*/
	}	

	protected void onUseridTextInput( int destid ) {

	}	

	protected void initExtendData() {

	}	

	protected void postNewIntent( int type ) {
		Intent intent;		
		intent = new Intent(this, PickFriendActivity.class);
		intent.putExtra("type", type);		
		startActivity(intent);
	}	
	
	public void back(View view) {
		finish();
	}

	@Override
	public void onClick(View arg0) {
		Intent intent;		
		int id = arg0.getId();
		if (id == R.id.img_back) {
			if (mType == LOOKTYPE_RECENT) {
				finish(this);
			} else {
				postNewIntent(LOOKTYPE_RECENT);
			}
		} else if (id == R.id.rl_pick_friend) {
			postNewIntent(LOOKTYPE_FRIEND);
		} else if (id == R.id.rl_pick_depart) {
			postNewIntent(LOOKTYPE_DEPART);
		} else if (id == R.id.rl_pick_crowd) {
			postNewIntent(LOOKTYPE_CROWD);
		} else if (id == R.id.rl_pick_class) {
			postNewIntent(LOOKTYPE_CLASS);
		} else if (id == R.id.btn_input) {
		}		
	}

}
