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.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.Toast;

import com.lianxin.pubqq.activity.MemberListActivity;
import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.bean.InformMessage;
import com.lianxin.panqq.common.bean.UserInfo;
import com.lianxin.pubqq.activity.adpter.InformListAdapter;
import com.lianxin.pubqq.activity.adpter.InformListAdapter.OnMyButClickListener;
import com.lianxin.panqq.list.ExInformListView;
import com.lianxin.panqq.main.bean.ListUtil;
import com.lianxin.panqq.main.bean.RecentItem;
import com.lianxin.pubqq.setup.ExamineDialog;
import com.lianxin.pubqq.setup.NewApplyActivity;
import com.lianxin.pubqq.setup.NewNoticeActivity;
import com.lianxin.pubqq.setup.SigninDialog;
import com.lianxin.pubqq.setup.VoteNoticeDialog;
//import com.lianxin.pubqq.chat.PickGroupsActivity;
import com.lianxin.panqq.client.callback.listCallBack;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;

public class Fragment_Office extends Fragment implements OnClickListener {
	// 办公OA窗口（没有完工）
	private Activity ctx;
	private View layout;  
    private ScrollView qqScroll=null;    

	private ExInformListView mListView;
	private InformListAdapter informAdpter;
	private List<InformMessage> InformList;//搜索用户列表    
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (layout == null) {
			ctx = this.getActivity();
			layout = ctx.getLayoutInflater().inflate(R.layout.fragment_office,
					null);
		//	initData();	//先要初始化数据，否则下一步出错		
			initViews();
			//initData();
			setOnListener();
		} else {
			ViewGroup parent = (ViewGroup) layout.getParent();
			if (parent != null) {
				parent.removeView(layout);
			}
		}
		qqScroll = (ScrollView) layout.findViewById(R.id.qq_scroll);
		qqScroll.scrollTo(0, 0);		
		qqScroll.smoothScrollTo(0, 0);
				
