using System;
using System.Collections.Generic;
using HC_APTBS.Models;
using Peak.Can.Basic;
using TPCANHandle = System.UInt16;
namespace HC_APTBS.Services
{
///
/// Abstracts CAN bus communication for the test bench.
/// The implementation () wraps
/// the PEAK PCAN-Basic native API.
///
public interface ICanService
{
// ── Events ────────────────────────────────────────────────────────────────
///
/// Raised on the CAN read background thread whenever the bus status changes.
/// message is the short status text; isOk is true when status == PCAN_ERROR_OK.
///
event Action? StatusChanged;
///
/// Raised when the bench controller starts or stops sending CAN frames.
/// alive is true when frames are being received, false after a timeout.
///
event Action? BenchLivenessChanged;
///
/// Raised when the pump ECU starts or stops sending CAN frames.
/// alive is true when frames are being received, false after a timeout.
///
event Action? PumpLivenessChanged;
// ── Properties ────────────────────────────────────────────────────────────
/// Most recent PCAN status code.
TPCANStatus CurrentStatus { get; }
/// True when the CAN read thread is running and the channel is open.
bool IsConnected { get; }
///
/// The PCAN channel handle that will be used on the next call.
/// Defaults to the channel supplied at construction.
/// Throws when set while is true.
///
TPCANHandle SelectedChannel { get; set; }
// ── Discovery ─────────────────────────────────────────────────────────────
///
/// Enumerates PCAN USB channels that are physically attached to the system.
/// Returns an empty list if no adapters are connected or if the PCAN-Basic DLL
/// is unavailable. Never throws.
///
System.Collections.Generic.IReadOnlyList EnumerateAttachedChannels();
// ── Lifecycle ─────────────────────────────────────────────────────────────
///
/// Opens the PCAN channel, performs OEM legitimation, and starts the receive thread.
///
/// True on success; false if the hardware is unavailable or legitimation fails.
bool Connect();
/// Stops the receive thread and releases the PCAN channel.
void Disconnect();
///
/// Sends a baudrate-change command to the bench firmware, then re-initialises the
/// PCAN channel at the new baudrate.
///
/// Target baudrate.
/// CAN message ID carrying the baudrate-change command.
void SwitchBaudrate(TPCANBaudrate newBaudrate, uint baudrateMessageId);
// ── Parameter map ─────────────────────────────────────────────────────────
/// Replaces the entire parameter map with the supplied dictionary.
void SetParameters(Dictionary> parameters);
/// Adds entries to the parameter map without removing existing ones.
void AddParameters(Dictionary> parameters);
/// Removes entries whose keys match the supplied dictionary.
void RemoveParameters(Dictionary> parameters);
///
/// Registers CAN message IDs that belong to the bench controller.
/// Frames with these IDs drive .
///
void RegisterBenchMessageIds(IReadOnlyCollection ids);
///
/// Registers CAN message IDs that belong to the pump ECU.
/// Frames with these IDs drive .
///
void RegisterPumpMessageIds(IReadOnlyCollection ids);
// ── Transmit ──────────────────────────────────────────────────────────────
///
/// Packs all parameters registered for into a standard
/// CAN frame (applying the calibration transfer function) and transmits it.
///
void SendMessageById(uint messageId);
///
/// Transmits a raw 8-byte CAN standard frame without any parameter lookup.
///
/// CAN message identifier.
/// Exactly 8 bytes of payload data.
void SendRawMessage(uint messageId, byte[] data);
}
}