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

    properties: {
        hitAudio: {
            default: null,
            type: cc.AudioClip,
            serializable: true,
        },
        missAudio: {
            default: null,
            type: cc.AudioClip,
            serializable: true,
        },
        bloodFlash: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    start()
    {
        this._super();
        if(cc.sys.isMobile)
        {
            this.avatar.spriteFrame = webView.avatarSpriteFrame;
            this.avatar.node.width = 80;
            this.avatar.node.height = 80;
            this.playerName.string = webView.userName;
            this.gender.string = webView.gender;

            webView.onBindHitBoxingPost();
            webView.onAddQuitModalListener();//添加退出事件
            this.node.on('onBoxingPostHit',this.onBoxingPostHit,this);
            this.node.on('onQuit',this.onQuit,this);
            this.node.on('onQuitModal',this.onQuitModal,this);
        }

        //注册Touch事件
        this.topHole.on(cc.Node.EventType.TOUCH_START,this.top,this);
        this.leftHole.on(cc.Node.EventType.TOUCH_START,this.left,this);
        this.rightHole.on(cc.Node.EventType.TOUCH_START,this.right,this);

        this.flashScp = this.bloodFlash.getComponent('BloodyFlash');
        this.gStatesScp.playerStatesArr.unshift(this.pStatesScp);
    },
    hit()
    {
        this.pStatesScp.hit++;
        this.score.string = this.pStatesScp.hit.toString();
        cc.audioEngine.play(this.hitAudio, false, 1);
    },
    wrong()
    {
        if(!this.pStatesScp.bPlayDirection) return;
        this._super();
        this.flashScp.flash();
        cc.audioEngine.play(this.missAudio, false, 1);
    },
    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.noDirectionPunch();
        }
    },
    //页面退出回调
    onQuit(data)
    {
        // console.log('onQuit=',data);
    },
    //弹出框回调
    onQuitModal(res)
    {
        if (res.data.confirm) {
            // let kCal = this.gStatesScp.hitCount*webView.caloriUnit+this.gStatesScp.currentTime*300/3600;
            let kCal = this.pStatesScp.hit*webView.kCalUnit;
            // console.log('webView.kCalUnit=',webView.kCalUnit);
            // console.log('this.pStatesScp0.hit=',this.pStatesScp.hit);
            webView.uploadInfo(0,this.gStatesScp.currentTime, kCal);
            webView.closeGame();
        }else if(res.data.cancel){
            console.log("取消");
        }
    }
});
