package admin.func
{
    import admin.conn.*;
    import admin.event.*;
    import admin.model.*;
    import admin.ui.login.*;
    import admin.util.*;
    import fl.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import flash.utils.*;

    public class Login extends Sprite
    {
        private var loginUI:LoginMain = null;
        private var btnToReg:Button = null;
        private var btnToLogin:Button = null;
        private var log:TextField = null;
        private var btnReg:Button = null;
        private var btnLogin:Button = null;
        private var bgImg:Loader = null;
        private var sharedData:SharedObject = null;
        private var timerLogin:int = 0;
        private var timerLog:int = 0;
        private static const SHARED_USER_KEY:String = "sqjl_user";

        public function Login()
        {
            addEventListener(Event.ADDED_TO_STAGE, this.onStageHandler);
            return;
        }// end function

        private function onStageHandler(event:Event) : void
        {
            removeEventListener(Event.ADDED_TO_STAGE, this.onStageHandler);
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this.bgImg = new Loader();
            addChild(this.bgImg);
            this.bgImg.load(new URLRequest("sq14.jpg"));
            if (!this.loginUI)
            {
                this.loginUI = new LoginMain();
                this.loginUI.x = 240;
                this.loginUI.y = 120;
                this.loginUI.mc_Loading.visible = false;
                this.loginUI.mc_Reg.visible = false;
                this.loginUI.mc_Login.visible = true;
                this.loginUI.mc_Login.ip_Password.displayAsPassword = true;
                this.loginUI.mc_Reg.ip_Password.displayAsPassword = true;
                this.loginUI.mc_Reg.ip_Password2.displayAsPassword = true;
                this.btnToReg = this.loginUI.mc_Login.btn_ToReg as Button;
                this.btnToLogin = this.loginUI.mc_Reg.btn_Back as Button;
                this.log = this.loginUI.tf_Log;
                this.log.visible = false;
                this.btnReg = this.loginUI.mc_Reg.btn_Reg as Button;
                this.btnLogin = this.loginUI.mc_Login.btn_Login as Button;
                this.addEvent();
                this.sharedData = SharedObject.getLocal(SHARED_USER_KEY);
                if (!this.sharedData.data.email)
                {
                }
                this.loginUI.mc_Login.ip_Email.text = "";
                if (!this.sharedData.data.password)
                {
                }
                this.loginUI.mc_Login.ip_Password.text = "";
                if (this.loginUI.mc_Login.ip_Password.text)
                {
                }
                if (this.loginUI.mc_Login.ip_Password.text != "")
                {
                    this.loginUI.mc_Login.cb_Password.selected = true;
                }
                else
                {
                    this.loginUI.mc_Login.cb_Password.selected = false;
                }
                addChild(this.loginUI);
            }
            this.timerLogin = setTimeout(this.loginDo, 500);
            return;
        }// end function

        private function loginDo() : void
        {
            if (this.timerLogin > 0)
            {
                clearTimeout(this.timerLogin);
            }
            AdminConn.connect();
            return;
        }// end function

        private function loginDataHandler(event:AdminEvent) : void
        {
            if (UserModel.instance.code == 0)
            {
                this.log.text = UserModel.instance.email + "登录成功，charId:" + UserModel.instance.charId;
                FuncUtil.instance.callUser(UserModel.instance);
            }
            else
            {
                this.log.text = "登录失败";
                switch(UserModel.instance.code)
                {
                    case 10201:
                    {
                        this.log.text = "登录失败，验证不通过";
                        break;
                    }
                    case 10202:
                    {
                        this.log.text = "登录失败，参数不正确。";
                        break;
                    }
                    case 10203:
                    {
                        this.log.text = "登录失败，角色被锁定。";
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                this.log.visible = true;
                UserModel.instance.clear();
                this.timerLog = setTimeout(this.logTimer, 3000);
            }
            return;
        }// end function

        private function regDataHandler(event:AdminEvent) : void
        {
            var _loc_2:Object = null;
            var _loc_3:String = null;
            if (UserModel.instance.code == 0)
            {
                this.log.text = UserModel.instance.email + "注册成功，charId:" + UserModel.instance.charId;
                _loc_2 = new Object();
                _loc_2.cmd = 65538;
                _loc_2.Name = UserModel.instance.email;
                _loc_2.Pwd = UserModel.instance.password;
                AdminConn.instance.send(_loc_2);
            }
            else
            {
                _loc_3 = "注册失败";
                trace("UserModel.instance.code=" + UserModel.instance.code);
                switch(UserModel.instance.code)
                {
                    case 10101:
                    {
                        _loc_3 = "注册失败，参数错误。";
                        break;
                    }
                    case 10102:
                    {
                        _loc_3 = "注册失败，用户名已存在。";
                        break;
                    }
                    case 10103:
                    {
                        _loc_3 = "注册失败，暂时无法注册。";
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                this.log.text = _loc_3;
                this.log.visible = true;
                UserModel.instance.clear();
                this.timerLog = setTimeout(this.logTimer, 3000);
            }
            return;
        }// end function

        private function logTimer() : void
        {
            if (this.timerLog > 0)
            {
                clearTimeout(this.timerLog);
                this.timerLog = 0;
            }
            this.log.visible = false;
            return;
        }// end function

        private function addEvent() : void
        {
            EventProxy.instance.addEventListener(AdminEvent.USER_REG_DATA_EVENT, this.regDataHandler);
            EventProxy.instance.addEventListener(AdminEvent.USER_LOGIN_DATA_EVENT, this.loginDataHandler);
            if (this.btnToReg)
            {
                this.btnToReg.addEventListener(MouseEvent.CLICK, this.btnToRegClick);
            }
            if (this.btnToLogin)
            {
                this.btnToLogin.addEventListener(MouseEvent.CLICK, this.btnToLoginClick);
            }
            if (this.btnReg)
            {
                this.btnReg.addEventListener(MouseEvent.CLICK, this.btnRegClick);
            }
            if (this.btnLogin)
            {
                this.btnLogin.addEventListener(MouseEvent.CLICK, this.btnLoginClick);
            }
            this.loginUI.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyEnterLogin);
            return;
        }// end function

        private function login() : Boolean
        {
            var _loc_1:* = this.loginUI.mc_Login.ip_Email.text;
            var _loc_2:* = this.loginUI.mc_Login.ip_Password.text;
            if (_loc_1 != "")
            {
            }
            if (_loc_2 == "")
            {
                this.log.text = "请正确填写!";
                this.log.visible = true;
                return false;
            }
            UserModel.instance.email = _loc_1;
            UserModel.instance.password = _loc_2;
            var _loc_3:* = new Object();
            _loc_3.cmd = 65538;
            _loc_3.Name = _loc_1;
            _loc_3.Pwd = _loc_2;
            AdminConn.instance.send(_loc_3);
            this.sharedData.data.email = UserModel.instance.email;
            if (this.loginUI.mc_Login.cb_Password.selected)
            {
                this.sharedData.data.password = UserModel.instance.password;
            }
            else
            {
                this.sharedData.data.password = "";
            }
            return true;
        }// end function

        private function reg() : Boolean
        {
            var _loc_1:* = this.loginUI.mc_Reg.ip_Email.text;
            var _loc_2:* = this.loginUI.mc_Reg.ip_Password.text;
            var _loc_3:* = this.loginUI.mc_Reg.ip_Password2.text;
            if (_loc_1 != "")
            {
            }
            if (_loc_2 != "")
            {
            }
            if (_loc_2 != _loc_3)
            {
                this.log.text = "请正确填写!";
                this.log.visible = true;
                return false;
            }
            UserModel.instance.email = _loc_1;
            UserModel.instance.password = _loc_2;
            var _loc_4:* = new Object();
            _loc_4.cmd = 65537;
            _loc_4.Name = _loc_1;
            _loc_4.Pwd = _loc_2;
            AdminConn.instance.send(_loc_4);
            return true;
        }// end function

        private function btnRegClick(event:MouseEvent) : void
        {
            this.reg();
            return;
        }// end function

        private function btnLoginClick(event:MouseEvent) : void
        {
            this.login();
            return;
        }// end function

        private function onKeyEnterLogin(event:KeyboardEvent) : void
        {
            if (!this.loginUI.mc_Login.visible)
            {
                return;
            }
            if (event.keyCode == 13)
            {
                this.login();
            }
            return;
        }// end function

        private function btnToRegClick(event:MouseEvent) : void
        {
            if (!this.loginUI)
            {
                return;
            }
            this.loginUI.mc_Login.visible = false;
            this.loginUI.mc_Reg.visible = true;
            this.log.visible = false;
            return;
        }// end function

        private function btnToLoginClick(event:MouseEvent) : void
        {
            if (!this.loginUI)
            {
                return;
            }
            this.loginUI.mc_Login.visible = true;
            this.loginUI.mc_Reg.visible = false;
            this.log.visible = false;
            return;
        }// end function

    }
}
