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

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;

//import com.lianxin.panqq.file.InviteFileUtil;
import com.lianxin.panqq.common.bean.CommonSysMsg;
import com.lianxin.panqq.common.bean.GroupEditInfo;
import com.lianxin.panqq.common.bean.InviteMessage;
import com.lianxin.panqq.common.bean.UserEditInfo;
import com.lianxin.panqq.common.bean.UserInfo;
import com.lianxin.panqq.common.bean.InviteMessage.InviteMesageStatus;
import com.lianxin.panqq.list.ExInviteList;
import com.lianxin.panqq.list.utils.UserUtil;
import com.lianxin.pubqq.R;
import com.lianxin.pubqq.activity.adpter.InviteListAdapter;
import com.lianxin.pubqq.activity.adpter.InviteListAdapter.OnMyButClickListener;
import com.lianxin.panqq.edit.AckAddFriendDialog;
import com.lianxin.panqq.edit.AckAskApplyDialog;
import com.lianxin.panqq.edit.AckAskCreateDialog;
import com.lianxin.panqq.edit.AckAskJoinDialog;
import com.lianxin.panqq.edit.AckInviteDialog;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.Utils;
import com.lianxin.pubqq.main.MyApp;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.client.AckClient;
import com.lianxin.panqq.client.callback.askCallBack;
import com.lianxin.panqq.client.callback.listCallBack;

//用户申请列表
 
public class InviteListActivity extends BaseActivity implements  OnClickListener {
	private ImageView img_back;    
	private InviteListAdapter mAdapter;
	private SysMessageBroadcastReceiver receiver;	
	private List<InviteMessage> InviteList = GloableParams.InviteList;//搜索用户列表
	private ExInviteList mListView;	
	
	private int nSendId = 0;
	private int nChatType = 0;	

