cc.Class({
    extends: cc.Component,

    properties: {
        waveUI: cc.Node,
        comboDisplay: cc.Node,
        scoreLabel: cc.Label,
        goldLabel: cc.Label,
        ratioLabel: cc.Label,
        subline: cc.Node,
        modeUndead: cc.Node,
        modeSubline: cc.Node,
        modeBingo: cc.Node,
        bgNormal: cc.Node,
    },

    // 初始化组件
    init (game) {
        this.score = 0;
        this.gold = 0;
        this.game = game;
        // this.shooter = game.shooter;
        this.audioMng = game.audioMng;

        this.modeTime = 15;

        this.waveUI = this.waveUI.getComponent('WaveUI');
        this.waveUI.node.active = false;

        this.comboDisplay = this.comboDisplay.getComponent('ComboDisplay');
        this.comboDisplay.init(this);

        this.lastScore = 0;
        this.speedMap = [300, 350, 400, 450, 500];
        this.speedLevel = 0;    //7级, 0 ~ 6
        this.scoreSave = 0;

        this.curRatio = 'normal'
        this.ratioMap = {       //分数倍率，1 ~ 3倍
            normal: 1,
            combo: 2,
            bingo: 3
        }
        this.scoreRatio = this.ratioMap[this.curRatio]
    },

    /**
     * 更新不同的游戏档位
     */
    updateSpeed () {
        let speed = 0;
        let level;
        let smap = this.speedMap;
        // if(this.score <= 10) {
        //     level = 0;
        //     speed = smap[level];
        // } else if(this.score > 10 && this.score <= 20) {
        //     level = 1;            
        //     speed = smap[level];
        // } else if(this.score > 20 && this.score <= 35) {
        //     level = 2;            
        //     speed = smap[level];
        // } else if(this.score > 35 && this.score <= 50) {
        //     level = 3;
        //     speed = smap[level];
        // } else if(this.score > 50 && this.score <= 70) {
        //     level = 4;
        //     speed = smap[level];
        // } else if(this.score > 70 && this.score <= 150) {
        //     level = 5;
        //     // 每插中5个，在3-5档中随机
        //     if(this.scoreSave >= 5) {
        //         let randomLevel = Math.floor(cc.random0To1() * 3) + 2;
        //         speed = smap[randomLevel]
        //         this.scoreSave = 0;
        //     } else {
        //         this.scoreSave++;
        //     }
        // } else {
        //     level = 6;
        //     // 每插中3个，在1-5档中随机
        //     if(this.scoreSave >= 3) {
        //         let randomLevel = Math.floor(cc.random0To1() * 5);
        //         speed = smap[randomLevel]
        //         this.scoreSave = 0;
        //     } else {
        //         this.scoreSave++;
        //     }
        // }

        if(this.score <= 50) {
            level = 0;
            speed = smap[level];
        } else if(this.score > 50 && this.score <= 100) {
            level = 1;            
            speed = smap[level];
        } else if(this.score > 100 && this.score <= 150) {
            level = 2;            
            speed = smap[level];
        } else if(this.score > 150 && this.score <= 200) {
            level = 3;
            // 每插中5个，在3-5档中随机
            if(this.scoreSave >= 5) {
                let randomLevel = Math.floor(cc.random0To1() * 3) + 1;
                speed = smap[randomLevel]
                this.scoreSave = 0;
            } else {
                this.scoreSave++;
            }
        } else {
            level = 4;
            // 每插中3个，在1-5档中随机
            if(this.scoreSave >= 3) {
                let randomLevel = Math.floor(cc.random0To1() * 3) + 2;
                speed = smap[randomLevel]
                this.scoreSave = 0;
            } else {
                this.scoreSave++;
            }
        }

        if(this.speedLevel == level) {
            return;
        } else {
            this.speedLevel = level;
            if(speed) {
                this.game.waveMng.changeSpeed(speed);
            }
        }
    },
    
    /**
     * 添加分数
     * @param {Number} score 分数
     */
    addScore (score = 1) {
        //播放击中音频
        this.scheduleOnce(() => {
            this.audioMng.playEnter()
        }, 0.1)

        
        //操作分数
        this.score += score * this.scoreRatio;
        this.scoreLabel.string = this.score;
        this.scoreLabel.getComponent(cc.Animation).play();

        //控制分数倍率显示
        if(this.scoreRatio > 1) {
            this.ratioLabel.string = 'X' + this.scoreRatio;
            this.ratioLabel.node.active = true;
            this.unschedule(this.hideScoreRatio);
            this.scheduleOnce(this.hideScoreRatio, 1)
        }

        //控制连击
        this.comboDisplay.playCombo();

        //更新游戏速度
        this.updateSpeed()

        if(this.score - this.lastScore >= 5) {
            this.game.waveMng.changeWave();
            this.lastScore = this.score;
        }
    },

    hideScoreRatio () {
        this.ratioLabel.node.active = false;
    },

    /**
     * 添加金币
     * @param {Number} gold 金币数
     */
    addGold (gold = 1) {        
        //播放击中金币音频
        this.audioMng.playGetGold();

        //操作金币数
        this.gold += gold;
        this.goldLabel.string = this.gold;
        this.goldLabel.getComponent(cc.Animation).play();
        
    },

    /**
     * 激活辅助线模式
     */
    showSubline () {
        if(!this.subline.active) {
            // this.modeSubline.active = true;
            this.subline.active = true;
            this.scheduleOnce(() => {
                this.hideSubline()
            }, 15)
        }
    },

    /**
     * 结束辅助线模式
     */
    hideSubline () {
        // this.modeSubline.active = false;
        this.subline.active = false;        
        this.game.resetMode();
    },

    /**
     * 激活华彩模式
     */
    showBingo () {
        if(!this.modeBingo.active) {
            /** 播放华彩模式背景音乐 */
            this.audioMng.playBgmBingo();

            /** 调整华彩模式的分数倍率 */
            this.changeScoreRatio('bingo');

            this.modeBingo.active = true;
            this.modeBingo.x = -836;
            this.modeBingo.getComponent(cc.Animation).play('bgbingo-show');
            this.scheduleOnce(() => {
                this.modeBingo.getComponent(cc.Animation).play('bgbingo-run');
            }, 0.5)

            this.scheduleOnce(() => {
                this.endBingo()
            }, 15)
        }
    },

    /**
     * 结束华彩模式
     */
    endBingo () {
        this.modeBingo.getComponent(cc.Animation).stop('bgbingo-run');
        this.modeBingo.getComponent(cc.Animation).play('bgbingo-hide');

        /** 结束华彩模式的分数倍率 */        
        this.endScoreRatio('bingo');

        /** 等待华彩背景渐隐动画结束（0.5秒） */
        this.scheduleOnce(() => {
            this.modeBingo.active = false;
            this.audioMng.stopBgmBingo();
            this.game.resetMode();
            this.audioMng.playBgm();
            
            /** 华彩模式结束后有5秒冷却时间，不在生成华彩瓶子 */
            this.bingoFreeze = true;
            this.scheduleOnce(() => {
                this.bingoFreeze = false;
            }, 5)
        }, 0.5)
    },
    
    showWave () {
        this.waveUI.node.active = true;
    },
    
    /**
     * 控制分数倍率
     */
    changeScoreRatio (ratio) {
        switch (ratio) {
            case 'combo':
                if(this.curRatio == 'normal') {
                    this.curRatio = ratio;
                    this.scoreRatio = this.ratioMap[ratio];
                }
                break;
            case 'bingo':
                if(this.curRatio != 'bingo') {
                    this.curRatio = ratio
                    this.scoreRatio = this.ratioMap[ratio];
                }
                break;
            default:
                this.curRatio = ratio
                this.scoreRatio = this.ratioMap[ratio];
                break;
        }
    },

    /**
     * 结束分数倍率
     */
    endScoreRatio (ratio) {
        switch (ratio) {
            case 'combo':
                if(this.curRatio == 'combo') {
                    this.curRatio = 'normal'
                    this.scoreRatio = this.ratioMap['normal'];
                }
                break;
            case 'bingo':
                if(this.comboDisplay.comboActive) {
                    this.curRatio = 'combo'
                    this.scoreRatio = this.ratioMap['combo'];
                }
                break;
            default:
                this.curRatio = 'normal';
                this.scoreRatio = this.ratioMap['normal'];
                break;
        }
        if(this.scoreRatio == 1) {
            this.ratioLabel.node.active = false;
        }
    }
});
