const dirname = '../../';
const path = require('path');
const util = require('util');
const EventEmitter = require('events').EventEmitter;
const chalk = require('chalk');
const moment = require('moment');
const clsPlayer = require('./clsPlayer').clsPlayer;
const que = require('queue-fifo');

class PlaylistPlayer {
    constructor() {
        this.playerEvents = {
            stateChanged: 'stateChanged',
            positionChange: 'positionChange',
            fileChanged: 'fileChanged',
            playlistFinished: 'playlistFinished',
            fadeInStarted: 'fadeInStarted',
            fadeOutStarted: 'fadeOutStarted',
            vumeter: 'vumeter',
            Error: 'error'
        };

        this.volume = 100;
        this.channel = 0;
        this.channelCount = 0;
        this.soundCard = -1;
        this.que = new que();
        this.player = new clsPlayer();
        // this.basswrapper = require(path.join(dirname, 'basswrapper.node'));
        // this.basswrapper.InitLibrary(dirname + path.sep);
        this.bass = require('./basswrapper').basswrapper;
        this.basslib = new bass();
        this.songs = [];
        this.flags = 0;
        this.splitChan = 0;
        this.splitSource = 0;
        this.mixer = 0;
        this.freq = 44100;
        this.player.on(this.player.playerEvents.positionChange, (position, duration, channel) => {
            let pos = moment({hour: 0, minute: 0, second: 0}).second(position).format("mm:ss");
            let dur = moment({hour: 0, minute: 0, second: 0}).second(duration).format("mm:ss");
            console.log('position: ', pos, '-', dur, '-', duration);
            pos = null;
            dur = null;
        });
        this.player.on(this.playerEvents.playlistFinished, (channel) => {

            });
    }

    initSoundCard(soundCard, flags) {
        this.flags = flags;
        try {
            if (this.soundCard != soundCard) {
                this.soundCard = soundCard;
            }
            this.player.initSoundCard(this.soundCard, this.flags);
        } catch (e) {
            console.log('error initializing sound card: ', e.message);
        }
    }

    createMixer(chanCount, mixer, freq) {
        this.mixer = mixer;
        this.freq = freq;
        this.channelCount = chanCount;
        this.basslib.BASS_Mixer_StreamCreate(this.freq, this.channelCount, this.basslib.BASS_STREAM_DECODE);
    }

    addSongs(newSong) {
        this.que.enqueue(newSong);
        this.songs.push(newSong);
    }

    clearSongs() {
        this.que.clear();
        this.songs = [];
    }

    clearQue() {
        this.que.clear();
    }

    fillQue() {
        if (this.que.size() == 0) {
            if (this.songs.length > 0) {
                for (let i = 0; i < this.songs.length; i++) {
                    this.que.enqueue(this.files[i]);
                }
            }
        }
    }

    nextSong() {
        if (this.que.size() > 0) {
            let newSong = this.que.dequeue();
            if (this.mixer != 0) {
                this.createMixer(2);
                this.channel = this.player.openFile(newSong, this.basslib.BASS_STREAM_DECODE | this.basslib.BASS_SAMPLE_FLOAT);
                let addChannelToMixer = this.basslib.BASS_Mixer_StreamAddChannel(this.mixer, this.channel, this.basslib.BASS_STREAM_DECODE | this.basslib.BASS_SAMPLE_FLOAT);
                this.basslib.BASS_ChannelPlay(this.mixer, 0);
            }
        } else {
            this.fillQue();
            setTimeout(() => {
                this.nextSong();
                console.log('looking for next song to play');
            }, 250);
        }
    }
}

util.inherits(PlaylistPlayer, EventEmitter);
module.exports = {
    PlaylistPlayer: PlaylistPlayer
};