    @Override
   	public void onCreate(Bundle savedInstanceState) {
      	 
   		super.onCreate(savedInstanceState);
   		setContentView(R.layout.activity_invitelist);
   		
		img_back = (ImageView) findViewById(R.id.img_back);		
		img_back.setOnClickListener(this); 	
		mListView = (ExInviteList)findViewById(R.id.user_xListView);
		mListView.setPullLoadEnable(true);

		nChatType = getIntent().getIntExtra("mChatType", 0);
		nSendId = getIntent().getIntExtra("mSendId", 0);
		mListView.setTypeAndId(nChatType,nSendId);		
	//	initData();
		 
		mAdapter =  new InviteListAdapter(this, InviteList);
		mListView.setAdapter(mAdapter);
		mListView.setDataAdapter(InviteList,mAdapter);		
		mListView.setStatusListener(new listCallBack(){
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				InviteList = GloableParams.InviteList;
				mAdapter.setData(InviteList);	
			}
			@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 v, int position,
					long id) {
			//	Utils.showLongToast(InviteListActivity.this, "onItemClick");				
				onItemListClick(position);				
			}
			
		});
		mAdapter.setButListener(new OnMyButClickListener(){  
			@Override
			public void onButClicked(int position) {
			//	Utils.showLongToast(InviteListActivity.this, "onItemButtonClick");
				onItemButtonClick(position);						
			}  
        });
		// 注册接收消息广播
		receiver = new SysMessageBroadcastReceiver();
		IntentFilter intentFilter = new IntentFilter(MyApp.getInstance().getSysMessageBroadcastAction());
		// 设置广播的优先级别大于Mainacitivity,这样如果消息来的时候正好在chat页面，直接显示消息，而不是提示消息未读
		intentFilter.setPriority(5);
		registerReceiver(receiver, intentFilter);
		
	//	mListView.resetData();	//刷新远程数据		
    }
 
	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.img_back) {
			Utils.finish(InviteListActivity.this);
			//finish();
			//finish();

			/*	case R.id.img_right:
			Intent intent = new Intent();
			intent.setClass(this, UserSearchActivity.class);
			intent.putExtra("REQUEST_FROM", 1);
			startActivityForResult(intent, 20);
			break;*/
		}
	}    

	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){	
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(InviteListActivity.this, "你还没有登录服务器!");
			return;
		}		
		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(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(InviteListActivity.this, str);
					}
				});	
			}
		});	 
	}	
	
	public void ackInviteGroup(int chatType,UserEditInfo uinfo,int comm){	
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(InviteListActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在提交...").show();		
		AckClient.AckInviteGroup(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(InviteListActivity.this, str);
					}
				});	
			}
		});	 
	}	

	public void ackJoinGroup(int chatType,UserEditInfo uinfo,int comm){	
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(InviteListActivity.this, "你还没有登录服务器!");
			return;
		}		
		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(InviteListActivity.this, str);
					}
				});	
			}
		});	 
	}	
	
	public void ackApplyGroup(int chatType,UserInfo uinfo,int comm){
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(InviteListActivity.this, "你还没有登录服务器!");
			return;
		}		
		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(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(InviteListActivity.this, str);
					}
				});	
			}
		});	 
	}

	public void ackCreateGroup(int chatType,GroupEditInfo uinfo,int comm){	
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(InviteListActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在提交...").show();		
		AckClient.AckCreateGroup(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(InviteListActivity.this, str);
					}
				});	
			}
		});	 
	}	
	/**
	 * 消息广播接收者
	 * 
	 */
	private class SysMessageBroadcastReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {

			int Destid = getIntent().getIntExtra("Chat_ID",10002);
			int Type = getIntent().getIntExtra("Chat_Type", 0);
			if (nSendId == 0 || nChatType == 0) {
				// 消息不是发给当前会话，return
			}else if (nSendId != Destid || nChatType != Type) {
				// 消息不是发给当前会话，return
			//	return;
			}			
			// 通知adapter有新消息，更新ui
			mAdapter.refresh();
			mListView.setSelection(mListView.getCount() - 1);
			// 记得把广播给终结掉
			abortBroadcast();
		}
	}	
		
	public void onItemListClick(int position) {
		// TODO Auto-generated method stub
		//InviteMessage informMsg = InviteList.get(position);
		final InviteMessage InviteInfo = mAdapter.getItem(position-1);		
		int dataType = InviteInfo.datatype;
		final int chatType = InviteInfo.chattype;				
		switch (dataType) {
		case CommonSysMsg.DATATYPE_ASKFRIEND:
			new AckAddFriendDialog(InviteListActivity.this,InviteInfo,
					new AckAddFriendDialog.OnSetListener() {
						@Override
						public void onSelect(UserEditInfo uinfo,int comm) {
							if (chatType > 0)							
							ackAddFriend(uinfo,comm);
							if(comm>0)
							    InviteInfo.setStatus(InviteMesageStatus.AGREED);
							else
							    InviteInfo.setStatus(InviteMesageStatus.REFUSED);	
						  //  InviteFileUtil.ackDealwithMessage(InviteInfo,103, 100); 
							UserUtil.ackDealwithMessage(InviteListActivity.this,InviteInfo,103); 						    
							runOnUiThread(new Runnable() {
								public void run() {
								//	Utils.showLongToast(LoginActivity.this, message);
									mAdapter.notifyDataSetChanged();											
								}
							});										
						}
					}).show();	
			break;
		case CommonSysMsg.DATATYPE_ASKADD:
			new AckInviteDialog(InviteListActivity.this,InviteInfo,
					new AckInviteDialog.OnSetListener() {
						@Override
						public void onSelect(UserEditInfo uinfo,int comm) {
							if (chatType > 0)							
								ackInviteGroup(chatType,uinfo,comm);
							if(comm>0)
							    InviteInfo.setStatus(InviteMesageStatus.AGREED);
							else
							    InviteInfo.setStatus(InviteMesageStatus.REFUSED);	
						  //  InviteFileUtil.ackDealwithMessage(InviteInfo,103, 100); 
							UserUtil.ackDealwithMessage(InviteListActivity.this,InviteInfo,103); 						    
							runOnUiThread(new Runnable() {
								public void run() {
								//	Utils.showLongToast(LoginActivity.this, message);
									mAdapter.notifyDataSetChanged();											
								}
							});										
						}
					}).show();	
			break;			
		case CommonSysMsg.DATATYPE_ASKJOIN:
			new AckAskJoinDialog(InviteListActivity.this,InviteInfo,
					new AckAskJoinDialog.OnSetListener() {
						@Override
						public void onSelect(UserEditInfo uinfo,int comm) {
							if (chatType > 0)							
							ackJoinGroup(chatType,uinfo,comm);
							if(comm>0)
							    InviteInfo.setStatus(InviteMesageStatus.AGREED);
							else
							    InviteInfo.setStatus(InviteMesageStatus.REFUSED);	
							UserUtil.ackDealwithMessage(InviteListActivity.this,InviteInfo,104); 						
							runOnUiThread(new Runnable() {
								public void run() {
									mAdapter.notifyDataSetChanged();											
								}
							});										
						}
					}).show();	
			break;					
		case CommonSysMsg.DATATYPE_USERINFO:
		/*	new AckAddFriendDialog(InviteListActivity.this,InviteInfo,
					new AckAddFriendDialog.OnSetListener() {
						@Override
						public void onSelect(UserInfo uinfo) {
						//	addFriend(askInfo);
						}
					}).show();	*/
			break;	
		case CommonSysMsg.DATATYPE_GROUPINFO:

			break;			
		case CommonSysMsg.DATATYPE_ASKAPPLY:
			new AckAskApplyDialog(InviteListActivity.this,InviteInfo,
					new AckAskApplyDialog.OnSetListener() {
						@Override
						public void onSelect(UserInfo uinfo,int comm) {
							if (chatType > 0)							
							ackApplyGroup(chatType,uinfo,comm);
							if(comm>0)
							    InviteInfo.setStatus(InviteMesageStatus.AGREED);
							else
							    InviteInfo.setStatus(InviteMesageStatus.REFUSED);	
							UserUtil.ackDealwithMessage(InviteListActivity.this,InviteInfo,104); 						
							runOnUiThread(new Runnable() {
								public void run() {
									mAdapter.notifyDataSetChanged();											
								}
							});										
						}
					}).show();	
			break;		
		case CommonSysMsg.DATATYPE_ASKCREATE:
			new AckAskCreateDialog(InviteListActivity.this,InviteInfo,
					new AckAskCreateDialog.OnSetListener() {
						@Override
						public void onSelect(GroupEditInfo uinfo,int comm) {
							if (chatType > 0)							
							ackCreateGroup(chatType,uinfo,comm);
							if(comm>0)
							    InviteInfo.setStatus(InviteMesageStatus.AGREED);
							else
							    InviteInfo.setStatus(InviteMesageStatus.REFUSED);	
							UserUtil.ackDealwithMessage(InviteListActivity.this,InviteInfo,104); 							
							runOnUiThread(new Runnable() {
								public void run() {
									mAdapter.notifyDataSetChanged();											
								}
							});										
						}
					}).show();
			break;	
			
		default:			
			break;
		}					
	

    }	
	
	public void onItemButtonClick(int position) {
		// TODO Auto-generated method stub
		InviteMessage InviteInfo = InviteList.get(position);
		int dataType = InviteInfo.datatype;
		final int chatType = InviteInfo.chattype;	
		if (chatType == 0) {
			InviteInfo.setStatus(InviteMesageStatus.AGREED);
			mAdapter.notifyDataSetChanged();
			UserUtil.ackDealwithMessage(InviteListActivity.this,
					InviteInfo, 103);
			return;
		}

		switch (dataType) {
		case CommonSysMsg.DATATYPE_ASKFRIEND: {
			UserEditInfo uinfo = new UserEditInfo();
			uinfo.userid = InviteInfo.userid;
			uinfo.destid = InviteInfo.destid;
			uinfo.username = InviteInfo.username;
			uinfo.nickname = InviteInfo.destname;			
			uinfo.type = 1;
			uinfo.power = 4;			
			ackAddFriend(uinfo, 2);
		}
			break;
		case CommonSysMsg.DATATYPE_ASKJOIN: {
			UserEditInfo uinfo = new UserEditInfo();
			uinfo.userid = InviteInfo.userid;
			uinfo.destid = InviteInfo.destid;
			//uinfo.nickname = InviteInfo.username;
			uinfo.username = InviteInfo.username;
			uinfo.nickname = InviteInfo.destname;			
			uinfo.type = 1;
			uinfo.power = 1;
			uinfo.imageid = InviteInfo.imageid;
			ackJoinGroup(chatType, uinfo, 1);
		}
			break;

		case CommonSysMsg.DATATYPE_ASKADD: {
			UserEditInfo uinfo = new UserEditInfo();
			//uinfo.userid = InviteInfo.userid;
			uinfo.destid = InviteInfo.destid;
			//uinfo.nickname = InviteInfo.username;
			//uinfo.username = InviteInfo.username;
			uinfo.nickname = InviteInfo.destname;			
			uinfo.type = 1;
			uinfo.power = 1;
			//uinfo.imageid = InviteInfo.imageid;
        	uinfo.userid = GloableParams.m_szUserId; 
           	uinfo.username = GloableParams.m_szUserName;
        	uinfo.imageid = GloableParams.m_szImage;		
			ackInviteGroup(chatType, uinfo, 1);
		}
			break;			
			
		case CommonSysMsg.DATATYPE_USERINFO:

			break;
		case CommonSysMsg.DATATYPE_GROUPINFO:

			break;
		case CommonSysMsg.DATATYPE_ASKAPPLY:{
			UserInfo uinfo = new UserInfo();
			uinfo.userid = InviteInfo.userid;
			uinfo.destid = InviteInfo.destid;
			uinfo.nickname = InviteInfo.username;
			// uinfo.type = tempPower;
			uinfo.type = 2;
			uinfo.power = 2;
			uinfo.imageid = InviteInfo.imageid;
			ackApplyGroup(chatType, uinfo, 1);
		}
			break;
		case CommonSysMsg.DATATYPE_ASKCREATE:{
			GroupEditInfo uinfo = new GroupEditInfo();
			uinfo.userid = InviteInfo.userid;
			uinfo.groupid = InviteInfo.destid;
		//	uinfo.groupname = InviteInfo.destname;
			uinfo.nickname = InviteInfo.username;
			uinfo.groupname = InviteInfo.destname;			
			// uinfo.type = tempPower;
			uinfo.type = 2;
			uinfo.power = 2;
			uinfo.imageid = InviteInfo.imageid;
			ackCreateGroup(chatType, uinfo, 1);
		}
			break;

		default:
			break;
		}
	    InviteInfo.setStatus(InviteMesageStatus.AGREED);
		mAdapter.notifyDataSetChanged();		    
	   // InviteFileUtil.ackDealwithMessage(InviteListActivity.this,InviteInfo,103); 
		UserUtil.ackDealwithMessage(InviteListActivity.this,InviteInfo,103);	    
    }		
}
 