using System;
using System.Threading;
using System.Threading.Tasks;
using HC_APTBS.Models;
namespace HC_APTBS.Services
{
///
/// Orchestrates the test bench: relay control, temperature PID, RPM control,
/// and test sequence execution.
///
public interface IBenchService
{
// ── Events ────────────────────────────────────────────────────────────────
/// Raised when a test sequence starts.
event Action? TestStarted;
///
/// Raised when a test sequence completes (normally or via stop).
/// interrupted is true if stopped early; success is the overall pass/fail result.
///
event Action? TestFinished; //interrupted,success
/// Raised at each phase transition with the phase name.
event Action? PhaseChanged; //phaseName
/// Raised with verbose status text during test execution (e.g. "Conditioning… 45s").
event Action? VerboseMessage; //message
/// Raised when a PSG sync-pulse error halts a SVME test.
event Action? PsgSyncError;
///
/// Raised after a phase finishes with its name and pass/fail result.
///
event Action? PhaseCompleted; // phaseName,passed
///
/// Raised when a safety check triggers an emergency stop. The bench motor
/// and pump parameters are already stopped when this fires.
/// Fires on a background thread — consumers must marshal to the UI thread.
///
event Action? EmergencyStopTriggered; //reason
// ── Active pump ───────────────────────────────────────────────────────────
///
/// Registers the currently selected pump so that pump-specific CAN parameters
/// (me, FBKW, mepi, RPM, Temp, Tein, Status, Empf3) can be read and written
/// through and .
///
void SetActivePump(PumpDefinition? pump);
// ── Bench parameter access ────────────────────────────────────────────────
///
/// Returns the current live value of a bench parameter by name.
///
/// Bench parameter name (see ).
double ReadBenchParameter(string parameterName);
///
/// Returns the current live value of a pump parameter by name.
///
/// Pump parameter name (see ).
double ReadPumpParameter(string parameterName);
///
/// Returns the current live value of a parameter by name.
/// Looks in the active pump's parameters first, then falls back to bench parameters.
/// Use or when the source is known.
///
/// Parameter name (see / ).
double ReadParameter(string parameterName);
/// Sets a pump/bench parameter value in the local parameter map.
/// Parameter name.
/// New value in engineering units.
void SetParameter(string parameterName, double value);
/// Flushes all pending parameter changes to the CAN bus.
/// CAN message ID to transmit.
void SendParameters(uint messageId);
// ── Pump control sender ───────────────────────────────────────────────────
///
/// Sets the target value for a pump control parameter (me, FBKW, mepi).
/// FBKW is written immediately; ME and PreIn are slew-rate filtered by the periodic sender.
///
void SetPumpControlValue(string parameterName, double targetValue);
/// Starts the periodic CAN sender that transmits pump control parameters.
void StartPumpSender();
/// Stops the periodic pump control CAN sender.
void StopPumpSender();
/// Starts the periodic MemoryRequest sender that polls the pump ECU for Tein data.
void StartMemoryRequestSender();
/// Stops the periodic MemoryRequest sender.
void StopMemoryRequestSender();
/// Starts the periodic ElectronicMsg keepalive sender (1-second interval).
void StartElectronicMsgSender();
/// Stops the periodic ElectronicMsg keepalive sender.
void StopElectronicMsgSender();
///
/// Raised when a pump control value is set (e.g. during test execution),
/// so the ViewModel can update slider positions. Arguments: parameterName, value.
///
event Action? PumpControlValueSet;
// ── RPM control ───────────────────────────────────────────────────────────
///
/// Commands the bench motor to the specified RPM by computing and applying
/// the corresponding voltage from the RPM-to-voltage lookup table.
/// Used by automated test execution for direct voltage jumps.
///
void SetRpm(double rpm);
///
/// Starts the PID-based RPM ramp controller to smoothly reach the target RPM.
/// Sends an initial voltage jump from the RPM-voltage lookup table, then hands
/// control to the PID loop after an approach delay.
/// Used for interactive (manual) bench control.
///
/// Desired RPM setpoint.
void StartRpmPid(double targetRpm);
///
/// Stops the PID RPM controller, sends 0 V to the motor.
///
void StopRpmPid();
/// The last RPM target that was commanded via or .
double LastTargetRpm { get; }
/// The last voltage value sent to the motor CAN parameter.
double LastCommandVoltage { get; }
/// Raised after a voltage command is sent to the motor (from or the PID loop).
event Action? RpmCommandSent;
// ── Temperature control ───────────────────────────────────────────────────
///
/// Sets the temperature PID setpoint.
///
/// Target temperature in °C.
/// Acceptable deviation in °C.
///
/// True if the temperature check should be ignored (e.g. sensor fault or
/// the DefaultIgnoreTin setting is active).
///
bool SetTemperatureSetpoint(double setpointCelsius, double toleranceCelsius);
// ── Relay control ─────────────────────────────────────────────────────────
/// Energises or de-energises the named relay.
/// Relay name (see ).
/// True = ON, false = OFF.
void SetRelay(string relayName, bool state);
///
/// Transmits the current relay bitmask once. Called on CAN connect so the
/// bench controller receives the initial relay state immediately.
///
void SendInitialRelayState();
/// Starts the periodic relay bitmask sender (~21 ms cycle).
void StartRelaySender();
/// Stops the periodic relay bitmask sender.
void StopRelaySender();
// ── Test execution ────────────────────────────────────────────────────────
///
/// Starts the full test sequence for the given pump in a background task.
///
/// Pump definition containing tests and CAN parameters.
/// Cancellation token — cancel to trigger a controlled stop.
Task RunTestsAsync(PumpDefinition pump, CancellationToken ct);
/// Requests a controlled stop of the currently running test sequence.
void StopTests();
// ── Lock angle ────────────────────────────────────────────────────────────
///
/// Raised during test execution to signal that the Lock Angle measurement phase
/// is ready. The ViewModel should display the lock angle acquisition UI.
///
event Action? LockAngleFaseReady;
///
/// Raised when the PSG encoder synchronisation phase begins.
///
event Action? PsgModeFaseReady;
// ── DFI auto-adjust ───────────────────────────────────────────────────────
/// Returns true if auto-DFI adjustment is currently enabled by the operator.
bool IsAutoDfiEnabled { get; }
///
/// Writes a new DFI value to the pump ECU as part of the auto-adjust loop.
///
void SetDfi(double dfi);
// ── Tolerance display ─────────────────────────────────────────────────────
///
/// Raised so the chart view can draw tolerance bands for the specified parameter.
///
event Action? ToleranceUpdated; //parameterName, value, tolerance
///
/// Raised for each individual measurement sample collected during a test phase.
/// Fires on a background thread — consumers must marshal to the UI thread.
///
event Action? MeasurementSampled; //parameterName, value
}
}