class clsLogger {
    constructor() {
        const appRoot = process.cwd();
        const fs = require('fs');
        const moment = require('moment');
        const path = require('path');
        const winston = require('winston');
        const { createLogger, format, transports } = winston;
        const { combine, printf, colorize, align } = format;


        require('winston-daily-rotate-file');
        winston.addColors(winston.config.npm.colors);
        const foldernames = {
            DownloadSchedules: 'DownloadSchedules',
            DownloadAnnounces: 'DownloadAnnounces',
            PlayerEventsAnnounces: 'PlayerEventsAnnounces',
            PlayerEventsSchedules: 'PlayerEventsSchedules',
            PlayingAnnounces: 'PlayingAnnounces',
            PlayingSchedules: 'PlayingSchedules',
            PlayingDj: 'PlayingDj',
            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',
            Debug: 'Debug',
            PrayerTime: 'PrayerTime',
            InputPlayer: 'InputPlayer'
        };

        const myFormat = printf(info => {
            return JSON.stringify({level:info.level,messsage:info.message,timestamp:moment().utc(true).format("YYYY-MM-DD HH:mm:ss")})
        });

        this.logDir = path.join(appRoot, 'log');


        //console.log(`logdir is ${this.logDir}`)
        this.folderDownloadSchedules = path.join(this.logDir, foldernames.DownloadSchedules);
        this.folderDownloadAnnounces = path.join(this.logDir, foldernames.DownloadAnnounces);
        this.folderPlayerEventsAnnounces = path.join(this.logDir, foldernames.PlayerEventsAnnounces);
        this.folderPlayerEventsSchedules = path.join(this.logDir, foldernames.PlayerEventsSchedules);
        this.folderPlayingAnnounces = path.join(this.logDir, foldernames.PlayingAnnounces);
        this.folderPlayingSchedules = path.join(this.logDir, foldernames.PlayingSchedules);
        this.folderPlayingDj = path.join(this.logDir, foldernames.PlayingDj);
        this.folderSystem = path.join(this.logDir, foldernames.System);
        this.folderSyncConnection = path.join(this.logDir, foldernames.SyncConnection);
        this.folderDebugger = path.join(this.logDir, foldernames.Debugger);
        this.folderAudioEvents = path.join(this.logDir, foldernames.AudioEvents);
        this.folderDownloadManager = path.join(this.logDir, foldernames.DownloadManager);
        this.foldericeCast = path.join(this.logDir, foldernames.iceCast);
        this.folderplayerupdate = path.join(this.logDir, foldernames.PLayerUpdate);
        this.folderSSHTerminal = path.join(this.logDir, foldernames.SSHTerminal);
        this.folderPushAudio = path.join(this.logDir, foldernames.PushAudio);
        this.folderIceCastPlayer = path.join(this.logDir, foldernames.IceCastPlayer);
        this.folderStreamUrl = path.join(this.logDir, foldernames.StreamUrl);
        this.folderFileManager = path.join(this.logDir, foldernames.FileManager);
        this.folderDownloader = path.join(this.logDir, foldernames.Downloader);
        this.folderDJPlayer = path.join(this.logDir, foldernames.DJPlayer);
        this.folderAutoUpdate = path.join(this.logDir, foldernames.AutoUpdate);
        this.folderThirdParty = path.join(this.logDir, foldernames.ThirdParty);
        this.folderDebug = path.join(this.logDir, foldernames.Debug);
        this.folderPrayerTime = path.join(this.logDir, foldernames.PrayerTime);
        this.folderInputPlayer = path.join(this.logDir, foldernames.InputPlayer);


        // Create the directory if it does not exist


        if (!fs.existsSync(this.logDir))fs.mkdirSync(this.logDir);
        if (!fs.existsSync(this.folderDownloadSchedules))fs.mkdirSync(this.folderDownloadSchedules);
        if (!fs.existsSync(this.folderDownloadAnnounces))fs.mkdirSync(this.folderDownloadAnnounces);
        if (!fs.existsSync(this.folderPlayerEventsAnnounces))fs.mkdirSync(this.folderPlayerEventsAnnounces);
        if (!fs.existsSync(this.folderPlayerEventsSchedules))fs.mkdirSync(this.folderPlayerEventsSchedules);
        if (!fs.existsSync(this.folderPlayingAnnounces))fs.mkdirSync(this.folderPlayingAnnounces);
        if (!fs.existsSync(this.folderPlayingSchedules))fs.mkdirSync(this.folderPlayingSchedules);
        if(!fs.existsSync(this.folderPlayingDj))fs.mkdirSync(this.folderPlayingDj);
        if (!fs.existsSync(this.folderSystem))fs.mkdirSync(this.folderSystem);
        if (!fs.existsSync(this.folderSyncConnection))fs.mkdirSync(this.folderSyncConnection);
        //if (!fs.existsSync(this.folderDebugger))fs.mkdirSync(this.folderDebugger);
        if (!fs.existsSync(this.folderAudioEvents))fs.mkdirSync(this.folderAudioEvents);
        if (!fs.existsSync(this.folderDownloadManager))fs.mkdirSync(this.folderDownloadManager);
        if (!fs.existsSync(this.foldericeCast))fs.mkdirSync(this.foldericeCast);
        if (!fs.existsSync(this.folderplayerupdate))fs.mkdirSync(this.folderplayerupdate);
        if (!fs.existsSync(this.folderSSHTerminal))fs.mkdirSync(this.folderSSHTerminal);
        if (!fs.existsSync(this.folderPushAudio))fs.mkdirSync(this.folderPushAudio);
        if (!fs.existsSync(this.folderStreamUrl))fs.mkdirSync(this.folderStreamUrl);
        if (!fs.existsSync(this.folderIceCastPlayer))fs.mkdirSync(this.folderIceCastPlayer);
        if (!fs.existsSync(this.folderFileManager))fs.mkdirSync(this.folderFileManager);
        if (!fs.existsSync(this.folderDownloader))fs.mkdirSync(this.folderDownloader);
        if (!fs.existsSync(this.folderDJPlayer))fs.mkdirSync(this.folderDJPlayer);
        if (!fs.existsSync(this.folderAutoUpdate))    fs.mkdirSync(this.folderAutoUpdate);
        if(!fs.existsSync(this.folderThirdParty)) fs.mkdirSync(this.folderThirdParty);
        if(!fs.existsSync(this.folderDebug))  fs.mkdirSync(this.folderDebug);
        if(!fs.existsSync(this.folderPrayerTime)) fs.mkdirSync(this.folderPrayerTime);
        if(!fs.existsSync(this.folderInputPlayer)) fs.mkdirSync(this.folderInputPlayer);

        class logTransport {
            constructor (dirname){

                this.filename = 'log-%DATE%.log';
                this.dirname = dirname;
                this.datePattern = 'YYYY-MM-DD';
                this.level = 'info';
                this.maxSize = '10m';
                this.colorize = true;
                this.tailable = true;
                this.json = true;
                this.exitOnError = false;
                this.zippedArchive = false;
            }
        }

        // class consoleTransport {
        //     constructor() {
        //         this.timestamp = () => {
        //             return moment().utc(true).format('YYYY-MM-DD HH:mm:ss');
        //         };
        //         this.level = 'info';
        //         this.colorize = true;
        //     }
        // }

        class myLogger {
            logger(path) {
                return createLogger({
                    levels: {
                        info: 1
                    },
                    format: myFormat,
                    transports: [
                        new (transports.DailyRotateFile)(new logTransport(path)),
                        new transports.Console({
                            format: combine(
                                format.colorize(),
                                format.simple()
                            )
                        })
                    ]
                });
            }

            logWithOutConsole(path) {
                return  createLogger({
                    levels: {
                        info: 1
                    },
                    format: myFormat,
                    transports: [
                        new (transports.DailyRotateFile)(new logTransport(path))
                    ]
                });
            }

        }

        this.DownloadSchedulesLog = new myLogger().logger(this.folderDownloadSchedules);

        this.DownloadAnnouncesLog = new myLogger().logger(this.folderDownloadAnnounces);

        this.PlayerEventsAnnouncesLog = new myLogger().logWithOutConsole(this.folderPlayerEventsAnnounces);

        this.PlayerEventsSchedulesLog = new myLogger().logWithOutConsole(this.folderPlayerEventsSchedules);

        this.PlayingSchedulesLog = new myLogger().logger(this.folderPlayingSchedules);

        this.PlayingAnnouncesLog = new myLogger().logger(this.folderPlayingAnnounces);

        this.PlayingDjLog = new myLogger().logger(this.folderPlayingDj);

        this.SystemLog = new myLogger().logger(this.folderSystem);

        this.SyncConnectionLog = new myLogger().logger(this.folderSyncConnection);

        this.AudioEventsLog = new myLogger().logger(this.folderAudioEvents);

        this.iceCastLog = new myLogger().logWithOutConsole(this.foldericeCast);

        this.DownloadManagerLog = new myLogger().logger(this.folderDownloadManager);

        this.PlayerUpdateLog = new myLogger().logger(this.folderplayerupdate);

        this.SSHTerminalLog = new myLogger().logger(this.folderSSHTerminal);

        this.PushAudioLog = new myLogger().logger(this.folderPushAudio);

        this.StreamUrlLog = new myLogger().logWithOutConsole(this.folderStreamUrl);

        this.IceCastPlayerLog = new myLogger().logger(this.folderIceCastPlayer);

        this.FileManagerLog = new myLogger().logger(this.folderFileManager);

        this.DownloaderLog = new myLogger().logger(this.folderDownloader);

        this.PlayerEventsDJLog = new myLogger().logger(this.folderDJPlayer);

        this.AutoUpdatesLog = new myLogger().logger(this.folderAutoUpdate);

        this.ThirdPartyLog = new myLogger().logger(this.folderThirdParty);

        this.DebugLog = new myLogger().logger(this.folderDebug);

        this.PrayerTimeLog = new myLogger().logger(this.folderPrayerTime);

        this.InputPlayerLog = new myLogger().logger(this.folderInputPlayer);

    }

