namespace RobotNet.RobotManager.Services;

public class LoggerController<T>(ILogger<T> Logger) where T : class
{
    public event Action? LoggerUpdate;
    public void Write(string message, LogLevel level)
    {
        switch (level)
        {
            case LogLevel.Trace:
                Logger.LogTrace("{mes}", message);
                break;
            case LogLevel.Debug:
                Logger.LogDebug("{mes}", message);
                break;
            case LogLevel.Information:
                Logger.LogInformation("{mes}", message);
                break;
            case LogLevel.Warning:
                Logger.LogWarning("{mes}", message);
                break;
            case LogLevel.Error:
                Logger.LogError("{mes}", message);
                break;
            case LogLevel.Critical:
                Logger.LogCritical("{mes}", message);
                break;
        }
        LoggerUpdate?.Invoke();
    }

    public void Write(string message)
    {
        Write(message, LogLevel.Information);
    }

    public async Task WriteAsync(string message)
    {
        var write = Task.Run(() => Write(message));
        await write.WaitAsync(CancellationToken.None);
    }

    public async Task TraceAsync(string message)
    {
        var write = Task.Run(() => Write(message, LogLevel.Trace));
        await write.WaitAsync(CancellationToken.None);
    }

    public async Task DebugAsync(string message)
    {
        var write = Task.Run(() => Write(message, LogLevel.Debug));
        await write.WaitAsync(CancellationToken.None);
    }

    public async Task InfoAsync(string message)
    {
        var write = Task.Run(() => Write(message, LogLevel.Information));
        await write.WaitAsync(CancellationToken.None);
    }

    public async Task WarningAsync(string message)
    {
        var write = Task.Run(() => Write(message, LogLevel.Warning));
        await write.WaitAsync(CancellationToken.None);
    }

    public async Task ErrorAsync(string message)
    {
        var write = Task.Run(() => Write(message, LogLevel.Error));
        await write.WaitAsync(CancellationToken.None);
    }

    public async Task CriticalAsync(string message)
    {
        var write = Task.Run(() => Write(message, LogLevel.Critical));
        await write.WaitAsync(CancellationToken.None);
    }

    public void Trace(string message)
    {
        Write(message, LogLevel.Trace);
    }

    public void Debug(string message)
    {
        Write(message, LogLevel.Debug);
    }

    public void Info(string message)
    {
        Write(message, LogLevel.Information);
    }

    public void Warning(string message)
    {
        Write(message, LogLevel.Warning);
    }

    public void Error(string message)
    {
        Write(message, LogLevel.Error);
    }

    public void Critical(string message)
    {
        Write(message, LogLevel.Critical);
    }
}
