using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using HC_APTBS.Models;
namespace HC_APTBS.Services
{
///
/// Provides KWP2000 / KW1281 diagnostics operations over the ISO K-Line
/// via an FTDI USB-to-K-Line adapter.
///
public interface IKwpService
{
// ── Session lifecycle ─────────────────────────────────────────────────────
/// Current state of the persistent K-Line session.
KLineConnectionState KLineState { get; }
///
/// Raised whenever the K-Line session state transitions.
/// Fires on a background thread; consumers must marshal to the UI thread.
///
event Action? KLineStateChanged;
///
/// Opens a persistent K-Line session: performs 5-baud slow-init,
/// reads ECU info, then starts a background keep-alive loop (~1 s interval).
///
/// FTDI serial number or COM port identifier.
/// Cancellation token.
Task ConnectAsync(string port, CancellationToken ct = default);
///
/// Stops the keep-alive loop, sends EndCommunication to the ECU,
/// and disposes the FTDI interface.
///
void Disconnect();
// ── Progress reporting ────────────────────────────────────────────────────
///
/// Raised during long operations to report completion percentage and a status message.
/// Always marshalled to the UI thread by the implementation.
///
event Action? ProgressChanged;
// ── Full ECU read ─────────────────────────────────────────────────────────
///
/// Connects to the pump ECU over K-Line and reads all available identification data,
/// fault codes, DFI value, serial number, and software versions.
///
/// FTDI serial number or COM port identifier.
/// KWP protocol variant (0=V1, 1=V2, 2=V3/V4).
/// Cancellation token.
///
/// Dictionary with keys from .
/// The result key is "1" on success, "0" on failure.
///
Task> ReadAllInfoAsync(
string port, int pumpVersion, CancellationToken ct = default);
// ── DTC operations ────────────────────────────────────────────────────────
/// Reads all current diagnostic trouble codes from the ECU.
/// FTDI serial number or COM port identifier.
/// Cancellation token.
/// Formatted fault code string, or "No fault codes".
Task ReadFaultCodesAsync(string port, CancellationToken ct = default);
/// Clears all diagnostic trouble codes and returns the post-clear state.
/// FTDI serial number or COM port identifier.
/// Cancellation token.
/// Post-clear fault code string.
Task ClearFaultCodesAsync(string port, CancellationToken ct = default);
// ── DFI calibration ───────────────────────────────────────────────────────
/// Reads the current DFI calibration angle from EEPROM address 0x0044.
/// FTDI serial number or COM port identifier.
/// Cancellation token.
/// DFI value in degrees as a formatted string.
Task ReadDfiAsync(string port, CancellationToken ct = default);
///
/// Writes a new DFI calibration angle to EEPROM address 0x0044.
///
/// FTDI serial number or COM port identifier.
/// Target DFI angle (degrees, range ±1.45°).
/// KWP protocol variant (selects the authentication password).
/// Cancellation token.
/// Verified DFI value read back from EEPROM after the write.
Task WriteDfiAsync(
string port, float dfi, int version, CancellationToken ct = default);
///
/// Writes DFI to EEPROM, then triggers a pump power cycle via
/// /
/// to activate the new calibration.
///
Task WriteDfiAndRestartAsync(
string port, float dfi, int version, CancellationToken ct = default);
// ── Device detection ──────────────────────────────────────────────────────
///
/// Enumerates connected FTDI USB devices and returns the serial number of the
/// first one found, or if none are connected.
///
string? DetectKLinePort();
// ── Mid-read notifications ────────────────────────────────────────────
///
/// Raised during as soon as the pump identifier
/// string has been read from ROM, before the full read completes.
/// Fires on a background thread; consumers must marshal to the UI thread.
///
event Action? PumpIdentified;
///
/// Raised during when the DFI calibration
/// value has been read from EEPROM. Parameter is the DFI angle in degrees.
/// Fires on a background thread; consumers must marshal to the UI thread.
///
event Action? DfiRead;
// ── Power cycle callbacks ─────────────────────────────────────────────────
///
/// Raised when the service needs the bench to cut power to the pump
/// (e.g. after a DFI write) before reconnecting.
///
event Action? PumpDisconnectRequested;
///
/// Raised after when the service needs
/// the bench to restore power and re-establish the K-Line session.
///
event Action? PumpReconnectRequested;
}
}