package com.lianxin.pubqq.setup;

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.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 SetupPasswordActivity extends BaseActivity implements OnClickListener {
	private TextView txt_userid;	
	private EditText txt_oldpsw, txt_password, txt_confirmpsw;	
	private EditText 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_editpsw);
		super.onCreate(savedInstanceState);
		
		initControl();
	//	initView();
		initData();
		setListener();		
	}

	protected void initControl() {

		txt_userid = (TextView) findViewById(R.id.tv_user_userid);
		txt_oldpsw = (EditText) findViewById(R.id.tv_user_oldpsw);		
		txt_password = (EditText) findViewById(R.id.tv_user_password);		
		txt_confirmpsw = (EditText) findViewById(R.id.tv_user_confirmpsw);
		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();
			}
		});		

	}

	protected void initView() {

	}

	protected void initData() {
		int id = GloableParams.m_szUserId;		
		txt_userid.setText(""+id);
	}

	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 oldpsw = txt_oldpsw.getText().toString().trim();		
		String password = txt_password.getText().toString().trim();	
		String confirmpsw = txt_confirmpsw.getText().toString().trim();
		if (TextUtils.isEmpty(oldpsw)){
			Utils.showShortToast(SetupPasswordActivity.this, "请填写旧登录密码！");
			txt_oldpsw.requestFocus();			
            return;
	    }
		if (TextUtils.isEmpty(password)){
			Utils.showShortToast(SetupPasswordActivity.this, "请填写新登录密码！");
			txt_password.requestFocus();			
            return;
	    }		
		if (TextUtils.isEmpty(confirmpsw)){
			Utils.showShortToast(SetupPasswordActivity.this, "请再次确认新登录密码！");
			txt_confirmpsw.requestFocus();			
            return;
	    }
		if (oldpsw.length() < 5){
			Utils.showShortToast(SetupPasswordActivity.this, "旧密码肯定错误！");	
			txt_oldpsw.requestFocus();			
            return;
	    }		
		if (password.length() < 6){
			Utils.showShortToast(SetupPasswordActivity.this, "用户密码必须6位以上！");	
			txt_password.requestFocus();			
            return;
	    }
		if (!password.equals(confirmpsw)){
			Utils.showShortToast(SetupPasswordActivity.this, "两次输入的密码不一致！");	
			txt_confirmpsw.requestFocus();			
            return;
	    }	
        String userInput = txt_checkcode.getText().toString();
        if(!CheckGetUtil.checkNum(userInput, checkNum)){
			Utils.showShortToast(SetupPasswordActivity.this, "验证码不对！");	
			txt_checkcode.requestFocus();			
            return;
	    }
        
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showShortToast(SetupPasswordActivity.this, "你还没有登录服务器！");				
			return;
		}
		getLoadingDialog("正在修改登录密码...  ").show();	
		setInfoClient.SetLoginPassword( oldpsw, password,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {	
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在修改登录密码...").dismiss();		
						String str = "成功修改登录密码！";  
						Utils.showLongToast(SetupPasswordActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在修改登录密码...").dismiss();		
						String str = "修改登录密码失败！";  
						Utils.showLongToast(SetupPasswordActivity.this, str);
					}
				});
			}
		});	         
	}

}
