using Microsoft.Extensions.Logging;
using System;

namespace Shared;

public class FeedbackLogger : ILogger
{

    public int EventId { get; set; }

    private readonly string _Name;
    private readonly LogLevel _LogLevel;
    private readonly IFeedback _Feedback;

    public FeedbackLogger(LogLevel logLevel, IFeedback feedback, string name)
    {
        _Feedback = feedback;
        _Name = name;
        EventId = 0;
        _LogLevel = logLevel;
        if (string.IsNullOrEmpty(_Name))
        { }
    }

    public IDisposable BeginScope<TState>(TState state) => null;

    public bool IsEnabled(LogLevel logLevel) => logLevel >= _LogLevel;

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
    {
        if (IsEnabled(logLevel) && (EventId == 0 || EventId == eventId.Id))
        {
            string message = formatter(state, null);
            int color = logLevel switch
            {
                LogLevel.Trace => -1,
                LogLevel.Debug => -1,
                LogLevel.Information => -16776961,
                LogLevel.Warning => -65281,
                LogLevel.Error => -65536,
                LogLevel.Critical => -65536,
                LogLevel.None => -1,
                _ => throw new Exception(),
            };
            _Feedback.Print(message, color);
        }
    }

}