 
package com.lianxin.pubqq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.edit.AddFriendDialog;
import com.lianxin.panqq.edit.AskJoinDialog;
import com.lianxin.panqq.edit.EditOpenLevelDialog;
import com.lianxin.panqq.edit.MyAlertDialog;
import com.lianxin.panqq.client.AskClient;
import com.lianxin.panqq.client.callback.askCallBack;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.list.utils.AddressUtils;
import com.lianxin.panqq.list.utils.CityUtils;
import com.lianxin.panqq.list.utils.CollegeUtils;
import com.lianxin.panqq.list.utils.DepartUtils;
import com.lianxin.panqq.list.utils.FriendUtils;
import com.lianxin.panqq.list.utils.GroupManger;
import com.lianxin.panqq.common.utils.KeywordUtils;
import com.lianxin.panqq.common.utils.OpenLevelUtils;
import com.lianxin.pubqq.chat.EMChatActivity;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.picker.AddressPickerDialog;
import com.lianxin.panqq.picker.CityPickerDialog;
import com.lianxin.panqq.picker.DeptPickerDialog;
import com.lianxin.panqq.picker.GradePickerDialog;
import com.lianxin.panqq.picker.ImagePickerDialog;
import com.lianxin.panqq.picker.KeyPickerDialog;
import com.lianxin.panqq.utils.ImagesUtils;
import com.lianxin.panqq.main.EMContactManager;
import com.lianxin.panqq.common.bean.AskInfo;

// 群组信息界面
public class GroupInfoActivity extends BaseActivity implements OnClickListener {

	private TextView txt_title,txt_right;
	private ImageView img_back, img_right; 
	private ImageView img_photo;  //用户头像
	private TextView txt_groupid; //用户id
	private EditText txt_groupname;  //用户名	
	private TextView txt_enyear;   //用户地区
	private TextView txt_type;   //用户职业
	private TextView txt_number;   //用户签名
	private TextView txt_openlevel;  //用户在哪动态
	private TextView cap_type, cap_enyear;	
	private TextView cap_groupid, cap_groupname;
	private Button btn_joingroup;
	private Button btn_jubao;
	private Button btn_editgroup;
	private Button btn_listmember;	
	private RelativeLayout lay_image;	
	private RelativeLayout lay_groupname;
	private RelativeLayout lay_type;  
	private RelativeLayout lay_enyear;
	private RelativeLayout lay_openlevel;
	
	private int nGroupId = 10100;//用户唯一id
	private String strGroupName = "情已停机";
	private int groupType = DefineUser.TYPE_CROWD;		
	private int nImage = 10;	
	int nKeyword = 2;
	int nAddress = 2;
	int nCollege = 2;
	int nEnyear = 2;
	int nDept = 30;
	int nCity = 2;		
	private int iNumber = 20;
	private int iLevel = 2;
	
	//public static final int REQUEST_CODE_ADD_TO_CONTACT = 226;
	//public static final int REQUEST_CODE_ADD_TO_BLACKLIST = 225;
	
