/*杳信、企事业单位通用信息化平台
 * 作者：萧遥（真实姓名:段宗曜）
 * QQ:328969810
 * http:/www.pubqq.com
 */
package com.lianxin.pubqq.main;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.lianxin.pubqq.activity.InformListActivity;
import com.lianxin.pubqq.extend.PartyActivity;
import com.lianxin.pubqq.activity.GroupSearchActivity;
import com.lianxin.pubqq.activity.InviteListActivity;
import com.lianxin.pubqq.activity.MemberInfoActivity;
import com.lianxin.pubqq.activity.MytypeInfoActivity;
import com.lianxin.pubqq.chat.EMChatActivity;
//import com.lianxin.panqq.chat.entity.EMMessage;
import com.lianxin.panqq.client.callback.listCallBack;
import com.lianxin.pubqq.R;
import com.lianxin.panqq.list.ExDepartListView;
import com.lianxin.panqq.main.bean.ListUtil;
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.OnMyGroupClickListener;
import com.lianxin.panqq.list.bean.DepartMent;
import com.lianxin.panqq.list.bean.Depart;
//import com.lianxin.panqq.list.utils.DepartMentUtils;
import com.lianxin.panqq.common.DefineChat;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.utils.CaptionUtils;

public class Fragment_Depart extends Fragment implements OnClickListener {
	// 部门窗口
	private Activity ctx;
	private View layout;

    private ExDepartListView TreeView;     
    private ScrollView qqScroll=null;     
	private DepartAdpter departAdpter;    
    private List<Depart> Departs = GloableParams.Departs; 
    private List<DepartMent> DepartMents = GloableParams.DepartMents; 
	private String  strCaption = "部门,搜索部门,部门通知,会议室,部门申请,新建部门,我的部门和同事,刷新我参加的部门"; //服务器各自定制的标题字符串
	private String[]  arrCaption = null; //标题字符串数组   
	private String  strTip = "刷新我参加的部门"; //	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (layout == null) {
			ctx = this.getActivity();
			layout = ctx.getLayoutInflater().inflate(R.layout.fragment_depart,
					null);
			initData();	//先要初始化数据，否则下一步出错		
			initViews();
			initCaption();		
			setOnListener();
		} else {
			ViewGroup parent = (ViewGroup) layout.getParent();
			if (parent != null) {
				parent.removeView(layout);
			}
		}
		qqScroll = (ScrollView) layout.findViewById(R.id.qq_scroll);
		qqScroll.smoothScrollTo(0, 0);
		//qqScroll.scrollTo(0, 0);				
		return layout;
	}

	public void refresh() {	    
		departAdpter.notifyDataSetChanged();	    
	}	

	public void refreshNewUser() {
		
		Departs = GloableParams.Departs;  
		DepartMents = GloableParams.DepartMents; 
		departAdpter.setData(Departs, DepartMents);	    
		departAdpter.notifyDataSetChanged();	    
	}	
 
	private void initData() {
		TreeView = (ExDepartListView)layout.findViewById(R.id.treeview); //实例化expandableLIstView
	      //给adapter设置监听  		
		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(getActivity(), GloableParams.Departs, GloableParams.DepartMents);
		ListUtil.setListViewHeightBasedOnChildren(TreeView);	
		TreeView.setAdapter(departAdpter);
		TreeView.setDataAdapter(departAdpter);		
	}	
	
	private void initViews() {
			
		setAdpterListener();		

		qqScroll = (ScrollView) layout.findViewById(R.id.qq_scroll);
		qqScroll.smoothScrollTo(0, 0);
		//qqScroll.scrollTo(0, 0);		
	}

	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 );	    	    
		//如果服务器有自定义字符串
	    //TextView cap_group = (TextView)layout.findViewById(R.id.cap_group);	
	    //cap_group.setText(arrCaption[0]);
	    //((TextView)layout.findViewById(R.id.cap_group)).setText(arrCaption[0]);	    
		if( arrCaption != null && arrCaption.length > 7 ){
			//txt_title.setText(arrCaption[0]);
			((TextView)layout.findViewById(R.id.txt_search)).setText(arrCaption[1]);			
			((TextView)layout.findViewById(R.id.cap_addfriend)).setText(arrCaption[2]);
			((TextView)layout.findViewById(R.id.cap_group)).setText(arrCaption[3]);			
			((TextView)layout.findViewById(R.id.cap_public)).setText(arrCaption[4]);			
			((TextView)layout.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) {
				// TODO Auto-generated method stub
	           //    Toast.makeText(ctx, "I am "+ group.getName(), Toast.LENGTH_SHORT).show();               
					Intent intent = new Intent(getActivity(), EMChatActivity.class);
					intent.putExtra("Chat_ID", group.getId());
					int nChatType = DefineChat.ChatType_DEPART;					
					intent.putExtra("Chat_Type", nChatType);
					intent.putExtra("Chat_Name", group.getName());
					//getActivity().startActivity(intent);
					Utils.start_Activity(getActivity(),intent);					
	                return true;					
			}
			@Override
			public boolean  onGroupClicked1(GroupView view, Depart group,
					int position) {

				Intent intent = new Intent(getActivity(), 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);			
				//getActivity().startActivity(intent);
				Utils.start_Activity(getActivity(),intent);			
	                return true;					
			}			

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

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

     	
					Intent intent = new Intent(getActivity(), MemberInfoActivity.class);
					intent.putExtra("TypeID", child.type);
				//	intent.putExtra("TypeType", EMMessage.ChatType.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);			
					//getActivity().startActivity(intent);
					Utils.start_Activity(getActivity(),intent);					
			}  			
        }); 
	}

	private void setOnListener() {
		// TODO Auto-generated method stub
		layout.findViewById(R.id.layout_addfriend).setOnClickListener(this);
		layout.findViewById(R.id.layout_search).setOnClickListener(this);
		layout.findViewById(R.id.layout_group).setOnClickListener(this);
		layout.findViewById(R.id.layout_public).setOnClickListener(this);
		layout.findViewById(R.id.layout_refresh).setOnClickListener(this);		
	}	

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();		
		int id = v.getId();
		if (id == R.id.layout_search) {// 搜索好友及公众号

			intent.setClass(getActivity(), GroupSearchActivity.class);
			intent.putExtra("REQUEST_FROM", 0);
			intent.putExtra("SEARCH_TYPE", DefineUser.TYPE_DEPART);
			startActivityForResult(intent, 20);
		} else if (id == R.id.layout_addfriend) {
			intent.setClass(getActivity(), InformListActivity.class);
			//startActivity(intent);
			Utils.start_Activity(getActivity(), intent);
		} else if (id == R.id.layout_group) {
			intent.setClass(getActivity(), PartyActivity.class);
			//getActivity().startActivity(intent);
			Utils.start_Activity(getActivity(), intent);
		} else if (id == R.id.layout_public) {
			intent.setClass(getActivity(), InviteListActivity.class);
			intent.putExtra("mSendId", 0);
			intent.putExtra("mChatType", DefineUser.TYPE_DEPART);
			//startActivity(intent);
			Utils.start_Activity(getActivity(), intent);
		} else if (id == R.id.layout_refresh) {//Toast.makeText(ctx, "刷新我参加的部门", Toast.LENGTH_SHORT).show();
			Toast.makeText(ctx, strTip, Toast.LENGTH_SHORT).show();
			TreeView.refreshData();
			departAdpter.setData(Departs, DepartMents);
			departAdpter.notifyDataSetChanged();
		}
	}
	

}


