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();
///
/// The FTDI serial number of the device that is currently holding an open
/// K-Line session, or when no session is active.
///
string? ConnectedPort { get; }
// ── 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;
// ── Fast immobilizer unlock ───────────────────────────────────────────────
///
/// Attempts a fast immobilizer unlock by sending a KWP custom command
/// over an existing K-Line session. The RAM address byte written by the
/// command is selected by : 1 → 0xA8,
/// 2 → 0xE8. Any other value is rejected and returns
/// .
///
/// Pump unlock variant (1 or 2).
///
/// when the command was acknowledged,
/// on NAK, no active session, or unsupported type.
///
Task TryFastUnlockAsync(int unlockType);
// ── Raw custom KWP packet (developer use) ─────────────────────────────────
///
/// Sends a raw KWP1281 custom packet over the persistent K-Line session and
/// returns the bytes of every response packet. The supplied
/// is the title byte plus body — framing (length + counter + end byte) is added
/// by the lower-level transport.
///
/// Returns an empty list when no session is active or the send fails.
/// Used by the Developer Tools page; never called from production paths.
///
/// Packet bytes excluding length/counter/end framing.
/// Cancellation token.
/// Each response packet's full byte sequence (length…end inclusive).
Task> SendRawCustomAsync(byte[] payload, CancellationToken ct = default);
///
/// Reads bytes from EEPROM starting at
/// over the persistent K-Line session (KWP ReadEeprom command 0x19).
/// Returns an empty list when no session is active or the ECU returns NAK.
/// Used by the Developer Tools page's dumper; max 13 bytes per call.
///
Task> ReadEepromAsync(ushort address, byte count, CancellationToken ct = default);
///
/// Reads bytes from ROM/EEPROM starting at
/// over the persistent K-Line session (KWP ReadRomEeprom command 0x03).
/// Valid range 0x0000–0x9FFF. Returns an empty list when no session is active or
/// the ECU returns NAK. Used by the Developer Tools page's dumper; max 13 bytes per call.
///
Task> ReadRomEepromAsync(ushort address, byte count, CancellationToken ct = default);
// ── BIP status ────────────────────────────────────────────────────────────
///
/// Reads the 16-bit BIP status word from ECU RAM address 0x0106
/// (ADR-S_BIP_HW_UW) over the persistent K-Line session.
/// Returns when no session is active or if the read fails.
///
/// Cancellation token.
Task ReadBipStatusAsync(CancellationToken ct = default);
///
/// Raised after a successful call with the
/// raw 16-bit BIP status word. Fires on a background thread;
/// consumers must marshal to the UI thread.
///
event Action? BipStatusRead;
// ── 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;
}
}