const BottleType = require('Types').BottleType;
const Roll = require('Roll');

cc.Class({
    extends: cc.Component,

    properties: {
        //{0: 小瓶; 1: 中瓶; 2: 大瓶; 3: 特大瓶}
        bottleType: {
            default: BottleType.MNormal,
            type: BottleType
        },
        // tp1: cc.Node,
        // tp2: cc.Node,
        // tp3: cc.Node,
        bottleFrames: [cc.SpriteFrame],
        shooterFrames: [cc.SpriteFrame],
        item: cc.Node,
        stick: cc.Node,
        subline: cc.Node,
        bingo: cc.Node,
        gold: cc.Node,
        hit: cc.Node,
        bounder: cc.Node,
    },

    init (waveMng) {
        this.waveMng = waveMng;
        this.vSize = cc.director.getVisibleSize();
        
        this.subline = this.subline.getComponent('bItem');
        this.subline.init(this);

        this.gold = this.gold.getComponent('bItem');
        this.gold.init(this);

        this.hit = this.hit.getComponent('bItem');
        this.hit.init(this);

        this.stick = this.stick.getComponent('Stick');
        this.stick.init(this);

        // 设置箭矢皮肤
        let arrIndex = window.Global.arrowSkinIndex || 0
        this.stick.getComponent(cc.Sprite).spriteFrame = this.shooterFrames[arrIndex]

        this.isEnter = false;
        this.hasChange = false;
        //刚体组件
        this.rigidBody = this.getComponent(cc.RigidBody);

        //包围盒上下限范围设定
        let lRange = this.vSize.width > 1550 ? 240 : 150
        this.checkLimit = [
            this.vSize.width / 2 - lRange,
            this.vSize.width / 2 + lRange,
        ]
        
        this.reset();
        this.tMode = 'normal';

        if(waveMng.game.gameMode == 'normal') {     //当前场景为普通模式
            //根据权重生成特殊瓶子
            let roller = new Roll();
            if(this.bottleType == 3) {
                roller.add('subline', 5);       //辅助线瓶 5%
                roller.add('bingo', 5);        //华彩瓶 5%
                roller.add('gold', 20);         //金币瓶 20%
                roller.add('normal', 70);       //普通瓶 70%
            } else {
                roller.add('subline', 5);       //辅助线瓶 5%
                roller.add('bingo', 10);        //华彩瓶 10%
                roller.add('gold', 20);         //金币瓶 20%
                roller.add('normal', 65);       //普通瓶 65%
            }
            this.tMode = roller.roll();

            switch (this.tMode) {
                case 'subline':
                    if(!this.waveMng.checkSpecialMode('subline')) {
                        this.waveMng.recordSpecialMode('subline');
                        this.subline.showSubline();
                        this.changeBottle(0)
                    } else {
                        this.tMode = 'normal';
                    }
                    break;
                case 'bingo':
                    if(!this.waveMng.checkSpecialMode('bingo')) {
                        this.waveMng.recordSpecialMode('bingo');
                        this.bingo.active = true;
                        this.changeBottle(9);
                        this.tMode = 'bingo';
                    } else {
                        this.bingo.active = false;
                        this.changeBottle(0);
                        this.tMode = 'normal';
                    }
                    break;
                case 'gold':
                    this.gold.showGold();
                    this.changeBottle(0)
                    break;
                default:
                    this.changeBottle(0)
                    break;
            }

        } else if(waveMng.game.gameMode == 'bingo') {
            //超大瓶体不出现华彩瓶
            this.tMode = 'bingo';
            this.bingo.active = true;
            this.changeBottle(9)

            if(!this.waveMng.checkSpecialMode('hit') && this.waveMng.game.bingoHit == 0) {
                let ran = cc.random0To1();
                // 8%概率出现必中瓶
                if(ran > 0.08) {
                    this.tMode = 'hit';
                    this.hit.showHit();
                    this.waveMng.recordSpecialMode('hit');
                }
            }
        } else {
            this.tMode = 'normal'
            this.bingo.active = false;
            this.changeBottle(0)
        }
    },
    
    /**
     * 重置瓶子各种模式
     */
    reset () {
        this.finished = false;
        this.stick.item.active = false;
        this.subline.item.active = false;
        this.gold.item.active = false;
        this.hit.item.active = false;
        this.bingo.active = false;
    },

    /**
     * 切换瓶子类型
     * @param {Number} bottleType 瓶子类型{0: 普通瓶; 9: 华彩瓶}
     */
    changeBottle (bottleType) {
        if(bottleType == 0) {
            bottleType = window.Global.bottleSkinIndex || 0
        }
        this.item.getComponent(cc.Sprite).spriteFrame = this.bottleFrames[bottleType]
    },

    /**
     * 切换X轴位移速度
     * @param {Number} speed 瓶子沿X轴位移速度
     */
    // changeSpeed (speed) {
    //     this.MoveX = speed;
    // },

    // onCollisionEnter (other, self) {
    //     this.doEnterCtrl();
    // },

    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact (contact, selfCollider, otherCollider) {
        if(otherCollider.node.group == "shooter") {
            this.rigidBody.enabledContactListener = false;
            this.doEnterCtrl();
        }
    },
    
    doEnterCtrl () {
        console.log(this.tMode);
        if(this.isEnter) {
            return;
        } else {
            this.isEnter = true;
            //播放箭矢击中瓶身动画
            this.stick.item.active = true;
            let clipRan = cc.random0To1() * 3
            clipRan = Math.ceil(clipRan)

            switch (this.bottleType) {
                case 0:
                    //小瓶播放特定的动画
                    this.getComponent(cc.Animation).play('bottle-run-s')
                    break;
                case 3:
                    //特大瓶播放特定的动画
                    this.getComponent(cc.Animation).play('bottle-run' + clipRan)
                    break;
                default:
                    //其他瓶播放随机动画
                    this.getComponent(cc.Animation).play('bottle-run' + clipRan)
                    break;
            }
        
            if(this.bingo.active) {
                if(this.hit.item.active) {
                    //激活必中瓶模式
                    this.hit.getComponent(cc.Animation).play();
                    this.waveMng.game.activeMode('hit');
                    this.waveMng.game.runHitMode(this.bottleType);
                    this.hit.item.active = false;
                } else {
                    // 激活华彩模式
                    this.waveMng.game.activeMode('bingo');
                }

                if(this.gold.item.active) {
                    /** 增加金币 */
                    this.gold.getComponent(cc.Animation).play();
                    this.waveMng.game.activeMode('gold');
                }
            } else if(this.subline.item.active) {
                /** 激活辅助线模式 */
                this.subline.getComponent(cc.Animation).play();
                this.waveMng.game.activeMode('subline');
            } else if(this.gold.item.active) {
                /** 增加金币 */
                this.gold.getComponent(cc.Animation).play();
                this.waveMng.game.activeMode('gold');
            }
        }
    },

    start () {
        // let action = cc.moveTo(3, cc.p(this.vSize.width + this.node.x + 1000, this.node.y))
        // this.node.runAction(action)
    },

    update (dt) {
        // if(this.waveMng.state == 3) {
        //     return;
        // }

        if(this.node.x > this.checkLimit[0] && this.node.x < this.checkLimit[1]) {
            this.rigidBody.enabledContactListener = true;
        } else {
            this.rigidBody.enabledContactListener = false;
        }

        //如果在华彩模式冷却时间下，转换为普通瓶子
        if(this.waveMng.game.inGameUI.bingoFreeze && this.bingo.active) {
            this.bingo.active = false;
            this.hit.item.active = false;
            this.hasChange = true;
            this.changeBottle(0)
            return;
        }
        
        if(this.waveMng.game.gameMode == 'bingo' && !this.bingo.active) {
            if(!this.hasChange) {
                this.bingo.active = true;
                this.subline.item.active = false;
                this.changeBottle(9);
                return;
            }
        }

        if(this.waveMng.game.gameMode == 'hit') {
            let spawn = cc.spawn(cc.moveTo(0.15, cc.p(this.node.x, this.node.y - 250)), cc.fadeOut(0.15));
            this.node.runAction(spawn);
            this.scheduleOnce(() => {
                this.recycle();
            }, 0.15)
        } else if (this.node.x > this.vSize.width + 80) {
            this.node.active = false;
            this.recycle();
        } else {
            this.node.x += dt * this.waveMng.MoveX;
        }
    },

    recycle () {
        if(!this.finished) {
            // console.log('=========== recycle ===========');
            this.finished = true;
            this.waveMng.despawnBottle(this);
        }
    }
});
