 
package com.lianxin.pubqq.extend;
import java.util.List;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.lianxin.pubqq.main.adpter.DepartAdpter;
import com.lianxin.pubqq.main.adpter.DepartAdpter.ChildView;
import com.lianxin.pubqq.main.adpter.DepartAdpter.GroupView;
import com.lianxin.pubqq.main.adpter.DepartAdpter.OnMyChildClickListener;
import com.lianxin.pubqq.main.adpter.DepartAdpter.OnMyChildLongClickListener;
import com.lianxin.pubqq.main.adpter.DepartAdpter.OnMyGroupClickListener;
import com.lianxin.pubqq.main.adpter.DepartAdpter.OnMyGroupLongClickListener;
import com.lianxin.panqq.list.ExDepartListView;
import com.lianxin.panqq.main.bean.ListUtil;
import com.lianxin.panqq.picker.MenuDialog;
import com.lianxin.pubqq.R;
import com.lianxin.pubqq.activity.GroupSearchActivity;
import com.lianxin.pubqq.activity.InformListActivity;
import com.lianxin.pubqq.activity.MemberInfoActivity;
import com.lianxin.pubqq.activity.MytypeInfoActivity;
import com.lianxin.pubqq.activity.MytypeListActivity;
import com.lianxin.pubqq.activity.NewGroupActivity;
import com.lianxin.panqq.client.callback.listCallBack;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.DefineChat;
import com.lianxin.panqq.common.GloableParams;
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.utils.DepartMentUtils;
import com.lianxin.pubqq.chat.EMChatActivity;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.utils.CaptionUtils;
//import com.lianxin.panqq.chat.entity.EMMessage;

/**
 * 部门管理界面
 */
