using System.Collections.Generic;
using Peak.Can.Basic;
using TPCANHandle = System.UInt16;
namespace HC_APTBS.Models
{
///
/// Describes a diesel injection pump model supported by the test bench.
/// This is the configuration-time definition loaded from the pump database.
/// Runtime state (current RPM, temperature, test progress) lives in the ViewModel.
///
public class PumpDefinition
{
// ── Identity ──────────────────────────────────────────────────────────────
/// Pump identifier code (e.g. "VP44-0460424", loaded from K-Line EEPROM).
public string Id { get; set; } = string.Empty;
/// Pump model name shown in the UI.
public string Model { get; set; } = string.Empty;
/// Serial number stamped on the pump body.
public string SerialNumber { get; set; } = string.Empty;
/// Injector nozzle specification string (parsed from ECU text).
public string Injector { get; set; } = string.Empty;
/// High-pressure tube specification.
public string Tube { get; set; } = string.Empty;
/// Solenoid valve specification.
public string Valve { get; set; } = string.Empty;
/// Electrical supply tension.
public string Tension { get; set; } = string.Empty;
/// Free-text info line from the pump database.
public string Info { get; set; } = string.Empty;
/// Raw ECU text returned by ReadEcuInfo over K-Line.
public string EcuText { get; set; } = string.Empty;
/// Lock-angle (shaft timing reference) in degrees.
public string Chaveta { get; set; } = string.Empty;
///
/// Alternative K-Line pump-IDs that should resolve to this canonical pump.
/// Used when the ECU reports a Bosch number (e.g. 1093423001) that differs from
/// this pump's canonical (e.g. 0470504027). Loaded from the
/// per-pump <Aliases><KlineId> entries in pumps.xml.
///
public List KlineAliases { get; set; } = new();
///
/// Alternative ModelReference strings (12-char ECU prefix, e.g. ME190297C150)
/// that should resolve to this canonical pump. Loaded from the per-pump
/// <Aliases><ModelRef> entries in pumps.xml.
///
public List ModelRefAliases { get; set; } = new();
// ── Physical parameters ───────────────────────────────────────────────────
///
/// Injection timing lock angle (degrees) used during the Lock Angle test phase.
///
public double LockAngle { get; set; }
/// Measured lock angle result, populated after the Lock Angle phase completes.
public double LockAngleResult { get; set; }
/// True if this pump model has a pilot/pre-injection solenoid.
public bool HasPreInjection { get; set; }
/// True for 4-cylinder pump configurations; false for 6-cylinder.
public bool Is4Cylinder { get; set; } = true;
/// Unlock protocol variant (0 = none, 1 = type 1, 2 = type 2).
public int UnlockType { get; set; }
///
/// Pump shaft rotation direction.
/// See and .
///
public string Rotation { get; set; } = RotationDirection.RightName;
// ── CAN configuration ─────────────────────────────────────────────────────
/// PCAN channel handle used to communicate with this pump.
public TPCANHandle CanChannel { get; set; } = PCANBasic.PCAN_USBBUS1;
/// CAN baudrate for this pump (most VP44 units use 500 kbps).
public TPCANBaudrate CanBaudrate { get; set; } = TPCANBaudrate.PCAN_BAUD_500K;
///
/// Parameters specific to this pump, keyed by name for quick lookup.
/// These are separate from the bench parameters.
///
public Dictionary ParametersByName { get; set; } = new();
/// Pump CAN parameters grouped by message ID for frame decoding.
public Dictionary> ParametersById { get; set; } = new();
// ── K-Line data ───────────────────────────────────────────────────────────
///
/// K-Line / KWP2000 data read from the pump ECU EEPROM.
/// Keys match constants.
///
public Dictionary KlineInfo { get; set; } = new();
///
/// Pump hardware version used to select the correct KWP protocol variant.
/// 0 = V1 original, 1 = V2, 2 = V3/V4.
///
public int KwpVersion { get; set; }
// ── Tests ─────────────────────────────────────────────────────────────────
/// Ordered list of test procedures applicable to this pump model.
public List Tests { get; set; } = new();
// ── BIP (pre-injection only) ──────────────────────────────────────────────
///
/// BIP (Begin of Injection Period) status definitions. Non-null only on
/// pre-injection PSG5-PI pumps (Type 2 T15xxx / Type 3 T18xxx Ford). Null on
/// standard VP44 pumps. See .
///
public PumpBipDefinition? BipStatus { get; set; }
// ── Runtime live values ────────────────────────────────────────────────────
/// Current fuel quantity feedback value (me) from the pump ECU.
public double ValueMe { get; set; }
/// Current FBKW (Füllungsbeiwert / fill factor) value from the pump ECU.
public double ValueFbkw { get; set; }
/// Current pre-injection quantity value from the pump ECU.
public double ValuePreIn { get; set; }
// ── Test helpers ──────────────────────────────────────────────────────────
///
/// Merges phases from a WL-type test into the first F-type test in the list,
/// then renames it to WL. This matches the legacy pump database convention
/// where WL extends the base F test with warm-up phases.
///
internal void CombineTestWL(TestDefinition wlTest)
{
foreach (var existing in Tests)
{
if (existing.Name == TestType.F)
{
existing.Phases.AddRange(wlTest.Phases);
existing.Name = TestType.Wl;
return;
}
}
}
// ── Helpers ───────────────────────────────────────────────────────────────
///
/// Parses the injector, tube, valve, and tension fields from the raw ECU text string.
/// The ECU text format is: "(injector) tube valve tension"
///
public void ParseEcuText()
{
if (string.IsNullOrEmpty(EcuText)) return;
int closeParenPos = EcuText.IndexOf(')') + 1;
Injector = EcuText[..closeParenPos].Trim();
var rest = EcuText[closeParenPos..];
var parts = rest.Split(new[] { " " }, System.StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 0) Tube = parts[0].Trim();
if (parts.Length > 1) Valve = parts[1].Trim();
if (parts.Length > 2) Tension = parts[2].Trim();
}
}
}