 
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.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.utils.UserheadUtils;
//用户超级信息界面
public class UserSuperInfoActivity 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_ip;   //用户ip	
	private TextView txt_timelong;   //在线时长
	private TextView txt_point;   //用户经验
	private TextView txt_score;  //用户积分
	private TextView txt_money;  //用户金钱
	private TextView txt_diamond;  //用户金钱
	private TextView txt_market;  //用户金钱
	private TextView txt_report;  //用户金钱

	private TextView txt_flower; //鲜花	
	private TextView txt_ege; //鸡蛋
	private TextView txt_save; //救赎
	private TextView txt_kick; //罚点		
	private TextView txt_gold;  //金钻		
	private TextView txt_green;	  //绿卡
	private TextView txt_blue;  //蓝盾
	private TextView txt_red;	  //红运
	private TextView txt_logip;  //最后登录ip
	private TextView txt_logtime;  //最后登录时间	
	private TextView txt_visit;  //访问次数
	private TextView txt_speak;  //发言次数	
		
	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 image = 10;
	private long userHead = 0;	
	private String strNickName = "赵八方";
	private int iSex = 1;
	private String Sex = "男";	
	private int iAge = 20;
	private int iAddress = 10;  //本地读取的是地址代码
	private String strAddress = "";//远程传送的是具体的地址字符串

	private int iTimeLong = 0;		
	private long dwLastTime = 0;
	private long dwLastIp = 0;	
	private int iPoint = 0;	
	private int iScore = 1000;
	private int iMoney = 100;
