using Microsoft.Extensions.Logging;

namespace EonaCat.Logger
{
    // This file is part of the EonaCat project(s) which is released under the Apache License.
    // See the LICENSE file or go to https://EonaCat.com/License for full license details.

    public enum ELogType
    {
        NONE = 0,
        INFO = 1,
        WARNING = 2,
        ERROR = 3,
        TRAFFIC = 4,
        DEBUG = 5,
        CRITICAL = 6,
        TRACE = 7
    }

    public static class LogTypeConverter
    {
        public static ELogType FromLogLevel(this LogLevel logLevel)
        {
            switch (logLevel)
            {
                case LogLevel.None:
                    return ELogType.NONE;
                case LogLevel.Error:
                    return ELogType.ERROR;
                case LogLevel.Debug:
                    return ELogType.DEBUG;
                case LogLevel.Critical:
                    return ELogType.CRITICAL;
                case LogLevel.Warning:
                    return ELogType.WARNING;
                case LogLevel.Trace:
                    return ELogType.TRACE;
                case LogLevel.Information:
                    return ELogType.INFO;
                default:
                    return ELogType.TRAFFIC;
            }
        }

        public static LogLevel ToLogLevel(this ELogType logLevel)
        {
            switch (logLevel)
            {
                case ELogType.NONE:
                    return LogLevel.None;
                case ELogType.ERROR:
                    return LogLevel.Error;
                case ELogType.DEBUG:
                    return LogLevel.Debug;
                case ELogType.CRITICAL:
                    return LogLevel.Critical;
                case ELogType.WARNING:
                    return LogLevel.Warning;
                case ELogType.TRACE:
                    return LogLevel.Trace;
                case ELogType.TRAFFIC:
                    return LogLevel.Trace;
                case ELogType.INFO:
                    return LogLevel.Information;
                default:
                    return LogLevel.Information;
            }
        }

        public static string ToString(this ELogType logLevel)
        {
            switch (logLevel)
            {
                case ELogType.NONE:
                    return "NONE";
                case ELogType.ERROR:
                    return "ERROR";
                case ELogType.DEBUG:
                    return "DEBUG";
                case ELogType.CRITICAL:
                    return "CRITICAL";
                case ELogType.WARNING:
                    return "WARNING";
                case ELogType.TRACE:
                    return "TRACE";
                case ELogType.TRAFFIC:
                    return "TRAFFIC";
                case ELogType.INFO:
                    return "INFO";
                default:
                    return "INFO";
            }
        }

        public static ELogType FromSeverity(this ESeverity logLevel)
        {
            switch (logLevel)
            {
                case ESeverity.Debug:
                    return ELogType.DEBUG;
                case ESeverity.Warn:
                    return ELogType.WARNING;
                case ESeverity.Emergency:
                    return ELogType.TRACE;
                case ESeverity.Critical:
                    return ELogType.CRITICAL;
                case ESeverity.Alert:
                    return ELogType.TRAFFIC;
                case ESeverity.Error:
                    return ELogType.ERROR;
                default:
                    return ELogType.INFO;
            }
        }

        public static int ToGrayLogLevel(this ELogType logLevel)
        {
            // Loglevel to GELF format
            switch (logLevel.ToString())
            {
                case "TRAFFIC": return 7;
                case "TRACE": return 7;
                case "DEBUG": return 7;
                case "INFO": return 6;
                case "WARNING": return 4;
                case "ERROR": return 3;
                case "CRITICAL": return 2;
                default: return 6; // Default to INFO
            }
        }

        public static ESeverity ToSeverity(this ELogType logLevel)
        {
            switch (logLevel)
            {
                case ELogType.DEBUG:
                    return ESeverity.Debug;
                case ELogType.WARNING:
                    return ESeverity.Warn;
                case ELogType.CRITICAL:
                    return ESeverity.Critical;
                case ELogType.TRACE:
                    return ESeverity.Emergency;
                case ELogType.ERROR:
                    return ESeverity.Error;
                case ELogType.TRAFFIC:
                    return ESeverity.Alert;
                default:
                    return ESeverity.Info;
            }
        }
    }

    /// <summary>
    /// Message severity.
    /// </summary>
    public enum ESeverity
    {
        /// <summary>
        /// Debug messages.
        /// </summary>
        Debug = 0,
        /// <summary>
        /// Informational messages.
        /// </summary>
        Info = 1,
        /// <summary>
        /// Warning messages.
        /// </summary>
        Warn = 2,
        /// <summary>
        /// Error messages.
        /// </summary>
        Error = 3,
        /// <summary>
        /// Alert messages.
        /// </summary>
        Alert = 4,
        /// <summary>
        /// Critical messages.
        /// </summary>
        Critical = 5,
        /// <summary>
        /// Emergency messages.
        /// </summary>
        Emergency = 6
    }
}