using RioJoy.Core.Mapping;

namespace RioJoy.Core.Serial;

/// <summary>
/// <see cref="ILampSink"/> that sends lamp feedback back to the RIO as
/// <c>LampRequest</c> packets over the serial link. Writes are fire-and-forget
/// (lamp feedback is best-effort and must not block input routing).
/// </summary>
public sealed class SerialLampSink : ILampSink
{
    private readonly RioSerialLink _link;

    public SerialLampSink(RioSerialLink link)
    {
        _link = link ?? throw new ArgumentNullException(nameof(link));
    }

    public void SetLamp(int address, byte lampState)
    {
        _ = SendAsync((byte)address, lampState);
    }

    private async Task SendAsync(byte address, byte state)
    {
        try
        {
            await _link.SetLampAsync(address, state).ConfigureAwait(false);
        }
        catch
        {
            // Best-effort: a dropped lamp update must not crash the input path.
        }
    }
}
