module.exports = class AudioQueue {
    constructor(connection) {
        this.connection = connection;
        this.queue = [];
        this.current = undefined;
    }
    playNext() {
        if (this.queue.length == 0) {
            this.current = undefined;
            return;
        }
        this.current = this.connection.play(this.queue[0]);
        this.current.on('speaking', (val) => {if (val == 0) this.handleStop(this.current)});
    }
    handleStop(current) {
        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();
    }
}