let gameConfig = require("GameConfig");
let library = require("../Library");
let webView = require("../WebView");
cc.Class({
    extends: cc.Component,

    properties: {
        gameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        gameMode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        rest: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        demostration: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        main: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    onLoad()
    {
        let self = this;
        this.gStatesScp = this.gameStates.getComponent('GameStates');
        this.gameModeScp = this.gameMode.getComponent('GameMode');
        this.restScp = this.rest.getComponent('Rest');
        // this.demostrationScp = this.demostration.getComponent('Demostration');
        this.mainScp = this.main.getComponent('Main');

        this.bCd = true;

        if(cc.sys.isMobile)
        {
            webView.init(this.node,function () {
                let aMode = self.matchIndex();
                if(aMode == -1)
                {
                    self.gStatesScp.bSinglePlayMode = false;
                }
                else
                {
                    self.gStatesScp.currentRound = aMode;
                }
                webView.onBindHitBoxingPost();
                webView.onAddQuitModalListener();//添加退出事件
                self.node.on('onBoxingPostHit',self.onBoxingPostHit,self);
                self.node.on('onQuit',self.onQuit,self);
                self.node.on('onQuitModal',self.onQuitModal,self);

                self.playRest();
            });
        }
    },
    start()
    {
        if(!cc.sys.isMobile) this.playRest();
    },
    matchIndex()
    {
        let arr = gameConfig.videoNameArr;
        for(let i=0;i<arr.length;i++)
        {
            if(arr[i]==webView.videoName)
            {
                return i;
            }
        }
        return -1;
    },
    playRest()
    {
        let self = this;
        // this.playRound();
        this.restScp.fire(function () {
            self.playRound();
            cc.tween(self.rest)
                .to(0.5, { opacity: 0})
                .start();
        });
    },
    // playDemo()
    // {
    //     let self = this;
    //     // play demo
    //     this.rest.active = false;
    //     this.demostration.active = true;
    //     this.demostration.opacity = 255;
    //
    //     this.demostrationScp.play(function () {
    //         self.playRound();
    //     });
    // },
    playRound()
    {
        this.mainScp.resetRound();
        this.mainScp.startPlay();
    },
    triggleCombo(self)
    {
        self.mainScp.combScp.triggleCombo();
    },
    unTriggleCombo()
    {
        self.mainScp.combScp.unTriggleCombo();
    },
    jar()
    {
        // console.log("jar");
        for(let i =0;i<this.gStatesScp.hightLightActionArr.length;i++)
        {
            if(this.gStatesScp.hightLightActionArr[i].nameIndex == 0)
            {
                this.gStatesScp.hightLightActionArr[i].hit();
                this.gStatesScp.hightLightActionArr.splice(i,1);
                break;
            }
        }
    },
    leftHook()
    {
        // console.log("leftHook");
        for(let i =0;i<this.gStatesScp.hightLightActionArr.length;i++)
        {
            if(this.gStatesScp.hightLightActionArr[i].nameIndex == 1)
            {
                this.gStatesScp.hightLightActionArr[i].hit();
                this.gStatesScp.hightLightActionArr.splice(i,1);
                break;
            }
        }
    },
    rightHook()
    {
        // console.log("rightHook");
        for(let i =0;i<this.gStatesScp.hightLightActionArr.length;i++)
        {
            if(this.gStatesScp.hightLightActionArr[i].nameIndex == 1)
            {
                this.gStatesScp.hightLightActionArr[i].hit();
                this.gStatesScp.hightLightActionArr.splice(i,1);
                break;
            }
        }
    },
    hit()
    {
        for(let i =0;i<this.gStatesScp.hightLightActionArr.length;i++)
        {
            this.gStatesScp.hightLightActionArr[i].hit();
            this.gStatesScp.hightLightActionArr.splice(i,1);
            break;
        }
    },
    onBoxingPostHit(data)
    {
        if(this.bCd) return;
        this.bCd = true;
        let self = this;
        this.scheduleOnce(function () {
            self.bCd = false;
        },0.1);

        if(this.gStatesScp.curretState == this.gStatesScp.finished) return;
        // console.log('data.ename='+data.ename);
        if(data.ename == 'hit')
        {
            this.hit();
        }
    },
    //页面退出回调
    onQuit(data)
    {
        // console.log('onQuit=',data);
        let kCal = this.gStatesScp.hitCount*webView.kCalUnit;
        console.log('score=',this.gStatesScp.hitCount);
        console.log('currentTime=',this.gStatesScp.currentTime);
        console.log('kCal=',kCal.toString());
    },
    //弹出框回调
    onQuitModal(res)
    {
        if (res.data.confirm) {
            let kCal = this.gStatesScp.hitCount*webView.kCalUnit;
            webView.uploadInfo(this.gStatesScp.hitCount, this.gStatesScp.currentTime, kCal);
            webView.closeGame();
        }else if(res.data.cancel){
            // console.log("取消");
        }
    },
});
