const Firework = {
    set: 0,
    intervalIds: [],
    stopTimeoutId: 0,
    fireworkClass:"firework",
    fireworkTime: 750,
    autoRemoveFfireworkTime: 9000,
    random: function (min, max) {
        return min + Math.random() * (max + 1 - min);
    },
    create: function () {
        const xPos = Firework.random(40, 60),
                yPos = Firework.random(40, 60),
                colour = '#' + Math.random().toString(16).substr(2, 6);
        for (let i = 1; i <= 30; i++) {
            const firework = document.createElement('div');
            firework.className = Firework.fireworkClass;
            firework.classList.add(Firework.fireworkClass + i);
            firework.classList.add("set" + this.set);
            firework.style.backgroundColor = colour;
            firework.style.left = xPos + '%';
            firework.style.top = yPos + '%';
            document.body.appendChild(firework);
        }
        this.set++;
    },
    delete: function () {
        const setToDelete = this.set - 1;
        if (this.set >= 0) {
            const oldFireworks = document.getElemntsByClassName("set" + setToDelete);
            oldFireworks.forEach(firework => {
                firework.remove();
            });
        }
    },
    start: function () {
        _this = Firework;
        _this.create();
        _this.intervalIds.push(setInterval(Firework.create, _this.fireworkTime));
        _this.intervalIds.push(setInterval(Firework.delete, _this.fireworkTime * 3));
        setTimeout(_this.stop, _this.autoRemoveFfireworkTime);
    },
    stop: function () {
        _this = Firework;
        for (let i = 0; i < _this.intervalIds.length; i++) {
            clearInterval(_this.intervalIds[i]);
        }
        let leftOvers = document.getElementsByClassName(_this.fireworkClass);
        while(leftOvers.length>0){
            leftOvers[leftOvers.length-1].remove();
        }
    }
};