let winston = require('winston'),
fs = require('fs'),appRoot=process.cwd(),
 moment = require('moment'),
logger, path = require('path'),logDir = path.join(appRoot, 'log');
require('winston-daily-rotate-file');

winston.setLevels(winston.config.npm.levels);
winston.addColors(winston.config.npm.colors);

let foldernames = {
    DownloadSchedules: 'DownloadSchedules',
    DownloadAnnounces: 'DownloadAnnounces',
    PlayerEventsAnnounces: 'PlayerEventsAnnounces',
    PlayerEventsSchedules: 'PlayerEventsSchedules',
    PlayingAnnounces: 'PlayingAnnounces',
    PlayingSchedules: 'PlayingSchedules',
    System: 'System',
    SyncConnection: 'SyncConnection',
    Debugger: 'Debugger',
    AudioEvents: 'AudioEvents',
    DownloadManager: 'DownloadManager',
    iceCast: 'iceCast',
    PLayerUpdate: 'PLayerUpdate',
    SSHTerminal: 'SSHTerminal',
    PushAudio: 'PushAudio',
    IceCastPlayer: 'IceCastPlayer',
    StreamUrl: 'StreamUrl',
    FileManager: 'FileManager',
    Downloader: 'Downloader',
    DJPlayer:'DJPlayer',
    AutoUpdate:'AutoUpdate',
    ThirdParty: 'ThirdParty'
};
// console.log(`logdir is ${logDir}`);

let folderDownloadSchedules = path.join(logDir, foldernames.DownloadSchedules);
let folderDownloadAnnounces = path.join(logDir, foldernames.DownloadAnnounces);
let folderPlayerEventsAnnounces = path.join(logDir, foldernames.PlayerEventsAnnounces);
let folderPlayerEventsSchedules = path.join(logDir, foldernames.PlayerEventsSchedules);
let folderPlayingAnnounces = path.join(logDir, foldernames.PlayingAnnounces);
let folderPlayingSchedules = path.join(logDir, foldernames.PlayingSchedules);
let folderSystem = path.join(logDir, foldernames.System);
let folderSyncConnection = path.join(logDir, foldernames.SyncConnection);
let folderDebugger = path.join(logDir, foldernames.Debugger);
let folderAudioEvents = path.join(logDir, foldernames.AudioEvents);
let folderDownloadManager = path.join(logDir, foldernames.DownloadManager);
let foldericeCast = path.join(logDir, foldernames.iceCast);
let folderplayerupdate = path.join(logDir, foldernames.PlayerUpdate);
let folderSSHTerminal = path.join(logDir, foldernames.SSHTerminal);
let folderPushAudio = path.join(logDir, foldernames.PushAudio);
let folderIceCastPlayer = path.join(logDir, foldernames.IceCastPlayer);
let folderStreamUrl = path.join(logDir, foldernames.StreamUrl);
let folderFileManager = path.join(logDir, foldernames.FileManager);
let folderDownloader = path.join(logDir, foldernames.Downloader);
let folderDJPlayer = path.join(logDir, foldernames.DJPlayer);
let folderAutoUpdate = path.join(logDir, foldernames.AutoUpdate);
let folderThirdParty = path.join(logDir, foldernames.ThirdParty);

// Create the directory if it does not exist
if (!fs.existsSync(logDir))fs.mkdirSync(logDir);
if (!fs.existsSync(folderDownloadSchedules)) fs.mkdirSync(folderDownloadSchedules);
if (!fs.existsSync(folderDownloadAnnounces))fs.mkdirSync(folderDownloadAnnounces);
if (!fs.existsSync(folderPlayerEventsAnnounces))fs.mkdirSync(folderPlayerEventsAnnounces);
if (!fs.existsSync(folderPlayerEventsSchedules))    fs.mkdirSync(folderPlayerEventsSchedules);
if (!fs.existsSync(folderPlayingAnnounces))    fs.mkdirSync(folderPlayingAnnounces);
if (!fs.existsSync(folderPlayingSchedules))    fs.mkdirSync(folderPlayingSchedules);
if (!fs.existsSync(folderSystem))    fs.mkdirSync(folderSystem);
if (!fs.existsSync(folderSyncConnection))    fs.mkdirSync(folderSyncConnection);
if (!fs.existsSync(folderDebugger))    fs.mkdirSync(folderDebugger);
if (!fs.existsSync(folderAudioEvents))    fs.mkdirSync(folderAudioEvents);
if (!fs.existsSync(folderDownloadManager))    fs.mkdirSync(folderDownloadManager);
if (!fs.existsSync(foldericeCast))    fs.mkdirSync(foldericeCast);
if (!fs.existsSync(folderplayerupdate))    fs.mkdirSync(folderplayerupdate);
if (!fs.existsSync(folderSSHTerminal))    fs.mkdirSync(folderSSHTerminal);
if (!fs.existsSync(folderPushAudio))    fs.mkdirSync(folderPushAudio);
if (!fs.existsSync(folderStreamUrl))    fs.mkdirSync(folderStreamUrl);
if (!fs.existsSync(folderIceCastPlayer))    fs.mkdirSync(folderIceCastPlayer);
if (!fs.existsSync(folderFileManager))    fs.mkdirSync(folderFileManager);
if (!fs.existsSync(folderDownloader))    fs.mkdirSync(folderDownloader);
if (!fs.existsSync(folderDJPlayer))    fs.mkdirSync(folderDJPlayer);
if (!fs.existsSync(folderAutoUpdate))    fs.mkdirSync(folderAutoUpdate);
if(!fs.existsSync(folderThirdParty)) fs.mkdirSync(folderThirdParty);


let transport = new (winston.transports.DailyRotateFile)({
    timestamp: () => {
        return moment().utc(true).format('YYYY-MM-DD HH:mm:ss');
    },
    filename: 'debug',
    dirname: folderDebugger,
    //datePattern: 'yyyyMMdd.log',
    datePattern: 'YYYY-MM-DD',
    level: 'info',
    maxFiles: '7d', maxsize: '10m', colorize: true, tailable: true,
    json: true,
    exitOnError: false,
    zippedArchive: false
});
    new (winston.transports.Console)({
    timestamp: () => {
        return moment().utc(true).format('YYY-MM-DD HH:mm:ss');
    }, level: 'info', colorize: true
});
