using Microsoft.Extensions.Logging;

namespace EonaCat.LogStack;
// 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 static class LogTypeConverter
{
    public static Core.LogLevel FromLogLevel(this LogLevel logLevel)
    {
        switch (logLevel)
        {
            case LogLevel.None:
                return Core.LogLevel.None;
            case LogLevel.Error:
                return Core.LogLevel.Error;
            case LogLevel.Debug:
                return Core.LogLevel.Debug;
            case LogLevel.Critical:
                return Core.LogLevel.Critical;
            case LogLevel.Warning:
                return Core.LogLevel.Warning;
            case LogLevel.Trace:
                return Core.LogLevel.Trace;
            case LogLevel.Information:
                return Core.LogLevel.Information;
            default:
                return Core.LogLevel.Trace;
        }
    }

    public static LogLevel ToLogLevel(this Core.LogLevel logLevel)
    {
        switch (logLevel)
        {
            case Core.LogLevel.None:
                return LogLevel.None;
            case Core.LogLevel.Error:
                return LogLevel.Error;
            case Core.LogLevel.Debug:
                return LogLevel.Debug;
            case Core.LogLevel.Critical:
                return LogLevel.Critical;
            case Core.LogLevel.Warning:
                return LogLevel.Warning;
            case Core.LogLevel.Trace:
                return LogLevel.Trace;
            case Core.LogLevel.Information:
                return LogLevel.Information;
            default:
                return LogLevel.Information;
        }
    }

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

    public static Core.LogLevel FromSeverity(this ESeverity logLevel)
    {
        switch (logLevel)
        {
            case ESeverity.Debug:
                return Core.LogLevel.Debug;
            case ESeverity.Warn:
                return Core.LogLevel.Warning;
            case ESeverity.Emergency:
                return Core.LogLevel.Trace;
            case ESeverity.Critical:
                return Core.LogLevel.Critical;
            case ESeverity.Error:
                return Core.LogLevel.Error;
            default:
                return Core.LogLevel.Information;
        }
    }

    public static int ToGrayLogLevel(this Core.LogLevel logLevel)
    {
        // Loglevel to GELF format
        switch (logLevel.ToString())
        {
            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 Core.LogLevel logLevel)
    {
        switch (logLevel)
        {
            case Core.LogLevel.Debug:
                return ESeverity.Debug;
            case Core.LogLevel.Warning:
                return ESeverity.Warn;
            case Core.LogLevel.Critical:
                return ESeverity.Critical;
            case Core.LogLevel.Trace:
                return ESeverity.Emergency;
            case Core.LogLevel.Error:
                return ESeverity.Error;
            default:
                return ESeverity.Info;
        }
    }
}

public enum SyslogFacility
{
    Kernel = 0,            // 0 - Kernel messages
    UserLevel = 1,         // 1 - User-level messages
    MailSystem = 2,        // 2 - Mail system
    Daemon = 3,            // 3 - Daemon messages
    Auth = 4,              // 4 - Security/authorization messages
    Syslog = 5,            // 5 - Messages generated by syslogd
    Lpr = 6,               // 6 - Line printer subsystem
    News = 7,              // 7 - Network news subsystem
    UUCP = 8,              // 8 - UUCP subsystem
    Clock = 9,             // 9 - Clock daemon
    AuthPriv = 10,         // 10 - Security/authorization messages (privileged)
    Ftp = 11,              // 11 - FTP daemon
    Ntp = 12,              // 12 - NTP subsystem
    Audit = 13,            // 13 - Audit messages
    Alert = 14,            // 14 - Log alert messages
    Cron = 15,             // 15 - Cron daemon
    Local0 = 16,           // 16 - Local use 0 (custom usage)
    Local1 = 17,           // 17 - Local use 1 (custom usage)
    Local2 = 18,           // 18 - Local use 2 (custom usage)
    Local3 = 19,           // 19 - Local use 3 (custom usage)
    Local4 = 20,           // 20 - Local use 4 (custom usage)
    Local5 = 21,           // 21 - Local use 5 (custom usage)
    Local6 = 22,           // 22 - Local use 6 (custom usage)
    Local7 = 23            // 23 - Local use 7 (custom usage)
}

/// <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
}