 
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.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.edit.AddFriendDialog;
import com.lianxin.panqq.edit.MarkUserDialog;
import com.lianxin.panqq.edit.MyAlertDialog;
import com.lianxin.panqq.client.callback.askCallBack;
import com.lianxin.panqq.client.callback.getCallBack;
import com.lianxin.panqq.client.AskClient;
import com.lianxin.panqq.client.getClient;
import com.lianxin.panqq.client.getHeadClient;
import com.lianxin.panqq.client.getInfoClient;
import com.lianxin.panqq.client.markClient;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.DefineChat;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.list.bean.UserNode;
import com.lianxin.panqq.list.utils.AddressUtils;
import com.lianxin.panqq.list.utils.FriendUtils;
import com.lianxin.panqq.list.utils.UserUtil;
import com.lianxin.pubqq.chat.EMChatActivity;
import com.lianxin.panqq.utils.IPUtils;
import com.lianxin.panqq.utils.ImagesUtils;
import com.lianxin.panqq.main.EMContactManager;
//import com.lianxin.panqq.chat.entity.EMMessage;
import com.lianxin.panqq.chat.utils.ExPowerUtils;
//import com.lianxin.panqq.file.UserFileUtil;
import com.lianxin.panqq.common.bean.AskInfo;
import com.lianxin.panqq.common.bean.UserAdminInfo;
import com.lianxin.panqq.common.bean.UserDetailInfo;
import com.lianxin.panqq.common.utils.UserheadUtils;
// 用户详细信息界面
public class UserDetailInfoActivity extends BaseActivity implements OnClickListener {
	
	private TextView txt_title,txt_right;
	private ImageView img_back, img_right; 
	private ImageView img_photo;  //用户头像
	private TextView txt_userid; //用户id
	private TextView txt_username;  //用户名	
//	private TextView txt_nickname; //用户昵称
	private TextView txt_sex;  //用户性别
	private TextView txt_age;  //用户年龄
	private TextView txt_address;   //用户地区
	private TextView txt_animal; //属相	
	private TextView txt_blood; //血型
	private TextView txt_star; //星座
	private TextView txt_realname; //真实姓名	
	private TextView txt_school;  //毕业学校		
	private TextView txt_employ;	  //工作单位
	private TextView txt_profess;  //职业		
	private TextView txt_birthyear;	  //出生年月
	private TextView txt_birthday;	  //生日
	private TextView txt_tele;	  //固定电话
	private TextView txt_phone;  //手机号码		
	private TextView txt_email;	  //电子邮件
	private TextView txt_page;	  //个人主页
	
	private TextView txt_timelong;   //在线时长
	private TextView txt_point;   //用户经验
	private TextView txt_score;  //用户积分
	private TextView txt_money;  //用户金钱
		
	private Button btn_satrtchat;
	private Button btn_addfriend;
	private Button btn_toblack;
	private Button btn_jubao;
//	private Button btn_setsight;
//	private Button btn_setguard;

	private int nUserId = 10100;//用户唯一id
	private String strUserName = "情已停机";
	private int iImage = 10;
	private long iUserHead = 0;
	private String strSex = "男";	
	private int iAge = 20;
	private int iAddress = 10;  //本地读取的是地址代码
	private String strAddress = "";//远程传送的是具体的地址字符串

	private String strRealName = "";
	private String strSchool = "";	
	private String strEmploy = "";
	private String strProFess = "";
	private String strAnimal = "";	
	private String strBlood = "";	
	private String strStar = "";
	private String strEmail = "";
	private String strSignaTure = "";  //个性签名	
//	private int iAnimal = 0;	
//	private int iBlood = 0;
//	private int iProFess = 0;
//	private int iStar = 0;		

