let aGameInstance = require('GameInstance');
let aWebView = require("WebView");
let aLib = require("Library");
cc.Class({
    extends: cc.Component,

    properties: {
        GameStates: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        PlayerControllerNode: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        TimeLabelNode: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        StartButtonNode: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        TenSecondChallengeBG: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
        ReadyGo: {
            default: null, 
            type: cc.Node, 
            serializable: true,
        },
    },
    onLoad () {
        this.GameStatesScp = this.GameStates.getComponent('GameStates');
        this.PlayerControllerScp = this.PlayerControllerNode.getComponent('PlayerController');
        this.TimeLabelScp = this.TimeLabelNode.getComponent('CountDown');
        this.ReadyGoScp = this.ReadyGo.getComponent('ReadyGo');

        this.bStart = false;
    },
    start()
    {
        //是否在pC
        if(!aLib.isMobile()) return;

        let self = this;

        aWebView.init(self.node, ()=>{
            aWebView.onBindHitBoxingPost();
            self.node.on('onBoxingPostHit',self.onBoxingPostHit,self);
        });
    },
    onBoxingPostHit(data)
    {
        if(!this.GameStatesScp.bStart)
        {
            this.StarGame();
        }
        else
        {
            this.PlayerControllerScp.Run();
        }
        
    },
    //页面退出回调
    onQuit(data)
    {
        console.log('onQuit=',data);
    },
    //弹出框回调
    onQuitModal(res)
    {
        if (res.data.confirm) {
            console.log("退出");
        }else if(res.data.cancel){
            console.log("取消退出");
        }
    },
    StarGame(target,param)
    {
        this.PlayerControllerScp.Reset(); 
        if(!this.ReadyGo.active)
        {
            this.ReadyGo.active = true;
        }

        this.ReadyGoScp.Play(function(){
            this.GameStatesScp.bStart = true;

            this.TimeLabelScp.CountDown({'second':10,'millisecond':0},function(){
                this.GameStatesScp.bStart = false;
                this.GameStatesScp.PunchTimes = 0;
                this.StartButtonNode.active = true;
                this.TenSecondChallengeBG.active = true;
            }.bind(this));
        }.bind(this));
        this.StartButtonNode.active = false;
        this.TenSecondChallengeBG.active = false;
    }
});
