namespace Mlaumcherb.Client.Avalonia;

public class LauncherLogger : ILogger
{
    private CompositeLogger _compositeLogger;
    private FileLogger _fileLogger;
    public static readonly IOPath LogsDirectory = "launcher_logs";
    public IOPath LogFilePath => _fileLogger.LogfileName;
    
    public LauncherLogger()
    {
        _fileLogger = new FileLogger(LogsDirectory, "млаумчерб");
        _fileLogger.DebugLogEnabled = true;
        ILogger[] loggers = 
        [
            _fileLogger,
#if DEBUG
            new ConsoleLogger(),
#endif
        ];
        _compositeLogger = new CompositeLogger(loggers);
    }

    public record LogMessage(string context, LogSeverity severity, object message, ILogFormat format);

    public delegate void LogHandler(LogMessage msg);
    public event LogHandler? OnLogMessage;
    
    public void Log(string context, LogSeverity severity, object message, ILogFormat format)
    {
        _compositeLogger.Log(context, severity, message, format);
        bool isEnabled = severity switch
        {
            LogSeverity.Debug => DebugLogEnabled,
            LogSeverity.Info => InfoLogEnabled,
            LogSeverity.Warn => WarnLogEnabled,
            LogSeverity.Error => ErrorLogEnabled,
            _ => throw new ArgumentOutOfRangeException(nameof(severity), severity, null)
        };
        if(isEnabled)
            OnLogMessage?.Invoke(new(context, severity, message, format));
    }
    
    public void Dispose()
    {
        _compositeLogger.Dispose();
    }

    public ILogFormat Format
    {
        get => _compositeLogger.Format;
        set => _compositeLogger.Format = value;
    }

    public bool DebugLogEnabled
    {
        get => _compositeLogger.DebugLogEnabled;
        set
        {
            _compositeLogger.DebugLogEnabled = value;
            _fileLogger.DebugLogEnabled = true;
        }
    }

    public bool InfoLogEnabled
    {
        get => _compositeLogger.InfoLogEnabled;
        set => _compositeLogger.InfoLogEnabled = value;
    }

    public bool WarnLogEnabled
    {
        get => _compositeLogger.WarnLogEnabled;
        set => _compositeLogger.WarnLogEnabled = value;
    }

    public bool ErrorLogEnabled
    {
        get => _compositeLogger.ErrorLogEnabled;
        set => _compositeLogger.ErrorLogEnabled = value;
    }
}