package com.lianxin.pubqq.main;

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.panqq.common.BaseActivity;
//import com.lianxin.panqq.link.DefineLink;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.NetUtil;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.chat.utils.ExPowerUtils;
import com.lianxin.panqq.client.callback.netCallBack;
import com.lianxin.panqq.client.logClient;
//import com.lianxin.panqq.client.entity.LoginFor;
import com.lianxin.panqq.utils.CheckGetUtil;
import com.lianxin.panqq.widget.CheckView;

//登陆模块
public class AdminLogActivity extends BaseActivity implements OnClickListener {

	private String userId;
	private String adminname = "";	
	private String password;	
	private Button btn_login,btn_cancel;
	private TextView txt_userid;	
	private EditText txt_adminname, txt_password;	
	private EditText txt_checkcode;	
    private CheckView mMyView = null;  
    private String[] checkNum = new String[4];  //获取每次更新的验证码，可用于判断用户输入是否正确 
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_logadmin);
		super.onCreate(savedInstanceState);
		
		initControl();
		initView();
		initData();
		setListener();	
	}

	protected void initControl() {
		btn_login = (Button) findViewById(R.id.login_btn_login);
		btn_cancel = (Button) findViewById(R.id.login_btn_cancel);		
		txt_userid = (TextView) findViewById(R.id.login_edit_userid);
		txt_adminname = (EditText) findViewById(R.id.login_edit_admin);
		
		txt_password = (EditText) findViewById(R.id.login_edit_pwd);
		txt_checkcode = (EditText) findViewById(R.id.tv_user_checkcode);		
		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() {
		//ExPowerUtils.getAdminLevel();
	}

	protected void initData() {
	    if(GloableParams.m_szUserId>0)
	    	txt_userid.setText(""+GloableParams.m_szUserId);
	    if(ExPowerUtils.getAdminLevel()>0){
	    	txt_adminname.setText(""+GloableParams.m_AdminName);
	    	btn_login.setText("退出管理");
	    	btn_login.requestFocus();	    	
	    }
	}

	protected void setListener() {
		btn_login.setOnClickListener(this);		
		btn_cancel.setOnClickListener(this);			
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(AdminLogActivity.this);
		} else if (id == R.id.login_btn_cancel) {
			Utils.finish(AdminLogActivity.this);
		} else if (id == R.id.login_btn_login) {
			if (ExPowerUtils.getAdminLevel() > 0) {
				ExPowerUtils.setAdminExit();
				Utils.finish(AdminLogActivity.this);
			} else
				getLogin();
		}
	}

	private void getLogin() {		
		adminname = txt_adminname.getText().toString().trim();
		userId = txt_userid.getText().toString().trim();		
		password = txt_password.getText().toString().trim();
		if (TextUtils.isEmpty(adminname)){
			Utils.showShortToast(AdminLogActivity.this, "请填写你的专属管理名！");	
			txt_adminname.requestFocus();			
            return;
	    }
		if (TextUtils.isEmpty(userId)){
			Utils.showShortToast(AdminLogActivity.this, "请填写用户账号！");
			txt_userid.requestFocus();			
            return;
	    }
		if (TextUtils.isEmpty(password)){
			Utils.showShortToast(AdminLogActivity.this, "请填写管理口令！");
			txt_password.requestFocus();			
            return;
	    }
		if (adminname.length() < 4){
			Utils.showShortToast(AdminLogActivity.this, "错误的管理名！");	
			txt_adminname.requestFocus();			
            return;
	    }

		if (password.length() < 5){
			Utils.showShortToast(AdminLogActivity.this, "错误的管理口令！");
			txt_password.requestFocus();			
            return;
	    }
        String userInput = txt_checkcode.getText().toString();
        if(!CheckGetUtil.checkNum(userInput, checkNum)){
			Utils.showShortToast(AdminLogActivity.this, "验证码不对！");	
			txt_checkcode.requestFocus();			
            return;
	    }
		// 重新初始化验证码
		checkNum = mMyView.getValidataAndSetImage();		
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showShortToast(AdminLogActivity.this, "你还没有登录服务器!\n本地手机没有什么好管理的");
			txt_adminname.requestFocus();			
            return;
	    }
		if (!NetUtil.checkNetWork(context)) {
			Utils.showShortToast(AdminLogActivity.this, "网络不通！");			
			return;
		}		
		getLoadingDialog("正在登录...").show();
		
		getLogin(GloableParams.m_szUserId, adminname,password);
		//getLoadingDialog("正在登录...").setText("正在更新...");			
	}


	
	private void getLogin( int userId, String name, String password) {

		logClient.admintoServer(userId, name, password, new netCallBack() {// 回调	

					@Override
					public void onError(int code, final String message) { 
						runOnUiThread(new Runnable() {
							public void run() {
								getLoadingDialog("正在登录...").dismiss();
								Utils.showLongToast(AdminLogActivity.this, message);
							}
						});	
						finish();						
					}

					@Override				
					public void onSuccess(int code, String retMessage) {
	  						
						runOnUiThread(new Runnable() {
							public void run() {
								getLoadingDialog("正在登录...").dismiss();	
								Utils.showLongToast(AdminLogActivity.this, "管理登陆成功!");
								
							}
						});	
						finish();						
					}
					/*@Override
					public void onSuccess(int code, Object contex) {					

						finish();						
					}*/
					@Override
					public void onFailure(final int errorCode, final String message) {
		
						runOnUiThread(new Runnable() {
							public void run() {
								getLoadingDialog("正在登录...").dismiss();
								Utils.showLongToast(AdminLogActivity.this, message);								
							}
						});	
						finish();						
					}
				});
	}
	

}