//	private long dwDiamond = 1000;
//	private long dwReport = 100;

	private int iGoldDiamond = 58;  //金钻	
	private int iGreenDiamond = 32;	  //绿卡
	private int iBuleDiamond = 1000;  //蓝盾
	private int iRedDiamond = 100;	  //红运
	private int iVisitCount;  //访问次数
	private int iSpeakCount;  //申请次数

	private int iFlower; //鲜花
	private int iEge; //鸡蛋
	private int iSave; //黑点
	private int iKick; //罚点	
	
	private String  m_TimeLong;  //在线时长
	private String m_Lastip; //最后登录IP
	private String m_Lasttime; //最后登录时间
	private String m_Address;  //用户地址		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_usersuperinfo);
		
		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_ip = (TextView) findViewById(R.id.tv_user_ip);
		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_diamond = (TextView) findViewById(R.id.tv_user_diamond);
		txt_market = (TextView) findViewById(R.id.tv_user_market);
		txt_report = (TextView) findViewById(R.id.tv_user_report);
	
		txt_flower = (TextView) findViewById(R.id.tv_user_flower);		
		txt_ege = (TextView) findViewById(R.id.tv_user_ege);
		txt_save = (TextView) findViewById(R.id.tv_user_save);
		txt_kick = (TextView) findViewById(R.id.tv_user_kick);		
		txt_gold = (TextView) findViewById(R.id.tv_user_gold);		
		txt_green = (TextView) findViewById(R.id.tv_user_green);
		txt_blue = (TextView) findViewById(R.id.tv_user_blue);
		txt_red = (TextView) findViewById(R.id.tv_user_red);
		txt_logip = (TextView) findViewById(R.id.tv_user_logip);
		txt_logtime = (TextView) findViewById(R.id.tv_user_logtime);	
		txt_visit = (TextView) findViewById(R.id.tv_user_visit);
		txt_speak = (TextView) findViewById(R.id.tv_user_speak);
		
		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(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(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");		
		image = getIntent().getIntExtra("Info_Image", 23);
		Sex = getIntent().getStringExtra("Info_Sex");
		//如果是本地数据，则读取本地文件
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){		
		    UserNode thisUser = UserUtil.readOneUser(UserSuperInfoActivity.this, nUserId);	
			strUserName = thisUser.getName();
			iAge = thisUser.age;
			iAddress = thisUser.address;
			strAddress = thisUser.straddress;				
			image = thisUser.imageid;
			Sex = thisUser.sex;			     			     
		}
		txt_userid.setText(""+nUserId); 
		txt_username.setText(strUserName);	
//		txt_nickname.setText(strNickName);		
        if(image<0) image = -image;		
        if(image>80) image = image%80;
        img_photo.setImageResource(ImagesUtils.images[image]);	
      //  if(iSex==2)	txt_sex.setText("女");
      //  else	txt_sex.setText("男");
        txt_sex.setText(Sex);       
        txt_age.setText(""+iAge);
        if(iAddress>0)
		    txt_address.setText(AddressUtils.getAddress(iAddress));        
        else
		    txt_address.setText(strAddress); 
        
		txt_flower.setText(""+iFlower);	
		txt_ege.setText(""+iEge);
		txt_save.setText(""+iSave);
		txt_kick.setText(""+iKick);	
		txt_gold.setText(""+iGoldDiamond);		
		txt_green.setText(""+iGreenDiamond);
		txt_blue.setText(""+iBuleDiamond);
		txt_red.setText(""+iRedDiamond);
		txt_logip.setText(strAddress);
		txt_logtime.setText(strAddress);	
		txt_visit.setText(""+iVisitCount);
		txt_speak.setText(""+iSpeakCount);   
	}	

	protected void resetData() {
		txt_userid.setText(""+nUserId); 
		txt_username.setText(strUserName);		
        if(image<0) image = -image;		
        if(image>80) image = image%80;
        img_photo.setImageResource(ImagesUtils.images[image]);	
		txt_address.setText(AddressUtils.getAddress(iAddress));	       	 
        txt_sex.setText(Sex);
        txt_age.setText(""+iAge);        
		
		//txt_diamond.setText(strDiamond);		
		//txt_timelong.setText(""+iTimeLong);		
		txt_ip.setText(IPUtils.longToIP(dwLastIp));
		txt_point.setText(""+iPoint);
		txt_score.setText(""+iScore);
		txt_money.setText(""+iMoney+100);			
		txt_timelong.setText(m_TimeLong);	

		txt_flower.setText(""+iFlower);	
		txt_ege.setText(""+iEge);
		txt_save.setText(""+iSave);
		txt_kick.setText(""+iKick);	
		txt_gold.setText(""+iGoldDiamond);		
		txt_green.setText(""+iGreenDiamond);
		txt_blue.setText(""+iBuleDiamond);
		txt_red.setText(""+iRedDiamond);
		txt_logip.setText(m_Lasttime);
		txt_logtime.setText(strAddress);	
		txt_visit.setText(""+iVisitCount);
		txt_speak.setText(""+iSpeakCount); 		

        if(userHead > 0x10000){
        	int mode = UserheadUtils.showUserHead(nUserId,userHead,img_photo);
        	if(mode>1)
        	getRemoteUserHead();
        }		
	}	
	
	protected void setListener() {
		img_back.setOnClickListener(this);		

		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(UserSuperInfoActivity.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", strNickName);
			startActivity(intent);
		} else if (id == R.id.btn_addfriend) {
			AskInfo uinfo = new AskInfo(nUserId, strUserName, image);
			new AddFriendDialog(UserSuperInfoActivity.this, uinfo,
					new AddFriendDialog.OnSetListener() {
						@Override
						public void onSelect(AskInfo askInfo) {
							addFriend(askInfo);
						}
					}).show();
		} else if (id == R.id.btn_toblack) {
			new MyAlertDialog(UserSuperInfoActivity.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(UserSuperInfoActivity.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(UserSuperInfoActivity.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(UserSuperInfoActivity.this, "你还没有登录服务器!");
			return;
		}
	//	int permit = getPermitPower( 1024 );
	//	if(permit<2)
	//		return;
		getLoadingDialog("正在刷新数据...").show();	
		getClient.GetAdminUser( nUserId,nSelfid,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				//getLoadingDialog("正在刷新数据...").dismiss();	
				//SearUserList searlist = new SearUserList(recvBuf);	
				final UserAdminInfo userEditInfo = new UserAdminInfo(recvBuf);				
				//final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
						strUserName = userEditInfo.username;						
						strNickName = userEditInfo.nickname;
						iAddress = userEditInfo.address;
						iAge = userEditInfo.age;
						Sex = userEditInfo.sex;						
						image = userEditInfo.imageid;	
						userHead = userEditInfo.userhead;						
			       		iTimeLong = (int) userEditInfo.m_iTimeLong;
			       		dwLastIp = userEditInfo.m_iLastIP;			       		
		        		//iPoint = userEditInfo.m_iPoint/100;	
		        		//iScore = userEditInfo.m_iScore/100;
		        		//iMoney = userEditInfo.m_iMoney/100;
		        		iPoint = userEditInfo.m_iPoint;	
		        		iScore = userEditInfo.m_iScore;
		        		iMoney = userEditInfo.m_iMoney/1000;
		        		
		        		iFlower = userEditInfo.m_iFlower;			       		
		        		iEge = userEditInfo.m_iEge;	
		        		iSave = userEditInfo.m_iSave;
		        		iKick = userEditInfo.m_iKick;	
		        		iGoldDiamond = userEditInfo.m_iGoldDiamond;			       		
		        		iGreenDiamond = userEditInfo.m_iGreenDiamond;	
		        		iBuleDiamond = userEditInfo.m_iBlueDiamond;
		        		iRedDiamond = userEditInfo.m_iRedDiamond;
		        		iVisitCount = userEditInfo.m_iVisitCount;
		        		iSpeakCount = userEditInfo.m_iSpeakCount;	
		        		
		        		m_TimeLong = userEditInfo.m_TimeLong;  //在线时长
		        		m_Lastip = userEditInfo.m_Lastip; //最后登录IP
		        		m_Lasttime = userEditInfo.m_Lasttime; //最后登录时间
		        		m_Address = userEditInfo.m_Address;  //用户地址			        		

						resetData();						
						//Utils.showLongToast(MemberInfoActivity.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(UserSuperInfoActivity.this, str);
					}
				});	
			}
		});	 		
	
	}	  

	private void getRemoteUserHead(){

		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器		
			Utils.showLongToast(UserSuperInfoActivity.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,userHead,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(UserSuperInfoActivity.this,"无权查看",
						"对方的蓝盾级别太高了！",
						new MyAlertDialog.OnSetListener() {
							@Override
							public void onSelect(int comm) {								
							}
						}).show();					
				return 0;
			}else if((myPower-hePower)<10){
				new MyAlertDialog(UserSuperInfoActivity.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(UserSuperInfoActivity.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(UserSuperInfoActivity.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(UserSuperInfoActivity.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(UserSuperInfoActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在发送请求...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(UserSuperInfoActivity.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(UserSuperInfoActivity.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(UserSuperInfoActivity.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(UserSuperInfoActivity.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(UserSuperInfoActivity.this);		
	}

}
