package com.lianxin.pubqq.setup;

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.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.pubqq.main.MyApp;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.client.callback.getCallBack;
import com.lianxin.panqq.utils.CheckGetUtil;
import com.lianxin.panqq.widget.CheckView;
import com.lianxin.panqq.client.setInfoClient;

//修改登录密码
public class SetupRegoutActivity extends BaseActivity implements OnClickListener {
	private TextView txt_servip,txt_userid,txt_username;		
	private EditText txt_password, txt_checkcode;	
	private Button btn_confirm;	
    private CheckView mMyView = null;  
    private String[] checkNum = new String[4];  //获取每次更新的验证码，可用于判断用户输入是否正确  	

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.setup_regoff);
		super.onCreate(savedInstanceState);
		
		initControl();
	//	initView();
		initData();
		setListener();		
	}

//	@Override
	protected void initControl() {

		txt_servip = (TextView) findViewById(R.id.tv_user_servip);
		txt_userid = (TextView) findViewById(R.id.tv_user_userid);
		txt_username = (TextView) findViewById(R.id.tv_user_username);		
		txt_password = (EditText) findViewById(R.id.tv_user_password);		
		txt_checkcode = (EditText) findViewById(R.id.tv_user_checkcode);		
		btn_confirm = (Button) findViewById(R.id.btn_confirm);	
	       mMyView = (CheckView)findViewById(R.id.checkView);  
	        //初始化验证码  
	       checkNum = mMyView.getValidataAndSetImage();  
	        mMyView.setOnClickListener(new OnClickListener() {  
	              
	            @Override  
	            public void onClick(View v) {  
	                //重新初始化验证码  
	            	checkNum = mMyView.getValidataAndSetImage();           
	            }  
	        });			

	}

//	@Override
	protected void initView() {

	}

//	@Override
	protected void initData() {
		int id = GloableParams.m_szUserId;		
		txt_userid.setText(""+id);
		txt_servip.setText(GloableParams.m_szServerIp);	
		txt_username.setText(GloableParams.m_szUserName);		
	}

//	@Override
	protected void setListener() {
		btn_confirm.setOnClickListener(this);	
	
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			super.onBackPressed();
		} else if (id == R.id.btn_confirm) {
			getConfirm();
		}
	}

	private void getConfirm() {
		String password = txt_password.getText().toString().trim();		

		if (TextUtils.isEmpty(password)){
			Utils.showShortToast(SetupRegoutActivity.this, "请填写登录密码！");
			txt_password.requestFocus();			
            return;
	    }
		if (password.length() < 5){
			Utils.showShortToast(SetupRegoutActivity.this, "登录密码应该5位以上！");	
			txt_password.requestFocus();			
            return;
	    }		

        String userInput = txt_checkcode.getText().toString();
        if(!CheckGetUtil.checkNum(userInput, checkNum)){
			Utils.showShortToast(SetupRegoutActivity.this, "验证码不对！");	
			txt_checkcode.requestFocus();			
            return;
	    }
 
        
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showShortToast(SetupRegoutActivity.this, "你还没有登录服务器！");				
			return;
		}
		getLoadingDialog("正在退出登录...  ").show();			
		setInfoClient.UserRegOut(GloableParams.m_szUserId,password,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				getLoadingDialog("正在退出登录...").dismiss();
				MyApp.getInstance().stopPingThread();	//停止心跳线程				
				GloableParams.m_szServerIp = "";
				GloableParams.m_szUserId = 10000;								
				MyApp.getInstance().logUserLogExit();

							
			    Intent intent = new Intent(MyApp.getInstance().getNewUserLoginBroadcastAction()); 
			    intent.putExtra("mode", 3);							    
				MyApp.getInstance().sendBroadcast(intent); 
				
				Utils.finish(SetupRegoutActivity.this);	
				Utils.showLongToast(SetupRegoutActivity.this, "已经成功退出登录");				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				//getLoadingDialog("正在刷新数据...").dismiss();
				//final String str = message;
	
			}
		});			
   
	}

}