	private int iYear = 0;
	private int iDay = 0;
	private String strTele = "";
	private String strPhone = "";	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userdetailinfo);
		
		initControl();
		initView();
		initData();
		setListener();

		getRemoteMemberInfo();		
	}

	protected void initControl() {
		txt_title = (TextView) findViewById(R.id.txt_title);
		//txt_title.setText("用户查找");
		img_back = (ImageView) findViewById(R.id.img_back);
		img_back.setVisibility(View.VISIBLE);	
		txt_userid = (TextView)findViewById(R.id.tv_user_userid);
		txt_username = (TextView)findViewById(R.id.tv_user_username);		
		img_photo = (ImageView)findViewById(R.id.iv_user_image);
		txt_sex = (TextView)findViewById(R.id.tv_user_sex);
		txt_age = (TextView)findViewById(R.id.tv_user_age);
		txt_address = (TextView)findViewById(R.id.tv_user_address);

		txt_timelong = (TextView) findViewById(R.id.tv_user_timelong);		
		txt_point = (TextView) findViewById(R.id.tv_user_point);
		txt_score = (TextView) findViewById(R.id.tv_user_score);
		txt_money = (TextView) findViewById(R.id.tv_user_money);	
	
		txt_animal = (TextView) findViewById(R.id.tv_user_animal);		
		txt_blood = (TextView) findViewById(R.id.tv_user_blood);
		txt_star = (TextView) findViewById(R.id.tv_user_star);
		txt_realname = (TextView) findViewById(R.id.tv_user_realname);		
		txt_school = (TextView) findViewById(R.id.tv_user_school);		
		txt_employ = (TextView) findViewById(R.id.tv_user_employ);
		txt_profess = (TextView) findViewById(R.id.tv_user_profess);
		txt_birthyear = (TextView) findViewById(R.id.tv_user_birthyear);
		txt_birthday = (TextView) findViewById(R.id.tv_user_birthday);
		txt_tele = (TextView) findViewById(R.id.tv_user_tele);	
		txt_phone = (TextView) findViewById(R.id.tv_user_phone);
		txt_email = (TextView) findViewById(R.id.tv_user_email);
		txt_page = (TextView) findViewById(R.id.tv_user_page);
		
		btn_satrtchat = (Button)findViewById(R.id.btn_startchat);
		btn_addfriend = (Button)findViewById(R.id.btn_addfriend);
		btn_toblack = (Button)findViewById(R.id.btn_toblack);
		btn_jubao = (Button)findViewById(R.id.btn_jubao);
		
		//lay_right = (LinearLayout)findViewById(R.id.layout_right);
		txt_right = (TextView)findViewById(R.id.tv_right);
	}

	protected void initView() {
		nUserId = getIntent().getIntExtra("Info_UserId", 10010);
		//判断是否是自己
	//	if(userId.equals(MyApp.getInstance().getUser())){				
		if(nUserId == GloableParams.m_szUserId){
			txt_right.setText("我的故事");
			btn_satrtchat.setVisibility(View.GONE);
			btn_addfriend.setVisibility(View.GONE);
			btn_toblack.setVisibility(View.GONE);
			btn_jubao.setVisibility(View.GONE);
		}
		//判断是否是好友，如果是就把加好友隐藏
		//if(MyApp.getInstance().getContactList().contains(userId)){		
		if(FriendUtils.getFriendList(nUserId)>0){
		//	btn_addfriend.setVisibility(View.GONE);
		}
	
	}
	
	protected void initData() {

		nUserId = getIntent().getIntExtra("Info_UserId", 10010);		
		strUserName = getIntent().getStringExtra("Info_UserName");
		iAge = getIntent().getIntExtra("Info_Age", 20);
		iAddress = getIntent().getIntExtra("Info_Address", 10);
		//strAddress = getIntent().getStringExtra("Info_strAddress");		
		iImage = getIntent().getIntExtra("Info_Image", 23);
		strSex = getIntent().getStringExtra("Info_Sex");
		//如果是本地数据，则读取本地文件
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){		
		    UserNode thisUser = UserUtil.readOneUser(UserDetailInfoActivity.this, nUserId);	
			strUserName = thisUser.getName();
			iAge = thisUser.age;
			iAddress = thisUser.address;
			strAddress = thisUser.straddress;				
			iImage = thisUser.imageid;
			strSex = thisUser.sex;			     			     
		}
		txt_userid.setText(""+nUserId); 
		txt_username.setText(strUserName);	
