const util = require('util');
const EventEmitter = require('events').EventEmitter;
require('linqjs');
let Player = require('./clsPlayer').clsPlayer;
let _ = require('lodash');
let rootPath = process.cwd();
let path = require('path');
let moment = require('moment');
//let uid = require('gen-uid');
let chalk = require('chalk');
const streamPlayer=class streamPlayer{
    constructor({
                    moment, utils, config,
                    isLooping = true,
                    shuffle = true,
                    autoStart = true,
                    minFileCountToPlay = 3,
                    fadeInEnabled = true,
                    musicVolume = 100, decodeOnly = true
                } = {}) {
        this.moment = moment;
        this.utils = utils;
        this.config = config;
        this.shuffle = shuffle;
        this.isLooping = isLooping;
        this.autoStart = autoStart;
        this.minFileCountToPlay = minFileCountToPlay;
        this.fadeInEnabled = fadeInEnabled;
        this.musicVolume = musicVolume;
        this.mixerVolume = musicVolume;
        this.decodeOnly = decodeOnly;
        this.index = -1;
        this.totalFiles = 0;


        this.playerEvents = {
            stateChanged: 'stateChanged',
            positionChange: 'positionChange',
            fileChanged: 'fileChanged',
            playlistFinished: 'playlistFinished',
            fadeInStarted: 'fadeInStarted',
            fadeOutStarted: 'fadeOutStarted',
            vumeter: 'vumeter',
            Error: 'error',
            announcesFinished: 'announcesFinished'
        };

        this.lastPlayedSongOnDB = '';

        let mixerPlayer = new Player();
        this._mixerPlayer = mixerPlayer;
        this.announcesArray = [];

        mixerPlayer.initSoundCard(this.config.Settings.soundCard, mixerPlayer.basslib.BASS_DEVICE_SPEAKERS);

        //get multi output settings from config

        //default settings
        let mixerFlags = mixerPlayer.bassAttributes.BASS_SAMPLE_FLOAT;
        let speaker = mixerPlayer.bassAttributes.BASS_SAMPLE_DEFAULT;
        let channelCount = 2;
        this.addChannelFlags = mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
        //check config  mono out and which channel
        if (this.config.MultiOutput.isEnabled) {
            switch(parseInt(this.config.MultiOutput.Channel)) {
                case 1:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_FRONT;
                    break;
                case 2:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_REAR;
                    break;
                case 3:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_CENLFE;
                    channelCount = 1;
                    break;
                case 4:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_REAR2;
                    break;
                case 5:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_LEFT;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                case 6:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_RIGHT;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                case 7:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_FRONTLEFT;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                case 8:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_FRONTRIGHT;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                case 9:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_REARLEFT;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                case 10:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_REARRIGHT;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                case 11:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_CENTER;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                case 12:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_LFE;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                case 13:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_REAR2LEFT;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                case 14:
                    speaker = mixerPlayer.speakerAttributes.BASS_SPEAKER_REAR2RIGHT;
                    channelCount = 1;
                    this.addChannelFlags = mixerPlayer.mixerAttributes.BASS_MIXER_DOWNMIX | mixerPlayer.bassAttributes.BASS_STREAM_DECODE;
                    break;
                default:
                    channelCount = 2;
                    break;
            }
        }

        if (this.decodeOnly) {
            speaker = mixerPlayer.bassAttributes.BASS_STREAM_DECODE;

        }
        //create the mixer
        let mixer = mixerPlayer.createMixer(channelCount, speaker);
        this.mixer = mixer;
        mixerPlayer.setVolume(mixer, this.mixerVolume);
        //mixerPlayer.setVolume(mixer,0)
        //var res=mixerPlayer.basslib.BASS_ChannelSlideAttribute(mixer, mixerPlayer.basslib.BASS_ATTRIB_VOL , 0,5000);
        let err = mixerPlayer.getError();
        let res1=mixerPlayer.PlayMixer();
        let err1 = mixerPlayer.getError();



    }
    p(){
        let p = new Player();
        let chan = p.openUrl('http://carrefour.smg.com.tr:8000/hiper', this._mixerPlayer.bassAttributes.BASS_STREAM_DECODE);
        if (chan == 0 || parseInt(p.getError()) != 0) {
            this.logger.StreamUrl(`Error opening stream url, err: ${this.player.getError()}`);

        }
        let result = this._mixerPlayer.addChannelToMixer(chan, this.addChannelFlags);
        //p.Play();
        console.log('burda dur')
    }
}


util.inherits(streamPlayer, EventEmitter);
module.exports = streamPlayer;