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

const sizeMap = ['S', 'S', 'M', 'M', 'L', 'L'];

const Spawn = cc.Class({
    name: 'Spawn',
    properties: {
        bottleType: {
            default: BottleType.MNormal,
            type: BottleType
        },
        total: 0,
    },

    ctor () {
        this.spawned = 0;
        this.finished = false;
    },

    init (obj) {
        if(obj) {
            this.bottleType = obj.bottleType;
            this.total = obj.total || this.total;
        }
    },
    
    spawn (bottleMng) {
        if (this.spawned >= this.total) {
            return;
        }
        this.size = SizeType[sizeMap[this.bottleType]];
        let newBottle = bottleMng.requestBottle(this.bottleType);
        if (newBottle) {
            this.spawned++;
            if (this.spawned === this.total) {
                this.finished = true;
            }
            return newBottle;
        } else {
            console.log('max bottle count reached, will delay spawn');
            return null;
        }
    }
});

module.exports = Spawn;