class clsLogger {
    constructor() {
        const appRoot = process.cwd();
        const winston = require('winston');
        const fs = require('fs');
        const moment = require('moment');
        const path = require('path');

        require('winston-daily-rotate-file');
        winston.setLevels(winston.config.npm.levels);
        winston.addColors(winston.config.npm.colors);
        const 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'
        };
        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.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);


        // 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.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);

        // this.rotateFile(this.folderDownloadSchedules);
        // this.rotateFile(this.folderDownloadAnnounces);
        // this.rotateFile(this.folderPlayerEventsAnnounces);
        // this.rotateFile(this.folderPlayerEventsSchedules);
        // this.rotateFile(this.folderSystem);
        // this.rotateFile(this.folderSyncConnection);
        // this.rotateFile(this.folderDebugger);
        // this.rotateFile(this.folderAudioEvents);
        // this.rotateFile(this.folderDownloadManager);
        // this.rotateFile(this.foldericeCast);
        // this.rotateFile(this.folderplayerupdate);
        // this.rotateFile(this.folderSSHTerminal);
        // this.rotateFile(this.folderPushAudio);
        // this.rotateFile(this.folderStreamUrl);
        // this.rotateFile(this.folderIceCastPlayer);
        // this.rotateFile(this.folderFileManager);
        // this.rotateFile(this.folderDownloader);
        // this.rotateFile(this.folderDJPlayer);
        // this.rotateFile(this.folderAutoUpdate);
        // this.rotateFile(this.folderThirdParty);
        //this.rotateFiles();

        class logTransport {
            constructor (dirname){

                this.filename = 'log-%DATE%.log';
                this.dirname = dirname;
                this.datePattern = 'YYYY-MM-DD';
                this.level = 'info';
                this.maxFiles = '7d';
                this.maxsize = '10m';
                this.colorize = true;
                this.tailable = true;
                this.json = true;
                this.exitOnError = false;
                this.zippedArchive = false;
                this.timestamp = () => {
                  return moment().utc(true).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        }

        class constantLog {
            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;
                this.timestamp = () => {
                    return moment().utc(true).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        }

        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 new winston.Logger({
                    levels: {
                        info: 1
                    },
                    transports: [
                        new (winston.transports.DailyRotateFile)(new logTransport(path)),
                        new (winston.transports.Console)(new consoleTransport())
                    ]
                });
            }
            // constLogger(path) {
            //     return new winston.Logger({
            //         levels: {
            //             info: 1
            //         },
            //         transports: [
            //             new (winston.transports.DailyRotateFile)(new constantLog(path)),
            //             new (winston.transports.Console)(new consoleTransport())
            //         ]
            //     });
            // }

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

            // rotateFile(folder) {
            //     let afterTime = 7;
            //     const regex = /\d{4}-\d{2}-\d{2}/gm;
            //     let logsFolder = fs.readdirSync(folder);
            //     let m;
            //     while ((m = regex.exec(logsFolder)) !== null) {
            //         if (m.index === regex.lastIndex) {
            //             regex.lastIndex++;
            //         }
            //         m.forEach((match, groupIndex) => {
            //             let q = new moment(match).utc(true);
            //             if(now.diff(q, 'days') >= afterTime) {
            //                 try {
            //                     let p = path.join(folder, `log-${match}.log`);
            //                     console.log(p);
            //                     fs.unlinkSync(p)
            //                 } catch (e) {
            //                     console.log(e);
            //                 }
            //             }
            //         });
            //     }
            // }
        }

        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.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.DownloadSchedules1 = new winston.Logger({
            levels: {
                info: 1
            },
            transports: [
                new (winston.transports.DailyRotateFile)({
                    timestamp: function () {
                        return moment().format('YYYY-MM-DD HH:mm:ss');
                    },
                    filename: 'DownloadSchedules',
                    dirname: this.folderDownloadSchedules,
                    datePattern: 'YYYY-MM-DD',
                    level: 'info',
                    maxFiles: 2, maxsize: 10485760, colorize: true, tailable: true,
                    json: true,
                    exitOnError: false
                }),
                new (winston.transports.Console)({
                    timestamp: function () {
                        return moment().format('YYYY-MM-DD HH:mm:ss');
                    }, level: 'info', colorize: true
                })
            ]
        });
        */

    }

    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)
    }
    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);
    }

    // rotateFile(folder) {
    //     let fs = require('fs');
    //     let moment = require('moment');
    //     let path = require('path');
    //     let now = new moment().utc(true);
    //     let afterTime = 7;
    //     const regex = /\d{4}-\d{2}-\d{2}/gm;
    //     let logsFolder = fs.readdirSync(folder);
    //     let totalDeleted = [];
    //     let m;
    //     while ((m = regex.exec(logsFolder)) !== null) {
    //         if (m.index === regex.lastIndex) {
    //             regex.lastIndex++;
    //         }
    //         m.forEach((match, groupIndex) => {
    //             let q = new moment(match).utc(true);
    //             if(now.diff(q, 'days') >= afterTime) {
    //                 try {
    //                     let p = path.join(folder, `log-${match}.log`);
    //                     totalDeleted.push(p);
    //                     fs.unlinkSync(p)
    //                 } catch (e) {
    //
    //                 }
    //             }
    //         });
    //     }
    //     return totalDeleted;
    // }

    // rotateFiles() {
    //     let fs = require('fs');
    //     let moment = require('moment');
    //     let path = require('path');
    //     let now = new moment().utc(true);
    //     let afterTime = 7;
    //     let totalDeleted = [];
    //     const regex = /\d{4}-\d{2}-\d{2}/gm;
    //     let logsFolder = fs.readdirSync(this.logDir);
    //     let logPath = [];
    //     logsFolder.forEach((c) => {
    //         if(c != '.DS_Store' && c != 'PlayingAnnounces' && c != 'PlayingSchedules'){
    //             logPath.push(path.join(this.logDir, c));
    //         }
    //     });
    //     let m;
    //     logPath.forEach((c) => {
    //         let qq = fs.readdirSync(c);
    //         while ((m = regex.exec(qq)) !== null) {
    //             if (m.index === regex.lastIndex) {
    //                 regex.lastIndex++;
    //             }
    //             m.forEach((match, groupIndex) => {
    //                 let q = new moment(match).utc(true);
    //                 if(now.diff(q, 'days') >= afterTime) {
    //                     try {
    //                         let p = path.join(c, `log-${match}.log`);
    //                         fs.unlinkSync(p);
    //                         totalDeleted.push(p);
    //                     } catch (e) {
    //
    //                     }
    //                 }
    //             });
    //         }
    //     });
    //     fs = null;
    //     moment = null;
    //     path = null;
    //     now = null;
    //     logsFolder = null;
    //     logPath = null;
    //     return totalDeleted;
    // }
}

module.exports={
    clsLogger:clsLogger
};
