using Peak.Can.Basic;
using IOModuleTestBlazor.Models;

namespace IOModuleTestBlazor.Services;

public interface ICanService
{
    bool IsConnected { get; }
    string ChannelName { get; }

    /// <summary>
    /// Raised on the worker thread each time a CAN message passes all filters.
    /// Subscribers must marshal UI updates with InvokeAsync(StateHasChanged).
    /// </summary>
    event Action<CanMessageDto> MessageReceived;

    // ── Filters ───────────────────────────────────────────────────────────────
    IReadOnlyList<CanFilter> Filters { get; }
    CanFilter AddFilter(CanFilter filter);
    bool RemoveFilter(Guid id);
    void ClearFilters();

    // ── Bitmasks ──────────────────────────────────────────────────────────────
    IReadOnlyList<CanBitmask> Bitmasks { get; }
    CanBitmask AddBitmask(CanBitmask bitmask);
    bool RemoveBitmask(Guid id);
    void ClearBitmasks();

    // ── Helpers used by the worker ────────────────────────────────────────────
    bool PassesFilter(uint messageId);
    IReadOnlyDictionary<string, double> ExtractSignals(uint messageId, byte[] data);
    void PublishMessage(CanMessageDto dto);

    // ── PCAN passthrough ──────────────────────────────────────────────────────
    void Initialize(PcanChannel channel, Bitrate bitrate);
    void Uninitialize();
    PcanStatus Read(out PcanMessage msg, out ulong timestamp);
    PcanStatus Write(PcanMessage msg);
}
