import FireEventObserver from '../framework/FireEventObserver';
import PreloadManager from '../common/PreloadManager';
import AudioControlManager from '../common/AudioControlManager';
import Event from '../net/Event';
import LogicUnitManager from '../framework/LogicUnitManager'
import Common from '../common/Common';


cc.Class({
    extends: FireEventObserver,

    properties: {
        progressBar: cc.ProgressBar,

        wxLogin:cc.Button,
        ykLogin:cc.Button,
        zhLogin:cc.Button,
        ljLogin:cc.Button,
        yk2Login:cc.Button,

        loginBtnNode:cc.Node,
        loadproNode:cc.Node,
        loginBannerNode:cc.Node,

        regAndreadNode:cc.Node,
        regNode:cc.Node,
        readNode:cc.Node,
        gotoRegBtn:cc.Node,

        inputName:cc.EditBox,
        inputPass:cc.EditBox,
        inputCmpass:cc.EditBox,
        inputphone:cc.EditBox,

        loginName:cc.EditBox,
        loginPass:cc.EditBox,
    },

    onLoad()
    {
        window['onGameBoot']();
        cc.macro.ENABLE_CULLING = false;
        this.loginBtnNode.active = false
        this.progressBarTarget = 0;
        this.scale = 1;
        this.initListener();
        this.preloadCount = 0;
        this.preloadTotal = 5;
        this.lum = LogicUnitManager;
        this.loginLogic = this.lum.getBehavior('LoginBehavior');
        // this.loginLogic.CreateLoginStart();
        this.bloaded = true
        this.initMgr();
    },

    initMgr() {
        cc.vv = {};
        cc.vv.http = require("../net/HTTP");
        var UserMgr = require("../controller/UserMgr");
        cc.vv.userMgr = new UserMgr();
    },

    onBtnConfirmClicked:function(){
        var user = this.inputName.string;
        var pass = this.inputPass.string;
        var cmpass = this.inputCmpass.string;
        var phone = this.inputphone.string;
        if(user == ""){
            console.log("invalid name.");
            return;
        }
        var data = {
            user:user,
            pass:pass, 
            confirm_password:cmpass,
            phone:phone
        };
        console.log(user);
        cc.vv.userMgr.create(data);
    },

    onBtnLoginClicked:function(){
        var user = this.loginName.string;
        var pass = this.loginPass.string;
        if(user == ""){
            console.log("invalid name.");
            return;
        }
        var data = {
            user:user,
            password:pass,
        };
        console.log(user);
        cc.vv.userMgr.login(data);
    },

    onBtnGuestClicked:function(){
        cc.vv.userMgr.guestAuth()
    },

    initListener()
    {
        this.onBehaviorEvent('LoginBehavior',Event.COM_MSG.PRELOAD_DONE,this.onLoaded.bind(this));
        this.onBehaviorEvent('LoginBehavior',Event.SOCKET_MSG.OPEN, this.onSocketOpen.bind(this));
    },


    onSocketOpen(sender,result)
    {
        this.bloaded = true
    },

    onLoaded(sender, result)
    {
        this.preloadCount++;
        this.progressBarTarget = this.preloadCount / this.preloadTotal;
        console.log("this.preloadCount:"+this.preloadCount+"this.progressBarTarget:"+this.progressBarTarget+"")
        if (this.progressBarTarget >= 1 && this.bloaded)
        {
            this.onResourcesLoaded();
        }
    },

    start()
    {
        this.preloadManager = new PreloadManager();
        this.audioControlManager = new AudioControlManager();
        //加载动画和预制体
        this.preloadManager.run();
        //加载音频
        this.audioControlManager.run();

        var self = this;
        cc.director.preloadScene("hall", function(){
            console.log("preload scene battle");
            self.onLoaded();
        })
    },

    onResourcesLoaded()
    {
        this.loginBtnNode.active = true
        this.loadproNode.active = false
    },

    update(dt)
    {
        if (this.progressBar.progress < this.progressBarTarget)
        {
            this.progressBar.progress += dt*this.scale
        }
        if (this.progressBarTarget >= 1 && this.bloaded && this.loginLogic.needShowAuth == false) {
            this.onResourcesLoaded();
        } else if (this.progressBarTarget >= 5 / 6 && this.bloaded && this.loginLogic.needShowAuth == false) {
            this.onResourcesLoaded();
        }
    },

    clickWXLogin(sender)
    {
        //weixin login
        // this.loginLogic.login()
    },
    clickYKLogin(sender){
        //youke login
        cc.vv.userMgr.guestAuth()
        // cc.director.loadScene('hall');
    },
    clickZHLogin(sender){
        //zhang hao login
        sender.target.active = false
        this.ykLogin.node.active = false

        this.loginBannerNode.active = true
        this.ljLogin.node.active = true
        this.yk2Login.node.active = true
    },

    clickRegBtn(sender){
        this.regAndreadNode.active = true
        this.regNode.active = true
    },

    clickReadYsBtn(sender){
        this.regNode.active = false
        this.readNode.active = true
        
    },

    clickCloseReg(sender){
        if (this.regNode.active) {
            this.regAndreadNode.active = false
        }else{
            this.regNode.active = true
            this.readNode.active = false
        }
    },
});


