﻿using System;
using System.Globalization;
using System.IO;

namespace BFHeroes_MasterServer.Components
{
    class Logger
    {
        public const int TYPE_NOTIFICATION = 0;
        public const int TYPE_WARNING = 1;
        public const int TYPE_ERROR = 2;

        public const string GOS_REDIRECTOR = "GOS Redirector";
        public const string BLAZE_CLIENT = "Blaze Client";

        private static bool useColor = true;
        private static bool createLog = false;
        private static bool logTimestamp = false;
        private static int fileLogLevel = 3;
        private static int consoleLogLevel = 1;

        private static string logFilename;

        public Logger(bool loadConfig = true)
        {
            if (loadConfig)
            {
                useColor = (bool)MasterServer.config.GetValue("console", "use_colors");
                createLog = (bool)MasterServer.config.GetValue("debug", "create_log");
                logTimestamp = (bool)MasterServer.config.GetValue("debug", "log_timestamp");
                fileLogLevel = (int)MasterServer.config.GetValue("debug", "file_log_level");
                consoleLogLevel = (int)MasterServer.config.GetValue("debug", "console_log_level");
            }

            if (createLog == true)
            {
                if (logTimestamp == true)
                {
                    logFilename = "server_" + DateTime.Now.ToString("yyyy_MM_dd__HH_mm_ss", CultureInfo.InvariantCulture) + ".log";
                }
                else
                {
                    logFilename = "server.log";
                    File.Delete(logFilename);
                }
            }
        }

        public void Notification(string from, string message, int level)
        {
            NewMessage(from, message, TYPE_NOTIFICATION, level, false);
        }

        public void Warning(string from, string message, int level)
        {
            NewMessage(from, message, TYPE_WARNING, level, false);
        }

        public void Error(string from, string message, int level, bool waitForUser = false)
        {
            NewMessage(from, message, TYPE_ERROR, level, waitForUser);
        }

        private static void NewMessage(string from, string message, int type, int level, bool waitForUser)
        {
            if (useColor)
                SetLineColor(from, type);

            message = "[" + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff", CultureInfo.InvariantCulture) + "][" + from + "] " + message;

            if (level <= consoleLogLevel)
                WriteMessage(message);

            if (level <= fileLogLevel && createLog)
                WriteMessageToFile(message);

            if (waitForUser)
                Console.ReadKey();
        }

        public static void WriteMessage(string message)
        {
            Console.WriteLine(message.PadRight(Console.WindowWidth - 1));
            Console.ResetColor();
        }

        public static void WriteMessageToFile(string message)
        {
            File.AppendAllText(logFilename, message);
        }

        private static void SetLineColor(string from, int type)
        {
            switch (type)
            {
                case TYPE_NOTIFICATION:
                    switch (from)
                    {
                        case GOS_REDIRECTOR:
                            Console.ForegroundColor = ConsoleColor.Green;
                            break;
                        case BLAZE_CLIENT:
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            break;
                        default:
                            Console.ForegroundColor = ConsoleColor.White;
                            break;
                    }
                    break;
                case TYPE_WARNING:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    break;
                case TYPE_ERROR:
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
            }
        }
    }
}
