package com.lianxin.pubqq.setup;

import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.pubqq.main.MyApp;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
//import com.lianxin.panqq.link.DefineLink;
import com.lianxin.panqq.list.utils.FriendUtils;
import com.lianxin.panqq.client.callback.getCallBack;
import com.lianxin.panqq.client.getInfoClient;
import com.lianxin.panqq.client.moneyClient;
import com.lianxin.panqq.edit.MoneyDiamondDialog;
import com.lianxin.panqq.edit.MoneyExchargeDialog;
import com.lianxin.panqq.common.bean.UserDetailInfo;
import com.lianxin.panqq.common.bean.UserDiamondInfo;
import com.lianxin.panqq.common.bean.UserMoneyInfo;
import com.lianxin.panqq.utils.MoneyUtil;

//注册
public class SetupMoneyActivity extends BaseActivity implements OnClickListener {
	private TextView txt_title;
	private ImageView img_back;
	private Button btn_modify, btn_logout,btn_exit, btn_regoff;	
	private TextView txt_time,txt_point,txt_score,txt_money;
	private RelativeLayout lay_score,lay_money;	
	private TextView txt_green,txt_blue,txt_red,txt_gold;
	private RelativeLayout lay_green,lay_blue,lay_red,lay_gold;		
	private RelativeLayout lay_userpic;	
	private int iTimeLong = 0;
	private int iPoint = 0;	
	private int iScore = 1000;
	private int iMoney = 100;
	private int iGreenDiamond = 0;
	private int iBlueDiamond = 0;	
	private int iRedDiamond = 0;
	private int iGoldDiamond = 0;	