		return layout;
	}

	private void initViews() {
	
		mListView = (ExInformListView)layout.findViewById(R.id.treeview); //实例化expandableLIstView
	      //给adapter设置监听  
		informAdpter = new InformListAdapter(getActivity(), InformList);
		ListUtil.setListViewHeightBasedOnChildren(mListView);	
		mListView.setAdapter(informAdpter); 			
		mListView.setDataAdapter(informAdpter);		
		mListView.setStatusListener(new listCallBack(){
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				InformList = GloableParams.InformList;
				informAdpter.setData(InformList);				
			}
			@Override
			public void onFailure(int errorCode, String errorMessage) {
				// TODO Auto-generated method stub
				
			}
		});		
		
		mListView.initData(); //初始化数据(视图初始化所需要的)		
		
		mListView.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);				
			}
		});
		informAdpter.setButListener(new OnMyButClickListener(){  

			@Override
			public void onButClicked(int position) {
				// TODO Auto-generated method stub
				onItemButtonClick(position);				
                //Toast.makeText(ctx, "Long "+ child.getName(), Toast.LENGTH_SHORT).show(); 	
		
			}  
        });		
		qqScroll = (ScrollView) layout.findViewById(R.id.qq_scroll);
		qqScroll.smoothScrollTo(0, 0);		
	}

	private void setOnListener() {

		layout.findViewById(R.id.layout_search).setOnClickListener(this);
		layout.findViewById(R.id.layout_notice).setOnClickListener(this);		
		layout.findViewById(R.id.layout_handin).setOnClickListener(this);
		layout.findViewById(R.id.layout_signin).setOnClickListener(this);
		layout.findViewById(R.id.layout_apply).setOnClickListener(this);		
		layout.findViewById(R.id.layout_affair).setOnClickListener(this);
		layout.findViewById(R.id.layout_order).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) {// 搜索好友及公众号
			//	Utils.start_Activity(getActivity(), SearchActivity.class);
			intent.setClass(getActivity(), MemberListActivity.class);
			//startActivity(intent);
			Utils.start_Activity(getActivity(), intent);
		} else if (id == R.id.layout_notice) {
			intent.setClass(getActivity(), NewNoticeActivity.class);
			//getActivity().startActivity(intent);
			Utils.start_Activity(getActivity(), intent);
		} else if (id == R.id.layout_handin) {
			intent.setClass(getActivity(), NewApplyActivity.class);
			getActivity().startActivity(intent);
		} else if (id == R.id.layout_signin) {//intent.setClass(getActivity(), PickGroupsActivity.class);
			//getActivity().startActivity(intent);
		} else if (id == R.id.layout_apply) {
			intent.setClass(getActivity(), NewApplyActivity.class);
			//getActivity().startActivity(intent);
			Utils.start_Activity(getActivity(), intent);
		} else if (id == R.id.layout_affair) {/*	UserInfo uinfo1 = new UserInfo(10001,10002,"NickName",5,2,10);
			new ExamineDialog(getActivity(),uinfo1,2,
					new ExamineDialog.OnSetListener() {
						@Override
						public void onSelect(UserInfo uinfo) {
							String strname = uinfo.nickname;
					 //       txt_nickname.setText(strname);
					 //       txt_index.setText(""+uinfo.index);
						}
					}).show();	*/
		} else if (id == R.id.layout_order) {/*UserInfo uinfo2 = new UserInfo(10001,10002,"NickName",5,2,10);
			new VoteNoticeDialog(getActivity(),uinfo2,2,
					new VoteNoticeDialog.OnSetListener() {
						@Override
						public void onSelect(UserInfo uinfo) {
							String strname = uinfo.nickname;
					 //       txt_nickname.setText(strname);
					 //       txt_index.setText(""+uinfo.index);
						}
					}).show();	*/
		} else if (id == R.id.layout_refresh) {
			Toast.makeText(ctx, "刷新我参加的部门", Toast.LENGTH_SHORT).show();
			mListView.refreshData();
		}
	}
	
	public void onItemButtonClick(int position) {
		// TODO Auto-generated method stub
		InformMessage informMsg = InformList.get(position);
		int oaType = informMsg.getMsgType();
		switch (oaType) {
		case InformMessage.OaType.OA_NOTICE:		
			new VoteNoticeDialog(ctx,informMsg,2,
					new VoteNoticeDialog.OnSetListener() {
						@Override
						public void onSelect(UserInfo uinfo) {
							String strname = uinfo.nickname;
					 //       txt_nickname.setText(strname);							
					 //       txt_index.setText(""+uinfo.index);
						}
					}).show();	
			break;
		case InformMessage.OaType.OA_HANDIN:

			break;
		case InformMessage.OaType.OA_SINGIN:
		//	UserInfo uinfo = new UserInfo(10001, 10002, "NickName", 5, 2, 10);
			new SigninDialog(ctx, informMsg, 2,
					new SigninDialog.OnSetListener() {
						@Override
						public void onSelect(UserInfo uinfo) {
							String strname = uinfo.nickname;
							// txt_nickname.setText(strname);
							// txt_index.setText(""+uinfo.index);
						}
					}).show();
			break;
		case InformMessage.OaType.OA_APPLY:
		//	UserInfo uinfo1 = new UserInfo(10001,10002,"NickName",5,2,10);			
			new ExamineDialog(ctx,informMsg,2,
					new ExamineDialog.OnSetListener() {
						@Override
						public void onSelect(UserInfo uinfo) {
							String strname = uinfo.nickname;
					 //       txt_nickname.setText(strname);							
					 //       txt_index.setText(""+uinfo.index);
						}
					}).show();	
			break;
		case InformMessage.OaType.OA_AFFAIR:

			break;
		case InformMessage.OaType.OA_ORDER:
	
			break;

		default:
			break;
		}
    }		
}


