package com.lianxin.pubqq.activity;
import java.lang.reflect.Method;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.PopupMenu;

import com.lianxin.panqq.common.bean.InformMessage;
import com.lianxin.panqq.common.bean.UserEditInfo;
import com.lianxin.panqq.common.bean.UserInfo;
import com.lianxin.panqq.list.ExInformList;
import com.lianxin.pubqq.activity.adpter.InformListAdapter.OnMyButClickListener;
import com.lianxin.pubqq.R;
import com.lianxin.pubqq.activity.adpter.InformListAdapter;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.pubqq.setup.ExamineDialog;
import com.lianxin.pubqq.setup.ReplyListActivity;
import com.lianxin.pubqq.setup.SigninDialog;
import com.lianxin.pubqq.setup.VoteNoticeDialog;
//import com.lianxin.panqq.chat.utils.MyLogger;
import com.lianxin.panqq.client.AckClient;
import com.lianxin.panqq.client.callback.askCallBack;
import com.lianxin.panqq.client.callback.listCallBack;
//办公事务列表
public class InformListActivity extends BaseActivity implements  OnClickListener {
	private ImageView img_back,img_right;    
	private InformListAdapter mAdapter;
	private List<InformMessage> InformList;//管理消息列表
	private ExInformList mListView;
	private int nSendId = 0;
	private int nChatType = 0;	

//	private static MyLogger Log = MyLogger.yLog();
	
    @Override
   	public void onCreate(Bundle savedInstanceState) {
      	 
   		super.onCreate(savedInstanceState);
   		setContentView(R.layout.activity_informlist);
   		
		img_back = (ImageView) findViewById(R.id.img_back);
		img_right = (ImageView) findViewById(R.id.img_right);   		
		img_back.setOnClickListener(this);
		img_right.setOnClickListener(this);  	

		mListView = (ExInformList)findViewById(R.id.user_xListView);
		mListView.setPullLoadEnable(true);

		nChatType = getIntent().getIntExtra("mChatType", DefineUser.TYPE_CROWD);
		nSendId = getIntent().getIntExtra("mSendId", 10001);
		mListView.setTypeAndId(nChatType,nSendId);		
		//initData();
		 
		mAdapter =  new InformListAdapter(this, InformList);
		mListView.setAdapter(mAdapter);
		mListView.setDataAdapter(InformList,mAdapter);		
		mListView.setStatusListener(new listCallBack(){
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				InformList = GloableParams.InformList;
				mAdapter.setData(InformList);				
			}
			@Override
			public void onFailure(int errorCode, String errorMessage) {
				// TODO Auto-generated method stub
				
			}
		});		
		
		mListView.initData(); //初始化数据(视图初始化所需要的)		
	//	mListView.setXListViewListener(this);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				final InformMessage InviteInfo = mAdapter.getItem(position-1);
				int dataType = InviteInfo.dataType;
				final int chatType = InviteInfo.chatType;	
				Intent intent = new Intent(context, ReplyListActivity.class);				
			//	intent.setClass(this, ReplyListActivity.class);			
				intent.putExtra("TypeID", 10058);
				intent.putExtra("TypeType", DefineUser.TYPE_CROWD);	
				intent.putExtra("Position", position-1);				
				startActivity(intent);					
	
			}
			
		});
		mAdapter.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(); 	
		
			}  
        });
    }
 
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(InformListActivity.this);
			//finish();
		} else if (id == R.id.img_right) {
			PopMenu(v);
		}
	} 
	
	private void PopMenu(View v) {
        //创建弹出式菜单对象（最低版本11）
        PopupMenu popup = new PopupMenu(this, v);//第二个参数是绑定的那个view
        //获取菜单填充器
        MenuInflater inflater = popup.getMenuInflater();
        //填充菜单
        inflater.inflate(R.menu.inform, popup.getMenu());
        setIconEnable(popup.getMenu(),true);  //  就是这一句使图标能显示        
        //绑定菜单项的点击事件
  //      popup.setOnMenuItemClickListener(this);
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
 
            	int itemId = item.getItemId();
				if (itemId == R.id.exit) {
					//  Toast.makeText(this, "退出", Toast.LENGTH_SHORT).show();
				} else if (itemId == R.id.set) {
					//  Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show();
				} else if (itemId == R.id.account) {
					//  Toast.makeText(this, "账号", Toast.LENGTH_SHORT).show();
				}             
                return false;
            }

        });        
        //显示(这一行代码不要忘记了)
        popup.show();		
	
	}

	/**
     * 解决不显示menu icon的问题
     * @param menu
     * @param flag
     */
    private void setIconEnable(Menu menu, boolean flag) {
        //判断menu是否为空
        if(menu != null) {
            try {
                //如果不为空,就反射拿到menu的setOptionalIconsVisible方法
                Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                //暴力访问该方法
                method.setAccessible(true);
                //调用该方法显示icon
                method.invoke(menu, flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) { // 清空消息
			//Intent intent = new Intent();			
			switch (requestCode) {
			case 20:
				mListView.onRefresh();
				break;			
			default:
				break;
			}			
		}
	}	

	public void ackAddFriend(UserEditInfo uinfo,int comm){	
		getLoadingDialog("正在提交...").show();		
		AckClient.AckAddFriend(uinfo,comm,new askCallBack(){	
			@Override
			public void onSuccess(int statusCode, String message) {
				getLoadingDialog("正在提交...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(InformListActivity.this, str);
					}
				});					
			}

			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在提交...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(InformListActivity.this, str);
					}
				});	
			}
		});	 
	}	
	
	public void ackJoinGroup(int chatType,UserEditInfo uinfo,int comm){	
		getLoadingDialog("正在提交...").show();		
		AckClient.AckJoinGroup(chatType,uinfo,comm,new askCallBack(){	
			@Override
			public void onSuccess(int statusCode, String message) {
				getLoadingDialog("正在提交...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
			//			Utils.showLongToast(InviteListActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在提交...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(InformListActivity.this, str);
					}
				});	
			}
		});	 
	}	
	
	public void ackApplyGroup(int chatType,UserInfo uinfo,int comm){	
		getLoadingDialog("正在提交...").show();		
		AckClient.AckApplyGroup(chatType,uinfo,comm,new askCallBack(){	
			@Override
			public void onSuccess(int statusCode, String message) {
				getLoadingDialog("正在提交...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(InformListActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在提交...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(InformListActivity.this, str);
					}
				});	
			}
		});	 
	}

	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:
			//UserInfo uinfo2 = new UserInfo(10001,10002,"NickName",5,2,10);			
			new VoteNoticeDialog(this,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(this, 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(this,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;
		}
    }		
}
 