public class DepartActivity extends BaseActivity implements OnClickListener {
	private TextView txt_title,txt_right;
	private ImageView img_back, img_right; 	
    private ExDepartListView TreeView;     
    private ScrollView qqScroll=null;    
	//private TreeAdpter departAdpter;  
	private DepartAdpter departAdpter; 	
    private List<Depart> Departs = GloableParams.Departs; 
    private List<DepartMent> DepartMents = GloableParams.DepartMents; 
	private LinearLayout lay_right;
	private String  strCaption = "部门,搜索部门,部门通知,会议室,部门申请,新建部门,我的部门和同事,刷新我参加的部门"; //服务器各自定制的标题字符串
	private String[]  arrCaption = null; //标题字符串数组  
	private String  strTip = "刷新我参加的部门";	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_depart);
		
		initControl();
		initData();		
		initCaption();
		setListener();		
	}

	protected void initControl() {
		txt_title = (TextView) findViewById(R.id.txt_title);
		//txt_title.setText("用户查找");
		img_back = (ImageView) findViewById(R.id.img_back);
		img_back.setVisibility(View.VISIBLE);

		qqScroll = (ScrollView)findViewById(R.id.qq_scroll);		
	}
	
	protected void initData() {
		TreeView = (ExDepartListView) findViewById(R.id.treeview); //实例化expandableLIstView
		TreeView.setStatusListener(new listCallBack(){
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				Departs = GloableParams.Departs;  
				DepartMents = GloableParams.DepartMents;	
			}
			@Override
			public void onFailure(int errorCode, String errorMessage) {
				// TODO Auto-generated method stub			
			}
		});	
		TreeView.initData();			
	      //给adapter设置监听  
		departAdpter = new DepartAdpter(this, GloableParams.Departs, GloableParams.DepartMents);
		ListUtil.setListViewHeightBasedOnChildren(TreeView);	
		TreeView.setAdapter(departAdpter);
		TreeView.setDataAdapter(departAdpter);				
	}	
	
	protected void initView() {
		setAdpterListener();
		
		qqScroll = (ScrollView) findViewById(R.id.qq_scroll);
		qqScroll.smoothScrollTo(0, 0);		
	}

	protected void setListener() {
		img_back.setOnClickListener(this);		
 
		findViewById(R.id.layout_addfriend).setOnClickListener(this);
		findViewById(R.id.layout_search).setOnClickListener(this);
		findViewById(R.id.layout_group).setOnClickListener(this);
		findViewById(R.id.layout_public).setOnClickListener(this);
		findViewById(R.id.layout_refresh).setOnClickListener(this);		
	}	

	protected void initCaption() {
		//加载服务器定制标题字符串
		//loadCustomCaption();
	    String sCaption = CaptionUtils.LoadCaption( "IDS_DEPART_WELCOME" );
	    if(!TextUtils.isEmpty(sCaption)&&sCaption.length() > 20)
	        arrCaption = CaptionUtils.SplitCaption( sCaption );	
	    else if(!TextUtils.isEmpty(strCaption)&&strCaption.length() > 20)
	        arrCaption = CaptionUtils.SplitCaption( strCaption );	    
		//如果服务器有自定义字符串    
		if( arrCaption != null && arrCaption.length > 7 ){
			//txt_title.setText(arrCaption[0]);
			((TextView)findViewById(R.id.txt_search)).setText(arrCaption[1]);			
			((TextView)findViewById(R.id.cap_addfriend)).setText(arrCaption[2]);
			((TextView)findViewById(R.id.cap_group)).setText(arrCaption[3]);			
			((TextView)findViewById(R.id.cap_public)).setText(arrCaption[4]);			
			((TextView)findViewById(R.id.cap_table)).setText(arrCaption[6]);
			strTip = arrCaption[7];			
		}
	}		
	
	private void setAdpterListener() {
		// TODO Auto-generated method stub
		departAdpter.setGroupListener(new OnMyGroupClickListener(){  
			 
			@Override
			public boolean  onGroupClicked(GroupView view, Depart group,
					int position) {              
					Intent intent = new Intent(DepartActivity.this, EMChatActivity.class);
					intent.putExtra("Chat_ID", group.getId());
					int nChatType = DefineChat.ChatType_DEPART;					
					intent.putExtra("Chat_Type", nChatType);
					intent.putExtra("Chat_Name", group.getName());
					//startActivity(intent);	
					Utils.start_Activity(context,intent); 				
	                return true;					
			}
			@Override
			public boolean  onGroupClicked1(GroupView view, Depart group,
					int position) {
				Intent intent = new Intent(DepartActivity.this, MytypeInfoActivity.class);
				intent.putExtra("TypeID", group.getId());
				//intent.putExtra("TypeType", EMMessage.ChatType.ChatType_DEPART);			
				//intent.putExtra("TypeName", group.getName());
				intent.putExtra("Info_GroupType", DefineUser.TYPE_DEPART);					
				//intent.putExtra("Info_UserId", group.getId());
				intent.putExtra("Info_NickName", group.getName());
			//	intent.putExtra("Info_Image", person.imageid);
				//intent.putExtra("Info_Image",1);
				//int mypower = DepartMentUtils.getDepartPower(group.getId());				
				//intent.putExtra("Info_Power", mypower);
				//intent.putExtra("Info_Index", group.index);			
				//startActivity(intent);	
				Utils.start_Activity(context,intent); 				
	                return true;					
			}			

        });  
		departAdpter.setChildListener(new OnMyChildClickListener(){  

			@Override
			public void onChildClicked(ChildView view, DepartMent child,
					int position) {
				Toast.makeText(context, "I am " + child.getName(),Toast.LENGTH_SHORT).show();
			
				Intent intent = new Intent(DepartActivity.this, EMChatActivity.class);
				intent.putExtra("Chat_ID", child.getId());
				intent.putExtra("Chat_Type", DefineChat.ChatType_USER);
				intent.putExtra("Chat_Name", child.getName());
				//startActivity(intent);
				Utils.start_Activity(context,intent); 				
			} 
			@Override
			public void onChildClicked1(ChildView view, DepartMent child,
					int position) {

					Intent intent = new Intent(DepartActivity.this, MemberInfoActivity.class);
					intent.putExtra("TypeID", child.type);
					intent.putExtra("TypeType", DefineChat.ChatType_DEPART);			
				//	intent.putExtra("TypeName", nChatName);	
					intent.putExtra("Info_GroupType", DefineUser.TYPE_DEPART);					
					intent.putExtra("Info_UserId", child.getId());
					intent.putExtra("Info_NickName", child.getName());
					intent.putExtra("Info_Image", child.imageid);			
					intent.putExtra("Info_Power", child.power);
					intent.putExtra("Info_Index", child.index);			
					//startActivity(intent);
					Utils.start_Activity(context,intent); 				
			}  			
        }); 
		departAdpter.setChildLongListener(new OnMyChildLongClickListener(){  

			@Override
			public void onChildLongClicked(ChildView view, DepartMent child,
					int position) {

				MenuDialog.Builder builder = new MenuDialog.Builder(DepartActivity.this);
				builder.setItems(new String[] { "刷新同事", "刷新部门", "保密设置" , "接待设置", "退出部门" , "申请创建" , "申请主管"},
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {

								Toast.makeText(context, "I am " + which,
										Toast.LENGTH_SHORT).show();
							}
						}).show();				
			}  
        });
		departAdpter.setGroupLongListener(new OnMyGroupLongClickListener(){  

			@Override
			public void onGroupLongClicked(GroupView view, Depart group, int position){
				MenuDialog.Builder builder = new MenuDialog.Builder(DepartActivity.this);
				builder.setItems(new String[] { "新建部门", "修改部门", "合并部门" , "移动部门" , "删除部门", "添加成员" , "清空成员"},
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {

								Toast.makeText(context, "I am " + which,
										Toast.LENGTH_SHORT).show();
							}
						}).show();					
			}  
        }); 		
	}


	@Override
	public void onClick(View v) {
		Intent intent = new Intent();		
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(DepartActivity.this);
			//finish();
			//finish(DepartActivity.this);
		} else if (id == R.id.layout_search) {// 搜索好友及公众号
			intent.setClass(DepartActivity.this, GroupSearchActivity.class);
			intent.putExtra("REQUEST_FROM", 0);
			intent.putExtra("SEARCH_TYPE", DefineUser.TYPE_PARTY);
			startActivityForResult(intent, 20);
		} else if (id == R.id.layout_addfriend) {
			intent.setClass(DepartActivity.this, NewGroupActivity.class);
			intent.putExtra("Info_GroupType", DefineUser.TYPE_PARTY);
			Utils.start_Activity(context, intent);
		} else if (id == R.id.layout_group) {
			intent.setClass(DepartActivity.this, MytypeListActivity.class);
			intent.putExtra("TypeID", 10058);
			intent.putExtra("TypeType", DefineUser.TYPE_DEPART);
			//startActivity(intent);
			Utils.start_Activity(context, intent);
		} else if (id == R.id.layout_public) {
			intent.setClass(DepartActivity.this, InformListActivity.class);
			Utils.start_Activity(context, intent);
		} else if (id == R.id.layout_refresh) {//Toast.makeText(context, "刷新我参加的部门", Toast.LENGTH_SHORT).show();
			Toast.makeText(context, strTip, Toast.LENGTH_SHORT).show();
			TreeView.refreshData();
			departAdpter.setData(Departs, DepartMents);
			departAdpter.notifyDataSetChanged();
		}
	} 

}