using System.Collections.Generic;
using System.Linq;
namespace HC_APTBS.Models
{
// BIP (Begin of Injection Period) status definitions.
// Applies only to pre-injection PSG5-PI pumps (Type 2 T15xxx / Type 3 T18xxx Ford).
// Null on standard VP44 pumps.
// Data source: KWP RAM read of ADR-S_BIP_HW_UW at address 0x0106 (16-bit unsigned).
///
/// One BIP-STATUS entry: a 16-bit HEX pattern matched against the captured BIP
/// status word, together with the reaction to apply on a match.
///
public class BipStatusDefinition
{
/// Zero-based definition index (BIP-STATUS0 = 0 … BIP-STATUS7 = 7).
public int Index { get; set; }
/// Whether this definition participates in pattern matching.
public bool Enabled { get; set; } = true;
///
/// 16-bit nibble pattern compared against the captured BIP status word.
/// Legacy examples: 0x0000 = BIP OK, 0x000F = no BIP in window,
/// 0x00F0 = no BIP + MV off, 0x0F00 = no MV drive, 0xF00F = deviation too large.
///
public ushort HexPattern { get; set; }
///
/// Reaction code applied on match:
/// 0 = none, 1 = abort (emergency stop), 2 = warning, 3 = log-only.
///
public int Reaction { get; set; } = 0;
///
/// Special-function marker from legacy CFG:
/// 9 = standard handling, 0xB = Ford timing-correction (Type 3 only).
///
public int SpecialFunction { get; set; } = 9;
/// Human-readable description shown on match.
public string Description { get; set; } = string.Empty;
}
///
/// Per-pump BIP status container. Holds up to 8
/// entries (max by legacy convention; not validated).
///
public class PumpBipDefinition
{
/// Ordered BIP status definitions (max 8).
public List Bits { get; set; } = new();
///
/// Returns the first enabled definition whose
/// matches via bitmask semantics: all bits set in the pattern
/// must be set in the captured word ((value & pattern) == pattern).
/// Pattern 0x0000 matches any value (OK state by convention in the legacy CFG).
///
public BipStatusDefinition? Match(ushort value) =>
Bits.FirstOrDefault(b => b.Enabled && (value & b.HexPattern) == b.HexPattern);
}
}