using Serilog.Events;
using System;

namespace VeloeMinecraftLauncher.Utils.Logger;

public class EventSink : Serilog.Core.ILogEventSink
{
    private readonly IFormatProvider? _formatProvider = null;
    public event EventHandler? DataReceived;

    public EventSink(IFormatProvider? formatProvider)
    {
        _formatProvider = formatProvider;
    }

    public void Emit(LogEvent logEvent)
    {
        string message;
        if (_formatProvider is not null)
            message = logEvent.RenderMessage(_formatProvider);
        else
        {
            message = $"[{logEvent.Timestamp:HH:mm:ss} {logEvent.Level.ToString()}] {logEvent.RenderMessage()}{logEvent.Exception}\n";
        }
        if (DataReceived != null)
        {
            var data = EventArgs.Empty;
            DataReceived?.Invoke(this, new MyEventArgs(message));
        }
    }
}

public class MyEventArgs : EventArgs
{
    public string Data;
    public MyEventArgs(string _myData)
    {
        Data = _myData;
    }
}