//		txt_nickname.setText(strNickName);		
        if(iImage<0) iImage = -iImage;		
        if(iImage>80) iImage = iImage%80;
        img_photo.setImageResource(ImagesUtils.images[iImage]);	
      //  if(iSex==2)	txt_sex.setText("女");
      //  else	txt_sex.setText("男");
        txt_sex.setText(strSex);       
        txt_age.setText(""+iAge);
        if(iAddress>0)
		    txt_address.setText(AddressUtils.getAddress(iAddress));        
        else
		    txt_address.setText(strAddress); 
  
		txt_realname.setText(strRealName);
		txt_school.setText(strSchool);
		txt_employ.setText(strEmploy);	
		txt_profess.setText(strProFess);
		txt_animal.setText(strAnimal);		
		txt_blood.setText(strBlood);
		txt_star.setText(strStar);
 
		txt_birthyear.setText(""+iYear);
		if(iDay>0)
		txt_birthday.setText(""+iDay/100+"月"+iDay%100+"日");		
		txt_tele.setText(strTele);	
		txt_phone.setText(strPhone);
		txt_email.setText(strEmail);	
		txt_page.setText(strProFess);
		//	txt_info.setText(strSignaTure);	       

	}	

	protected void resetData() {
		txt_userid.setText(""+nUserId); 
		txt_username.setText(strUserName);		
        if(iImage<0) iImage = -iImage;		
        if(iImage>80) iImage = iImage%80;
        img_photo.setImageResource(ImagesUtils.images[iImage]);	
		txt_address.setText(AddressUtils.getAddress(iAddress));	       	 
        txt_sex.setText(strSex);
        txt_age.setText(""+iAge);        
	
		txt_realname.setText(strRealName);
		txt_school.setText(strSchool);
		txt_employ.setText(strEmploy);	
		txt_profess.setText(strProFess);
		txt_animal.setText(strAnimal);		
		txt_blood.setText(strBlood);
		txt_star.setText(strStar);
 
		txt_birthyear.setText(""+iYear);
		if(iDay>0)		
		txt_birthday.setText(""+iDay/100+"月"+iDay%100+"日");		
		txt_tele.setText(strTele);	
		txt_phone.setText(strPhone);
		txt_email.setText(strEmail);	
		txt_page.setText(strProFess);
		//	txt_info.setText(strSignaTure);	
        if(iUserHead > 0x10000){
        	int mode = UserheadUtils.showUserHead(nUserId,iUserHead,img_photo);
        	if(mode>1)
        	getRemoteUserHead();
        }		
	}	
	
	protected void setListener() {
		img_back.setOnClickListener(this);		
		/*lay_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
			}
		});*/
		btn_satrtchat.setOnClickListener(this);
		btn_addfriend.setOnClickListener(this); 
		btn_toblack.setOnClickListener(this);
		btn_jubao.setOnClickListener(this);		
	}	
	

	
	@Override
	public void onClick(View arg0) {
		int id = arg0.getId();
		if (id == R.id.img_back) {
			Utils.finish(UserDetailInfoActivity.this);
			//finish();
			//finish(UserInfoActivity.this);
		} else if (id == R.id.btn_startchat) {
			Intent intent = new Intent(this, EMChatActivity.class);
			intent.putExtra("Chat_ID", nUserId);
			intent.putExtra("Chat_Type", DefineChat.ChatType_USER);
			intent.putExtra("Chat_Name", strUserName);
			startActivity(intent);
		} else if (id == R.id.btn_addfriend) {
			AskInfo uinfo = new AskInfo(nUserId, strUserName, iImage);
			new AddFriendDialog(UserDetailInfoActivity.this, uinfo,
					new AddFriendDialog.OnSetListener() {
						@Override
						public void onSelect(AskInfo askInfo) {
							addFriend(askInfo);
						}
					}).show();
		} else if (id == R.id.btn_toblack) {
			new MyAlertDialog(UserDetailInfoActivity.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_jubao) {
			new MarkUserDialog(UserDetailInfoActivity.this, nUserId, strUserName,
					new MarkUserDialog.OnSetListener() {
						@Override
						public void onSelect(int option) {
							MarkUser(option);
							// txt_power.setText(""+admin);
						}

						@Override
						public void onSelect(int count, int max) {
							String str = "投票成功。\n你今天已经投票" + count + "次，";
							int sub = max - count;
							if (count < max)
								str += "\n还可以投票" + sub + "次\n";
							else
								str += "\n每天的投票最大次数为20+金钻等级。\n超过的每次投票花费0.2个PQ币。\n";
							Utils.showLongToast(UserDetailInfoActivity.this, str);
						}
					}).show();
		}
	} 

	private void getRemoteMemberInfo(){
		int nDestid = nUserId;
		int nSelfid = GloableParams.m_szUserId;		
		if(nDestid == GloableParams.m_szUserId){//判断是否是自己
		//	startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "不能添加自己"));
		//	return;
		}	
		if(FriendUtils.getFriendList(nDestid)>0){//判断是否已经是好友
		//	startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "此用户已是你的好友"));
		//	return;
		}
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(UserDetailInfoActivity.this, "你还没有登录服务器!");
			return;
		}
	//	int permit = getPermitPower( 1024 );
	//	if(permit<2)
	//		return;
		getLoadingDialog("正在刷新数据...").show();		
		getInfoClient.GetUserInfo(1001,nUserId,1,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {	
				final UserDetailInfo userDetailInfo = new UserDetailInfo(recvBuf);				
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
						nUserId = userDetailInfo.m_iUserId;//用户唯一id	
						strUserName = userDetailInfo.m_cUserName;
						iImage = userDetailInfo.m_imyIcon;
						strSex = userDetailInfo.m_cSex;	
						iAge = userDetailInfo.m_iAge;
						iAddress = userDetailInfo.m_iCity;
						iUserHead = userDetailInfo.m_iUserHead;
						
						strRealName = userDetailInfo.m_cRealName;
						strSchool = userDetailInfo.m_strCollEge;
						strEmploy = userDetailInfo.m_strEmploy;
						strProFess = userDetailInfo.m_strProFess;						
						strAnimal = userDetailInfo.m_cAnimal;	
						strBlood = userDetailInfo.m_cBlood;
						strStar = userDetailInfo.m_cStar;		
	
						strSignaTure = userDetailInfo.m_strSignaTure;  //个性签名		
						iYear = userDetailInfo.m_iYear;
						iDay = userDetailInfo.m_iBirthDay;
						strTele = userDetailInfo.m_strTele;
						strPhone = userDetailInfo.m_strPhone;
						strEmail = userDetailInfo.m_strMail;
						//strPage = userDetailInfo.m_strMail;						
						resetData();					
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				//getLoadingDialog("正在刷新数据...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
						Utils.showLongToast(UserDetailInfoActivity.this, str);
					}
				});	
			}
		});		
	
	}	  

	private void getRemoteUserHead(){

		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器		
			Utils.showLongToast(UserDetailInfoActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在刷新头像...").show();	
		getHeadClient.GetUserHead(DefineUser.TYPE_FRIEND,nUserId,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {

				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新头像...").dismiss();					
						UserheadUtils.showUserHead(nUserId,iUserHead,img_photo);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {

				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新头像...").dismiss();						
					}
				});	
			}
		});	 		
	
	}	
	
	int ret1 = 1;	
	private int getPermitPower( int option ){
		int hePower = ExPowerUtils.getUserPower( (long)option, 1 );
		int myPower = ExPowerUtils.getGreenDiamond( );	
		if(hePower>0){
			if( hePower>245 || (myPower-hePower)<10 ){
				new MyAlertDialog(UserDetailInfoActivity.this,"无权查看",
						"对方的蓝盾级别太高了！",
						new MyAlertDialog.OnSetListener() {
							@Override
							public void onSelect(int comm) {								
							}
						}).show();					
				return 0;
			}else if((myPower-hePower)<10){
				new MyAlertDialog(UserDetailInfoActivity.this,"权限不够",
						"请升级自己的绿卡级别！",
						new MyAlertDialog.OnSetListener() {
							@Override
							public void onSelect(int comm) {								
							}
						}).show();					
				return 0;
			}else if((myPower-hePower)>=100){
					return 3;				
			}else{
				ret1 = 1;
				new MyAlertDialog(UserDetailInfoActivity.this,"查看对方信息？",
						"你愿意支付3个PQ币吗？",
						new MyAlertDialog.OnSetListener() {
							@Override
							public void onSelect(int comm) {
								ret1 = 2;								
							}
						}).show();				
				return ret1;
			}
		}else if( myPower >= 100){
			return 3;	
		}else{
			ret1 = 1;
			new MyAlertDialog(UserDetailInfoActivity.this,"查看对方信息？",
					"你愿意支付3个PQ币吗？",
					new MyAlertDialog.OnSetListener() {
						@Override
						public void onSelect(int comm) {
							ret1 = 2;								
						}
					}).show();				
			return ret1;		
		}		
		//return 1;
	}
	/**
	 *  添加contact
	 * @param view
	 */

	public void addFriend(AskInfo uinfo){
		int nDestid = uinfo.destid;
		if(nDestid == GloableParams.m_szUserId){//判断是否是自己
		//	startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "不能添加自己"));
		//	return;
		}	
		if(FriendUtils.getFriendList(nDestid)>0){//判断是否已经是好友
		//	startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "此用户已是你的好友"));
		//	return;
		}
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(UserDetailInfoActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在发送请求...").show();		
		AskClient.AskAddFriend(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(UserDetailInfoActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在发送请求...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(UserDetailInfoActivity.this, str);
					}
				});	
			}
		});	 
	}			
	//private void MarkUser( int nUserId, int option ){
	private void MarkUser( int option ){
		int nDestid = nUserId;
		if(nDestid == GloableParams.m_szUserId){//判断是否是自己
		//	return;
		}	
		if(FriendUtils.getFriendList(nDestid)>0){//判断是否已经是好友
		//	return;
		}
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(UserDetailInfoActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在投票中...").show();		
		markClient.putMarkVote(DefineUser.TYPE_FRIEND,nUserId,option,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				//getLoadingDialog("正在刷新数据...").dismiss();	
				final int count = recvBuf[4];	
				final int max = recvBuf[8];				
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在投票中...").dismiss();
						String str = "投票成功。\n你今天已经投票"+count+"次，";
						int sub = max - count;
						if(count<max)
							str += "\n还可以投票"+sub +"次\n";
						else
							str += "\n每天的投票最大次数为20+金钻等级。\n超过的每次投票花费0.2个PQ币。\n";						
						Utils.showLongToast(UserDetailInfoActivity.this, str);
						
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				//getLoadingDialog("正在刷新数据...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在投票中...").dismiss();							
						Utils.showLongToast(UserDetailInfoActivity.this, str);
					}
				});	
			}
		});	 
	}	

	/**
	 * 返回
	 * 
	 * @param view
	 */
	public void back(View view) {
		finish();
		Utils.finish(UserDetailInfoActivity.this);		
	}

}