	private UserMoneyInfo myMoneyInfo = new UserMoneyInfo();	
	private UIHandler UIhandler;	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.setup_money);
		super.onCreate(savedInstanceState);
		
		initControl();
		initData();		
		initView();

		setListener();
		getRemoteMoneyInfo();		
	}

	protected void initControl() {

		img_back = (ImageView) findViewById(R.id.img_back);
		btn_modify = (Button) findViewById(R.id.btn_modify);
		btn_logout = (Button) findViewById(R.id.btn_logout);
		btn_exit = (Button) findViewById(R.id.btn_exit);
		btn_regoff = (Button) findViewById(R.id.btn_regoff);
		txt_time = (TextView) findViewById(R.id.tv_user_time);
		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);	
		lay_score = (RelativeLayout) findViewById(R.id.rl_user_score);
		lay_money = (RelativeLayout) findViewById(R.id.rl_user_money);		
		txt_green = (TextView) findViewById(R.id.tv_diamond_green);
		txt_blue = (TextView) findViewById(R.id.tv_diamond_blue);
		txt_red = (TextView) findViewById(R.id.tv_diamond_red);
		txt_gold = (TextView) findViewById(R.id.tv_diamond_gold);	
		lay_green = (RelativeLayout) findViewById(R.id.rl_diamond_green);
		lay_blue = (RelativeLayout) findViewById(R.id.rl_diamond_blue);
		lay_red = (RelativeLayout) findViewById(R.id.rl_diamond_red);
		lay_gold = (RelativeLayout) findViewById(R.id.rl_diamond_gold);		

	}

	protected void initView() {
		//int m_point = iPoint+DefineLink.m_szPoint/1000;
		//int m_score = iScore+DefineLink.m_szScore/1000;
		int m_point = iPoint+MoneyUtil.getMyPoint();
		int m_score = iScore+MoneyUtil.getMyScore();		
		int m_money = iMoney;		
		txt_time.setText(""+iTimeLong);
		txt_point.setText(""+m_point);
		txt_score.setText(""+m_score);
		txt_money.setText(""+m_money);		
		txt_green.setText(""+iGreenDiamond);
		txt_blue.setText(""+iBlueDiamond);
		txt_red.setText(""+iRedDiamond);
		txt_gold.setText(""+iGoldDiamond);
		//int thisTimeLong = iTimeLong+DefineLink.m_szTimelong;
		int thisTimeLong = iTimeLong+MoneyUtil.getTimelong();		
		int iHours = thisTimeLong/3600;
		int iSecon = thisTimeLong%3600;
		int iMinu = iSecon/60;
		iSecon %= 60;
		txt_time.setText(""+iHours+":"+iMinu+":"+iSecon);		
	}

	protected void initData() {
		iTimeLong = GloableParams.myUserInfo.m_iTimeLong;
		iPoint = GloableParams.myUserInfo.m_iPoint/100;	
		iScore = GloableParams.myUserInfo.m_iScore/100;
		iMoney = GloableParams.myUserInfo.m_iMoney/100;
		iGreenDiamond = GloableParams.myUserInfo.m_green;
		iBlueDiamond = GloableParams.myUserInfo.m_blue;	
		iRedDiamond = GloableParams.myUserInfo.m_red;
		iGoldDiamond = GloableParams.myUserInfo.m_gold;		
	}

	protected void setListener() {

		img_back.setOnClickListener(this);	
		btn_modify.setOnClickListener(this);
		btn_logout.setOnClickListener(this);
		btn_exit.setOnClickListener(this);
		btn_regoff.setOnClickListener(this);
		lay_score.setOnClickListener(this); 
		lay_money.setOnClickListener(this);		
		lay_gold.setOnClickListener(this); 
		lay_green.setOnClickListener(this);
		lay_blue.setOnClickListener(this); 
		lay_red.setOnClickListener(this);		
	}

    private class UIHandler extends Handler{  
        @Override  
        public void handleMessage(Message msg) {  
            super.handleMessage(msg);  
            Bundle bundle = msg.getData(); 
            int ACTION = bundle.getInt("action"); 
            int type = bundle.getInt("count");             
            byte[] recvBuf = bundle.getByteArray("ByteArray");  
            if(ACTION==1){
            	UserMoneyInfo moneyInfo = new UserMoneyInfo(recvBuf); 
            	myMoneyInfo = moneyInfo;           	
        		iTimeLong = moneyInfo.m_iTimeLong;
        		iPoint = moneyInfo.m_iPoint/100;	
        		iScore = moneyInfo.m_iScore/100;
        		iMoney = moneyInfo.m_iMoney/100;
        		long iDiamond = moneyInfo.m_iDiamond; 
        		iGreenDiamond = (int) (iDiamond&0xff);
        		iBlueDiamond = (int) ((iDiamond>>8)&0xff);	
        		iRedDiamond = (int) ((iDiamond>>16)&0xff);;
        		iGoldDiamond = (int) ((iDiamond>>24)&0xff);      		
        		initView();		    
            }else{
   	
            }
      
        }  
    }	
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(SetupMoneyActivity.this);
		} else if (id == R.id.btn_modify) {
			Intent intent = new Intent(this, SetupMoneyPswActivity.class);
			startActivity(intent);
		} else if (id == R.id.btn_logout) {
			MyApp.getInstance().userLogOffline(0);
		} else if (id == R.id.btn_exit) {
			Utils.finish(SetupMoneyActivity.this);
			MyApp.getInstance().exit();
		} else if (id == R.id.btn_regoff) {
			Intent intent2 = new Intent(this, SetupRegoutActivity.class);
			startActivity(intent2);
		} else if (id == R.id.rl_user_score) {
			new MoneyExchargeDialog(SetupMoneyActivity.this, myMoneyInfo,
					new MoneyExchargeDialog.OnSetListener() {
						@Override
						public void onSelect(int admin) {
							moneyExcharge(admin);
							// txt_power.setText(""+admin);
						}
					}).show();
		} else if (id == R.id.rl_user_money) {//修改头像
			//		change_userpic();
		} else if (id == R.id.rl_diamond_green) {//修改城市
			change_diamond(1);
		} else if (id == R.id.rl_diamond_blue) {//修改城市
			change_diamond(2);
		} else if (id == R.id.rl_diamond_red) {//修改城市
			change_diamond(3);
		} else if (id == R.id.rl_diamond_gold) {//修改城市
			change_diamond(4);
		}
	}

	private void getRemoteMoneyInfo(){

		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			return;
		}	
		int nUserId = GloableParams.m_szUserId;	
		int nOldId = GloableParams.myUserInfo.m_iUserId;
		int nowtime = (int) new Date().getTime()/1000;	
		if(nUserId != nOldId){//判断是否是自己

		}else if((nowtime -10000 ) >= 20){
			
		}
		getLoadingDialog("正在刷新数据...").show();	
		UIhandler = new UIHandler();		
		getInfoClient.GetMoneyInfo(MoneyUtil.getMoneyCOUNT(),0,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {	
				getLoadingDialog("正在刷新数据...").dismiss();			
				Message msg = new Message();  
	            Bundle bundle = new Bundle(); 
	            bundle.putInt("action", 1);	            
	            bundle.putInt("count", statusCode);	            	            	
	            bundle.putByteArray("ByteArray", recvBuf);			            
	            msg.setData(bundle);  
	            UIhandler.sendMessage(msg);			
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				//getLoadingDialog("正在刷新数据...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
			//			Utils.showLongToast(MemberInfoActivity.this, str);
					}
				});	
			}
		});	 		
	
	}	
	//得到自己的DiamondInfo
	private void getRemoteDiamondInfo(){

		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			return;
		}	
		int nUserId = GloableParams.m_szUserId;	
		int nOldId = GloableParams.myUserInfo.m_iUserId;
		int nowtime = (int) new Date().getTime()/1000;	
		if(nUserId != nOldId){//判断是否是自己

		}else if((nowtime -10000 ) >= 20){
			
		}
		getLoadingDialog("正在刷新数据...").show();	
		UIhandler = new UIHandler();		
		getInfoClient.GetMoneyInfo(MoneyUtil.getMoneyDIAMOND(),0,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {	
				UserDiamondInfo userExInfo = new UserDiamondInfo(recvBuf);	
				//userExInfo.m_cUserName = "123456";				
				//GloableParams.myUserInfo = userExInfo;
				//myMoneyInfo = userExInfo;				
				//GloableParams.myUserInfo.m_cUserName = "abcdef";				
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
						initData();	
						initView();
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				//getLoadingDialog("正在刷新数据...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
			//			Utils.showLongToast(MemberInfoActivity.this, str);
					}
				});	
			}
		});	 		
	
	}
	//得到群组的DiamondInfo
	private void getRemoteGroupDiamondInfo(){

		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			return;
		}	
		int nUserId = GloableParams.m_szUserId;	
		int nOldId = GloableParams.myUserInfo.m_iUserId;
		int nowtime = (int) new Date().getTime()/1000;	
		if(nUserId != nOldId){//判断是否是自己

		}else if((nowtime -10000 ) >= 20){
			
		}
		getLoadingDialog("正在刷新数据...").show();	
		UIhandler = new UIHandler();		
		getInfoClient.GetDiamondInfo(DefineUser.TYPE_CROWD,10001,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {	
				UserDiamondInfo userExInfo = new UserDiamondInfo(recvBuf);	
				//userExInfo.m_cUserName = "123456";				
				//GloableParams.myUserInfo = userExInfo;
				//myMoneyInfo = userExInfo;				
				//GloableParams.myUserInfo.m_cUserName = "abcdef";				
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
						initData();	
						initView();
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				//getLoadingDialog("正在刷新数据...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
			//			Utils.showLongToast(MemberInfoActivity.this, str);
					}
				});	
			}
		});	 		
	
	}	
	
	//得到其他人的DiamondInfo
	private void getRemoteUserDiamondInfo(){

		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			return;
		}	
		int nUserId = GloableParams.m_szUserId;	
		int nOldId = GloableParams.myUserInfo.m_iUserId;
		int nowtime = (int) new Date().getTime()/1000;	
		if(nUserId != nOldId){//判断是否是自己

		}else if((nowtime -10000 ) >= 20){
			
		}
		getLoadingDialog("正在刷新数据...").show();	
		UIhandler = new UIHandler();		
		getInfoClient.GetDiamondInfo(DefineUser.TYPE_FRIEND,10001,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {	
				UserDiamondInfo userExInfo = new UserDiamondInfo(recvBuf);	
				//userExInfo.m_cUserName = "123456";				
				//GloableParams.myUserInfo = userExInfo;
				//myMoneyInfo = userExInfo;				
				//GloableParams.myUserInfo.m_cUserName = "abcdef";				
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
						initData();	
						initView();
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				//getLoadingDialog("正在刷新数据...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();						
			//			Utils.showLongToast(MemberInfoActivity.this, str);
					}
				});	
			}
		});	 		
	
	}	
	/**
	 * 更改地址
	 */
	public void change_diamond( int mode ){
 
		new MoneyDiamondDialog(SetupMoneyActivity.this,mode,myMoneyInfo,
				new MoneyDiamondDialog.OnSetListener() {
					@Override
					public void onSelect(int option) {
						BuyDiamond(option);
						// txt_power.setText(""+admin);							
					}
				}).show();			
	}	
	
	private void moneyExcharge( int option ){
		int nDestid = 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(SetupMoneyActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在兑换中...").show();		
		moneyClient.putMoneyExchange(option,nDestid,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				//getLoadingDialog("正在刷新数据...").dismiss();	
				//final int money = (recvBuf[4]+recvBuf[5]*0x100+recvBuf[6]*0x10000)/100;	
				//final int excharge = (recvBuf[8]+recvBuf[9]*0x100+recvBuf[10]*0x10000)/100;
				final int money = (MoneyUtil.ByteArrayToInt(recvBuf[4],recvBuf[5],recvBuf[6],recvBuf[7])+50)/100;
				final int excharge = (MoneyUtil.ByteArrayToInt(recvBuf[8],recvBuf[9],recvBuf[10],recvBuf[11])+50)/100;				
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在兑换中...").dismiss();
						txt_money.setText(""+money);
						String str = "成功兑换PQ币。\n现有金钱:"+money+"\n已兑积分:"+excharge;					
						Utils.showLongToast(SetupMoneyActivity.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(SetupMoneyActivity.this, str);
					}
				});	
			}
		});	 
	}

	private void BuyDiamond( final int option ){
		int nDestid = 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(SetupMoneyActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在升级中...").show();		
		moneyClient.putBuyDiamond(option,nDestid,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				//getLoadingDialog("正在刷新数据...").dismiss();	
				//final int money = (recvBuf[4]+recvBuf[5]*0x100+recvBuf[6]*0x10000)/100;
				int diamond = recvBuf[8];	
				if( option == 0)
					diamond = recvBuf[8];
				else if( option == 1)
					diamond = recvBuf[9];
				else if( option == 2)
					diamond = recvBuf[10];
				else if( option == 3)
					diamond = recvBuf[11];
				final int diamond1 = diamond;				
				final String strDiamond = ""+recvBuf[8]+":"+recvBuf[9]+":"+recvBuf[10]+":"+recvBuf[11];	
				final int money = (MoneyUtil.ByteArrayToInt(recvBuf[4],recvBuf[5],recvBuf[6],recvBuf[7])+50)/100;
			//	final int nType = option;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在升级中...").dismiss();
						if( option == 0)
							txt_green.setText(""+diamond1);
						else if( option == 1)
							txt_blue.setText(""+diamond1);
						else if( option == 2)
							txt_red.setText(""+diamond1);
						else if( option == 3)
							txt_gold.setText(""+diamond1);							
						String strType = "";
						if( option == 0)
							strType = "绿卡";
						else if( option == 1)
							strType = "蓝盾";
						else if( option == 2)
							strType = "红运";
						else if( option == 3)
							strType = "金钻";						
						String str = "成功升级"+strType+"。\n金钱余额:"+money+"\n钻石:"+strDiamond;					
						Utils.showLongToast(SetupMoneyActivity.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(SetupMoneyActivity.this, str);
					}
				});	
			}
		});	 
	}
	

}
