const Spawn = require('Spawn');

cc.Class({
    extends: cc.Component,

    properties: {
        tipsFrames: [cc.SpriteFrame],
        bingoFrames: [cc.SpriteFrame],
        hitWrap: cc.Node,
        hitLabel: cc.Label,
        hitTips: cc.Node,
        hitDisX: 800,
        hitDisY: 280,
        hitInterval : 0.15,
        bottleHitMng: cc.Node,     //对象池[Bottle]
        timer: cc.Label
    },

    init (game) {
        this.game = game;
        this.inGameUI = game.inGameUI;
        this.waveMng = game.waveMng;
        this.bottleHitMng = this.bottleHitMng.getComponent('BottleHitMng');
        this.bottleHitMng.init(this);
        this.touchInterval = 0;
        this.status = 0;    //{0: 默认状态; 1: 开启必中瓶状态; 2: 必中瓶模式结束后2秒静止状态}
    },
    
    /**
     * 开始必中瓶模式
     */
    startHit (bottleType) {
        this.hitTips.active = true;
        this.hitLabel.node.active = true;
        this.hitLabel.getComponent(cc.Animation).play();
        this.hitList = [];
        this.frameId = 0;
        this.status = 1;
        this.hitTime = 30;      //必中模式倒计时，持续3秒
        this.hitWrap.on(cc.Node.EventType.TOUCH_START, this.hitEvent, this)

        //设置必中瓶模式瓶身类型
        this.hitSprite = this.bingoFrames[bottleType];

        this.schedule(this.showHitTips, 0.2)

        this.scheduleOnce(() => {
            this.hitLabel.node.active = false;
        }, 1)

        //倒计时执行
        this.timer.node.active = true;
        this.timer.string = (this.hitTime / 10).toFixed(1) + 'S';
        this.timeCtrl = () => {
            if(this.hitTime == 0) {
                this.timer.node.active = false;
                this.endHit();
                this.unschedule(this.timeCtrl)
            } else {
                this.timer.string = (this.hitTime / 10).toFixed(1) + 'S';
                this.hitTime--;
            }
        }
        this.schedule(this.timeCtrl, 0.1)
    },

    showHitTips () {
        this.hitTips.getComponent(cc.Sprite).spriteFrame = this.tipsFrames[this.frameId]
        this.frameId = this.frameId == 0 ? 1 : 0;
    },

    hitEvent () {
        // 限制点击间隔
        if(this.touchInterval <= this.hitInterval) {
            return;
        } else {
            this.touchInterval = 0;
            let sp = new Spawn()
            sp.init({
                bottleType: 0,
                total: 1
            })
            let hb = sp.spawn(this.bottleHitMng)
            //显示对应瓶身尺寸的皮肤
            hb.children[1].getComponent(cc.Sprite).spriteFrame = this.hitSprite;

            if(this.hitList.length == 0) {
                this.hitList.unshift(hb)
                this.hitWrap.addChild(hb);
                hb.setPosition(-this.hitDisX, -this.hitDisY)
                let action = cc.moveTo(0.1, cc.p(0, -this.hitDisY))
                hb.runAction(action)
                this.scheduleOnce(() => {
                    hb.getComponent(cc.Animation).play()
                }, 0.1)
            } else {
                this.hitList.unshift(hb)
                this.hitWrap.addChild(hb);
                hb.setPosition(-this.hitDisX, -this.hitDisY)
                let action1 = cc.moveTo(0.1, cc.p(0, -this.hitDisY))
                hb.runAction(action1)
                this.scheduleOnce(() => {
                    hb.getComponent(cc.Animation).play()
                }, 0.1)
    
                let action2 = cc.moveTo(0.1, cc.p(this.hitDisX, -this.hitDisY))
                this.hitList[this.hitList.length - 1].runAction(action2)
                this.scheduleOnce(() => {
                    let p = this.hitList.pop();
                    p && this.bottleHitMng.returnBottle(0, p);
                }, 0.1)
            }
    
            this.inGameUI.addScore();
        }
    },

    /**
     * 结束必中瓶模式
     */
    endHit () {
        this.status = 2;

        //恢复游戏模式为华彩模式bingo
        this.game.gameMode = 'bingo';
    
        //重新生成新的波段内容
        this.waveMng.repeatWave();
        
        this.scheduleOnce(() => {
            //取消必中瓶模式事件监听
            this.hitWrap.off(cc.Node.EventType.TOUCH_START, this.hitEvent, this)
    
            //清除必中瓶模式的资源
            this.hitList.forEach(n => {
                let action = cc.moveTo(0.1, cc.p(this.hitDisX, -this.hitDisY))
                n.runAction(action)
                this.scheduleOnce(() => {
                    this.bottleHitMng.returnBottle(0, n)
                }, 0.12)
            })
    
            //清除必中瓶UI提示
            this.unschedule(this.showHitTips);
            this.hitTips.active = false;
            
            //必中瓶模式结束后1.5秒内暂停发射箭矢
            this.scheduleOnce(() => {
                this.status = 0;
            }, 1.5)
        }, 0.1)
    },

    update (dt) {
        if(this.status == 1) {
            this.touchInterval += dt;
        }
    },
});
