
package com.lianxin.pubqq.activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.edit.MyAlertDialog;
import com.lianxin.pubqq.chat.PickFriendActivity;
import com.lianxin.panqq.client.EditClient;
import com.lianxin.panqq.client.callback.askCallBack;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.bean.UserInfo;
import com.lianxin.panqq.list.utils.GroupManger;
//添加群组成员界面
public class AddnewMemberActivity extends PickFriendActivity {
	private static int groupId = 10001;
	private static int groupType = 5;
	private static String groupName = "";	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(mType == 0){
		groupId = getIntent().getIntExtra("Chat_ID",10002);
		groupType = getIntent().getIntExtra("Chat_Type", 6);
		groupName = getIntent().getStringExtra("Chat_Name");
		deaultTitle = "添加成员";
		//if(mType == 0){
		    TextView txt_title = (TextView) findViewById(R.id.txt_title);
		    txt_title.setText(deaultTitle);
		}
	}

	@Override	
	protected void initExtendData() {
		if(mType == 0){		
		groupId = getIntent().getIntExtra("Chat_ID",10006);
		groupType = getIntent().getIntExtra("Chat_Type", 9);
		groupName = getIntent().getStringExtra("Chat_Name");
		deaultTitle = "添加成员";
		//if(mType == 0){
		    TextView txt_title = (TextView) findViewById(R.id.txt_title);
		    txt_title.setText(deaultTitle);
		}
	}	

	@Override	
	protected void postNewIntent( int type ) {
		Intent intent;		
		intent = new Intent(this, AddnewMemberActivity.class);
		intent.putExtra("type", type);
		intent.putExtra("Chat_ID",groupId);
		intent.putExtra("Chat_Type",groupType);
		intent.putExtra("Chat_Name",groupName);	
		setIntent(intent);		
		startActivity(intent);
	}

	@Override	
	protected void onNewIntent( Intent intent ) {
		super.onNewIntent(intent);		
		setIntent(intent);
    }	

	@Override
	protected void onListItemSelected(int destid,int type) {
		if (destid > 10000) {
			String msg = "确定添加新成员："+destid;			
			String title = "添加新成员?";	
			final int nDestid = destid;
			final int nType = type;			
			new MyAlertDialog(this,title,msg,
					new MyAlertDialog.OnSetListener() {
						@Override
						public void onSelect(int comm) {
						//	onForwardMessage(nDestid,nType);
							UserInfo uinfo = new UserInfo(nDestid,groupId,"",5,1,50);	
							AddnewMember(uinfo);
						}
					}).show();	
		}
	}	

	@Override	
	protected void onUseridTextInput( int destid ) {
		if (destid > 10000) {
			//String msg = this.getString(R.string.confirm_forward_to, ""+destid);
			String msg = "确定添加新成员："+destid;			
			String title = "添加新成员?";	
			final int nDestid = destid;		
			new MyAlertDialog(this,title,msg,
					new MyAlertDialog.OnSetListener() {
						@Override
						public void onSelect(int comm) {
						//	onForwardMessage(nDestid,nType);
							UserInfo uinfo = new UserInfo(nDestid,groupId,"",5,1,50);	
							AddnewMember(uinfo);
						}
					}).show();	
		}
	}	
	
	public void AddnewMember(UserInfo uinfo){
		int nDestid = uinfo.destid;
		if(GroupManger.getMyAdminPower( )>0){//判断自己是否是系统管理员
		//	startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "不能添加自己"));
		//	return;
		}	
		else if(GroupManger.getMemberPower( groupType, GloableParams.m_szUserId, groupId)<=0){//判断是否有权限
			//Utils.showLongToast(AddnewMemberActivity.this, "你没有添加成员的权限1!");			
			String str = "你没有添加成员的权限:\n" + groupId +":"+ groupType +":"+ GloableParams.m_szUserId;
			Utils.showLongToast(AddnewMemberActivity.this, str);
			return;
		}	
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(AddnewMemberActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在发送请求...").show();		
		EditClient.NewMember(groupType,uinfo,new askCallBack(){	
			@Override
			public void onSuccess(int statusCode, String message) {
				getLoadingDialog("正在发送请求...").dismiss();	
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(AddnewMemberActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在发送请求...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(AddnewMemberActivity.this, str);
					}
				});	
			}
		});	 
	}	

	@Override
	protected void onListItemClick(int position) {
		if (position != 0) {

		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {

		}

		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public void onClick(View arg0) {
		Intent intent;		
		int id = arg0.getId();
		if (id == R.id.img_back) {//finish(this);
			Utils.finish(AddnewMemberActivity.this);
		} else if (id == R.id.rl_pick_friend) {
			intent = new Intent(this, AddnewMemberActivity.class);
			intent.putExtra("type", PickFriendActivity.LOOKTYPE_FRIEND);
			startActivity(intent);
		} else if (id == R.id.rl_pick_depart) {
			intent = new Intent(this, AddnewMemberActivity.class);
			intent.putExtra("type", PickFriendActivity.LOOKTYPE_DEPART);
			//intent.putExtra("type", 2);
			startActivity(intent);
		} else if (id == R.id.rl_pick_crowd) {
			intent = new Intent(this, AddnewMemberActivity.class);
			intent.putExtra("type", PickFriendActivity.LOOKTYPE_CROWD);
			startActivity(intent);
		} else if (id == R.id.rl_pick_class) {
			intent = new Intent(this, AddnewMemberActivity.class);
			intent.putExtra("type", PickFriendActivity.LOOKTYPE_CLASS);
			startActivity(intent);
		}	
	}	
}
