// const bass = require('./smg/player/basswrapper').basswrapper;
// const basslib = new bass();
require('linqjs')
const moment = require('moment');
const PlayerDB = require('../db/PlayerDB');
let db = new PlayerDB();

const Schedulesdb = require('../db/schedulesDB');
const Announcesdb = require('../db/announcesDB');
const SMGPush = require('../push/SMGPush');
const Utils = require('../utils/smgutils');
var schedulePlayer = require('../player/schedulePlayer')
let announcePlayer =require('../player/announcePlayer');
let masterMixer=require('../player/masterMixer')
let utils = new Utils(),config,  connectionUrl  ,
    retryConnect = true, announcesdb, schedulesdb,push,
    MasterMixer,schPlayer,announcesDownloader,schedulesDownloader;



const SchedulesDownloader = require('../downloader/schedulesDownloader');
const AnnouncesDownloader = require('../downloader/announcesDownloader');

async function Main() {
    //await config
    config = utils.readConfig();

    global.deviceID = config.Settings.deviceID;
    global.url = config.Settings.downloadURL;

    connectionUrl = config.Settings.serviceUrl
    retryConnect = true;

    //await db init
    await db.initsync();
    global.db = db;
    schedulesdb = new Schedulesdb({ db, moment });
    announcesdb = new Announcesdb({ db, moment });


    push = new SMGPush({ connectionUrl, retryConnect, db, schedulesdb, announcesdb,utils,config });


    push.connect();




    MasterMixer=new masterMixer({db,moment,utils,config})

    let ap = new announcePlayer({ db, moment, utils, config, schedulesdb, announcesdb, decodeOnly: true });
     let result1 = MasterMixer.addChannelToMixer(ap.mixer);


    ap.on(ap.eventTypes.announcesStarted, async() => {
        //MasterMixer.setVolume(sp.mixer, 0);
        MasterMixer.setFadeOutForAnnounces(schPlayer.mixer, 3000);
        console.log('fade out started for announce');
    });
    ap.on(ap.eventTypes.announcesFinished, async() => {
        //MasterMixer.setVolume(sp.mixer, 100);
        MasterMixer.setFadeInForAnnounces(schPlayer.mixer, 1500);
        console.log('fade in started after announce');
    });


    schPlayer = new schedulePlayer({db,moment,utils,config,schedulesdb,announcesdb,decodeOnly:true});
    var result= MasterMixer.addChannelToMixer(schPlayer.mixer)
    await schPlayer.checkNextPlay().catch(e=>console.log(`error in checknexPlay : ${e}`));

    MasterMixer.setVolume(schPlayer.mixer,100);



    announcesDownloader = new AnnouncesDownloader({ db, moment, config, utils });
    await announcesDownloader.start().catch(e=>console.log(`error in announcesDownloader : ${e}`));;


    schedulesDownloader = new SchedulesDownloader({ db, moment, config, utils });
    await schedulesDownloader.start().catch(e=>console.log(`error in schedulesDownloader : ${e}`));;



}

Main();
// setInterval(() => {}, 500)

