const Voice = require("@discordjs/voice");

module.exports = class AudioQueue {
    constructor(connection, api) {
        this.connection = connection;
        this.api = api;
        this.queue = [];
        this.current = undefined;
    }
    playNext() {
        if (this.queue.length == 0) {
            this.current = undefined;
            return;
        }
        this.current = this.api.play(this.queue[0]);
        this.api.player.on(Voice.AudioPlayerStatus.Idle, this.handleStop.bind(this));
    }
    handleStop(current) {
        console.log(`Handling stop`);
        this.queue.shift();
        this.playNext();
    }
    add(element) {
        this.queue.push(element);
        if (this.queue.length == 1) this.playNext();
    }
    flush() {
        this.current.setVolume(0);
        this.queue=[];
        this.playNext();
    }
}