	private LinearLayout lay_right;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_groupinfo);
		
		initControl();
		initView();
		initData();
		setListener();			
	}

	protected void initControl() {
		txt_title = (TextView) findViewById(R.id.txt_title);
		img_back = (ImageView) findViewById(R.id.img_back);
		img_back.setVisibility(View.VISIBLE);	
		txt_groupid = (TextView)findViewById(R.id.tv_groupid);
		txt_groupname = (EditText)findViewById(R.id.tv_groupname);		
		img_photo = (ImageView)findViewById(R.id.iv_image);
	//	txt_nickname = (TextView)findViewById(R.id.tv_user_nickname);
		txt_type = (TextView)findViewById(R.id.tv_type);
		txt_enyear = (TextView)findViewById(R.id.tv_enyear);
		txt_number = (TextView)findViewById(R.id.tv_number1);
		txt_openlevel = (TextView)findViewById(R.id.tv_openlevel);
		cap_groupid = (TextView) findViewById(R.id.tv_groupid_cap);			
		cap_groupname = (TextView) findViewById(R.id.tv_groupname_cap);
		cap_type = (TextView) findViewById(R.id.tv_type_cap);			
		cap_enyear = (TextView) findViewById(R.id.tv_enyear_cap);
		lay_image = (RelativeLayout) findViewById(R.id.rl_group_pic);		
		lay_groupname = (RelativeLayout) findViewById(R.id.rl_groupname);
		lay_type = (RelativeLayout) findViewById(R.id.rl_type);
		lay_enyear = (RelativeLayout) findViewById(R.id.rl_enyear);
		lay_openlevel = (RelativeLayout) findViewById(R.id.rl_openlevel);		
	//	btn_satrtchat = (Button)findViewById(R.id.btn_startchat);
		btn_joingroup = (Button)findViewById(R.id.btn_joingroup);
		btn_editgroup = (Button)findViewById(R.id.btn_editgroup);
		btn_jubao = (Button)findViewById(R.id.btn_jubao);
		btn_listmember = (Button)findViewById(R.id.btn_listmember);		
		lay_right = (LinearLayout)findViewById(R.id.layout_right);
		txt_right = (TextView)findViewById(R.id.tv_right);
	}

	protected void initView() {
		nGroupId = getIntent().getIntExtra("Info_GroupId", 10010);
		strGroupName = getIntent().getStringExtra("Info_GroupName");
		groupType = getIntent().getIntExtra("Info_GroupType", 3);	
		setCaption();		
	}
	
	protected void initData() {

		nGroupId = getIntent().getIntExtra("Info_GroupId", 10010);		
		strGroupName = getIntent().getStringExtra("Info_GroupName");
		groupType = getIntent().getIntExtra("Info_GroupType", 3);		
		nCollege = getIntent().getIntExtra("Info_Type", 20);
		nEnyear = getIntent().getIntExtra("Info_Enyear", 10);
		nKeyword = nCollege;
		nAddress = nEnyear;	
		nDept = nAddress;		
		nImage = getIntent().getIntExtra("Info_Image", 23);
		iNumber = getIntent().getIntExtra("Info_Number", 10);
		iLevel = getIntent().getIntExtra("Info_Open", 10);		

        setGroupInfo();       
	}
	
	protected void setGroupInfo() {	
		txt_groupid.setText(""+nGroupId); 
		txt_groupname.setText(strGroupName);	
	//	txt_nickname.setText(strNickName);		
        if(nImage<0) nImage = -nImage;		
        if(nImage>80) nImage = nImage%80;
        img_photo.setImageResource(ImagesUtils.images[nImage]);	
       
        txt_number.setText(""+iNumber);		
		txt_openlevel.setText(OpenLevelUtils.getOpenLevelText(iLevel));
		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			txt_type.setText(KeywordUtils.getKeyword(nKeyword));
			txt_enyear.setText(AddressUtils.getAddress(nAddress));	
			break;		
		case DefineUser.TYPE_CROWD:
			txt_type.setText(KeywordUtils.getKeyword(nKeyword));
			txt_enyear.setText(AddressUtils.getAddress(nAddress));			
			break;
		case DefineUser.TYPE_MYCLASS:	
			nCollege = nKeyword;
			nEnyear = nAddress;	
			txt_type.setText(CollegeUtils.getCollege(nCollege));	
			txt_enyear.setText("" + nEnyear);				
			break;
		case DefineUser.TYPE_DEPART:
			nDept = nAddress;
			txt_type.setText(DepartUtils.getDepartName(nDept));						
			txt_enyear.setText(DepartUtils.getDepartTypeName(nDept));			
			break;		
		case DefineUser.TYPE_PARTY:
			nDept = nAddress;
			txt_type.setText(DepartUtils.getDepartName(nDept));						
			txt_enyear.setText(DepartUtils.getDepartTypeName(nDept));	
			break;
		case DefineUser.TYPE_GROUP:
			nCity = nAddress;
			txt_type.setText(CityUtils.getCityTypeName(nCity));						
			txt_enyear.setText(CityUtils.getCityName(nCity));	
			break;
		case DefineUser.TYPE_CITY:
			nCity = nAddress;
			txt_type.setText(CityUtils.getCityTypeName(nCity));						
			txt_enyear.setText(CityUtils.getCityName(nCity));
			break;		
	
		default:
			txt_type.setText(KeywordUtils.getKeyword(nKeyword));
			txt_enyear.setText(AddressUtils.getAddress(nAddress));			
			break;
		}  				
	}	

	protected void setCaption() {
		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			txt_title.setText("分类");
			cap_groupid.setText("分类ID");
			cap_groupname.setText("分类名称");
			cap_type.setText("关  键  字");
			cap_enyear.setText("兴趣地域");	
			break;		
		case DefineUser.TYPE_CROWD:
			txt_title.setText("群资料");
			cap_groupid.setText("群ID");
			cap_groupname.setText("群名称");
			cap_type.setText("关  键  字");
			cap_enyear.setText("兴趣地域");			
			break;
		case DefineUser.TYPE_MYCLASS:
			txt_title.setText("班资料");
			cap_groupid.setText("班ID");
			cap_groupname.setText("班名称");
			cap_type.setText("所属院系");
			cap_enyear.setText("入学年份");	
			break;
		case DefineUser.TYPE_DEPART:
			txt_title.setText("部门资料");
			cap_groupid.setText("部门ID");
			cap_groupname.setText("部门名称");
			cap_type.setText("所属父项");
			cap_enyear.setText("主管部门");	
			break;		
		case DefineUser.TYPE_PARTY:
			txt_title.setText("会议室资料");
			cap_groupid.setText("会议ID");
			cap_groupname.setText("会议名称");
			cap_type.setText("所属父项");
			cap_enyear.setText("主管部门");	
			break;
		case DefineUser.TYPE_GROUP:
			txt_title.setText("俱乐部资料");
			cap_groupid.setText("输入部ID");
			cap_groupname.setText("输入部名");
			cap_type.setText("所属省份");
			cap_enyear.setText("所在城市");	
			break;
		case DefineUser.TYPE_CITY:
			txt_title.setText("地域资料");
			cap_groupid.setText("地域ID");
			cap_groupname.setText("地域名称");
			cap_type.setText("所属省份");
			cap_enyear.setText("所在城市");	
			break;		
	
		default:
			txt_title.setText("群资料");
			cap_groupid.setText("群ID");
			cap_groupname.setText("输入群名称");
			cap_type.setText("关  键  字");
			cap_enyear.setText("兴趣地域");				
			break;
		}			
	}	
	
	protected void setListener() {
		img_back.setOnClickListener(this);		
		lay_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
		//0314		startActivity(new Intent(Userinfo.this , UserActivity.class).putExtra("userId", userId));
			}
		});
	//	btn_satrtchat.setOnClickListener(this);
		btn_joingroup.setOnClickListener(this); 
		btn_editgroup.setOnClickListener(this);
		btn_jubao.setOnClickListener(this);	
		btn_listmember.setOnClickListener(this);			
		lay_image.setOnClickListener(this);			
		lay_type.setOnClickListener(this); 
		lay_enyear.setOnClickListener(this); 
		lay_openlevel.setOnClickListener(this); 
 		
	}	
		
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int id = arg0.getId();
		if (id == R.id.img_back) {
			Utils.finish(GroupInfoActivity.this);
			//finish();
			//finish(GroupInfoActivity.this);
		} else if (id == R.id.btn_startchat) {
			Intent intent = new Intent(this, EMChatActivity.class);
			intent.putExtra("Chat_ID", nGroupId);
			intent.putExtra("Chat_Type", groupType);
			intent.putExtra("Chat_Name", strGroupName);
			startActivity(intent);
		} else if (id == R.id.btn_joingroup) {
			AskInfo uinfo = new AskInfo(nGroupId, strGroupName, nImage);
			new AskJoinDialog(GroupInfoActivity.this, uinfo,
					new AskJoinDialog.OnSetListener() {
						@Override
						public void onSelect(AskInfo askInfo) {
							joinGroup(askInfo);
						}
					}).show();
		} else if (id == R.id.btn_toblack) {
			new MyAlertDialog(GroupInfoActivity.this, "移入到黑名单？",
					"你确定要将该成员移入到黑名单吗？",
					new MyAlertDialog.OnSetListener() {
						@Override
						public void onSelect(int comm) {
							//	UserInfo uinfo = new UserInfo(nUserId,nTypeId,strNickName,image,iPower,iIndex);
							//	deleteMember(uinfo);
						}
					}).show();
		} else if (id == R.id.btn_listmember) {
			Intent intent_list = new Intent(this, MemberListActivity.class);
			intent_list.putExtra("TypeID", nGroupId);
			intent_list.putExtra("TypeType", groupType);
			startActivity(intent_list);
		} else if (id == R.id.btn_jubao) {
			AskInfo uinfo1 = new AskInfo(nGroupId, strGroupName, nImage);
			new AddFriendDialog(GroupInfoActivity.this, uinfo1,
					new AddFriendDialog.OnSetListener() {
						@Override
						public void onSelect(AskInfo askInfo) {
							//addFriend(askInfo);
						}
					}).show();
		} else if (id == R.id.rl_openlevel) {
			new EditOpenLevelDialog(GroupInfoActivity.this, iLevel,
					new EditOpenLevelDialog.OnSetListener() {
						@Override
						public void onSelect(int level) {
							iLevel = level;
							txt_openlevel.setText(OpenLevelUtils.getOpenLevelText(iLevel));
						}
					}).show();
		} else if (id == R.id.rl_type) {
			Select_Type();
		} else if (id == R.id.rl_enyear) {//修改城市
			Select_Enyear();
		} else if (id == R.id.rl_group_pic) {//修改头像
			change_image();
		}
	} 
	
	protected void Select_Type() {
		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			change_keyword();
			break;		
		case DefineUser.TYPE_CROWD:
			change_keyword();		
			break;
		case DefineUser.TYPE_MYCLASS:
			change_grade();					
			break;
		case DefineUser.TYPE_DEPART:
			change_depart();
			break;		
		case DefineUser.TYPE_PARTY:
			change_depart();
			break;
		case DefineUser.TYPE_GROUP:
			change_city();	
			break;
		case DefineUser.TYPE_CITY:
			change_city();
			break;			
		default:
			change_keyword();		
			break;
		}  		
	}	

	protected void Select_Enyear() {
		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			change_address();
			break;		
		case DefineUser.TYPE_CROWD:
			change_address();		
			break;
		case DefineUser.TYPE_MYCLASS:
			change_grade();					
			break;
		case DefineUser.TYPE_DEPART:
			change_depart();
			break;		
		case DefineUser.TYPE_PARTY:
			change_depart();
			break;
		case DefineUser.TYPE_GROUP:
			change_city();	
			break;
		case DefineUser.TYPE_CITY:
			change_city();
			break;			
		default:
			change_address();		
			break;
		}  		
	}	
	
	public void change_address() {
		new AddressPickerDialog(GroupInfoActivity.this,
				AddressPickerDialog.THEME_HOLO_LIGHT,nAddress,false,
				new AddressPickerDialog.OnAddressSetListener() {
					@Override
					public void onAddressSelect(int address) {
						nAddress = address;
						txt_enyear.setText(AddressUtils.getAddress(address));						
						String textString = "" + address;
						Utils.showLongToast(GroupInfoActivity.this, textString);
					}
				}).show();
	}

	public void change_city() {
		new CityPickerDialog(GroupInfoActivity.this,
				CityPickerDialog.THEME_HOLO_LIGHT,nCity,false,
				new CityPickerDialog.OnCitySetListener() {
					@Override
					//public void onCitySelect(int province,int city) {					
					public void onCitySelect(int city) {
						nCity = city;
						txt_type.setText(CityUtils.getCityTypeName(city));						
						txt_enyear.setText(CityUtils.getCityName(city));						
						String textString = "" + city;
						Utils.showLongToast(GroupInfoActivity.this, textString);
					}
				}).show();
	}	
	
	public void change_depart() {
		new DeptPickerDialog(GroupInfoActivity.this,
				DeptPickerDialog.THEME_HOLO_LIGHT,nDept,false,
				new DeptPickerDialog.OnDeptSetListener() {
					@Override
					public void onDeptSelect(int address) {
						nDept = address;
						txt_type.setText(DepartUtils.getDepartName(address));						
						txt_enyear.setText(DepartUtils.getDepartTypeName(address));						
						String textString = "" + address;
						Utils.showLongToast(GroupInfoActivity.this, textString);
					}
				}).show();
	}

	public void change_grade() {
		new GradePickerDialog(GroupInfoActivity.this,
				GradePickerDialog.THEME_HOLO_LIGHT,nCollege,nEnyear,
				new GradePickerDialog.OnGradeSetListener() {
					@Override
					public void onGradeSelect(int minage,int maxage) {
						nCollege = minage;
						nEnyear = maxage;	
						txt_type.setText(CollegeUtils.getCollege(nCollege));	
						txt_enyear.setText("" + nEnyear);							
						String textString = "" + minage+"-" + maxage;
				//		txt_age.setText(""+ageMin+"-"+ageMax);						
						Utils.showLongToast(GroupInfoActivity.this, textString);
					}
				}).show();
	}	
	
	public void change_keyword() {
		new KeyPickerDialog(GroupInfoActivity.this,
				KeyPickerDialog.THEME_HOLO_LIGHT,nKeyword,
				new KeyPickerDialog.OnKeySetListener() {
					@Override
					public void onKeySelect(int keyword) {
						String textString = KeywordUtils.getKeyword(keyword);
						nKeyword = keyword;
						txt_type.setText(KeywordUtils.getKeyword(nKeyword));						
						Utils.showLongToast(GroupInfoActivity.this, textString);
					}
				}).show();
	}
	public void change_image(){
		 
		new ImagePickerDialog(this,
				ImagePickerDialog.THEME_HOLO_LIGHT,
				new ImagePickerDialog.OnImageSetListener() {
					@Override
					public void onImageSelect(int imageid) {
						nImage = imageid;
					//	img_user.setImageResource(ImagesUtils.images[imageid]);
				        img_photo.setImageResource(ImagesUtils.images[nImage]);						
					}
				}).show();	
	}	

	public void joinGroup(AskInfo uinfo){
		int nDestid = uinfo.destid;
		if(nDestid == GloableParams.m_szUserId){//判断是否是自己
		//	startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "不能添加自己"));
		//	return;
		}	
		if(GroupManger.getMemberPower( groupType, GloableParams.m_szUserId, nGroupId)>0){//已经是成员了		
			//String str = "你没有添加成员的权限:\n" + groupId +":"+ groupType +":"+ GloableParams.m_szUserId;
			//Utils.showLongToast(AddnewMemberActivity.this, str);
			return;
		}		
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(GroupInfoActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在发送请求...").show();		
		AskClient.AskJoinGroup(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(GroupInfoActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在发送请求...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(GroupInfoActivity.this, str);
					}
				});	
			}
		});	 
	}		

	/**
	 * 加入到黑名单
	 * 
	 * @param username
	 */
	private void addUserToBlacklist(String username) {
		try {
			EMContactManager.getInstance().addUserToBlackList(username, true);
			Toast.makeText(getApplicationContext(), "移入黑名单成功", 0).show();
		//} catch (EaseMobException e) {
		} catch (Exception e) {			
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "移入黑名单失败", 0).show();
		}
	}
	/**
	 * 返回
	 * 
	 * @param view
	 */
	public void back(View view) {
		//finish();
		Utils.finish(GroupInfoActivity.this);		
	}
	

}