    DownloadSchedules(msg){
        this.DownloadSchedulesLog.info(msg)
    }
    DownloadAnnounces(msg){
        this.DownloadAnnouncesLog.info(msg)
    }
    PlayerEventsAnnounces(msg){
        this.PlayerEventsAnnouncesLog.info(msg)
    }
    PlayerEventsSchedules(msg){
        this.PlayerEventsSchedulesLog.info(msg)
    }
    PlayingSchedules(msg){
        this.PlayingSchedulesLog.info(msg)
    }
    PlayingAnnounces(msg){
        this.PlayingAnnouncesLog.info(msg)
    }
    PlayingDj(msg){
        this.PlayingDjLog.info(msg)
    }
    System(msg){
        this.SystemLog.info(msg)
    }
    SyncConnection(msg){
        this.SyncConnectionLog.info(msg)
    }
    AudioEvents(msg){
        this.AudioEventsLog.info(msg)
    }
    iceCast(msg){
        this.iceCastLog.info(msg)
    }
    DownloadManager(msg){
        this.DownloadManagerLog.info(msg)
    }
    PlayerUpdate(msg){
        this.PlayerUpdateLog.info(msg)
    }
    SSHTerminal(msg){
        this.SSHTerminalLog.info(msg)
    }
    PushAudio(msg){
        this.PushAudioLog.info(msg)
    }
    StreamUrl(msg){
        this.StreamUrlLog.info(msg)
    }
    IceCastPlayer(msg){
        this.IceCastPlayerLog.info(msg)
    }
    FileManager(msg){
        this.FileManagerLog.info(msg)
    }
    Downloader(msg){
        this.DownloaderLog.info(msg)
    }
    PlayerEventsDJ(msg){
        this.PlayerEventsDJLog.info(msg)
    }
    AutoUpdates(msg){
        this.AutoUpdatesLog.info(msg)
    }
    ThirdParty(msg) {
        this.ThirdPartyLog.info(msg);
    }

    Debug(msg) {
        this.DebugLog.info(msg);
    }

    PrayerTime(msg) {
        this.PrayerTimeLog.info(msg);
    }

    InputPlayer(msg) {
        this.InputPlayerLog.info(msg);
    }

}

module.exports={
    clsLogger:clsLogger
};
