using System;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HC_APTBS.Models;
using HC_APTBS.Services;
namespace HC_APTBS.ViewModels
{
///
/// Represents a single phase card within a test section.
/// Displays the phase name, enable/disable toggle, operation values,
/// and graphic result indicators.
///
public sealed partial class PhaseCardViewModel : ObservableObject
{
private readonly ILocalizationService _loc;
/// Initialises a new phase card with a localization service.
public PhaseCardViewModel(ILocalizationService loc) => _loc = loc;
// ── Identity ──────────────────────────────────────────────────────────────
/// Display name of the phase (e.g. "1 - S_001").
[ObservableProperty] private string _name = string.Empty;
/// True when failure in this phase halts the entire test sequence.
[ObservableProperty] private bool _isCritical;
// ── Enable/disable ────────────────────────────────────────────────────────
///
/// Whether this phase is included in the test run.
/// Writing this property also updates the underlying
/// and notifies the parent to recalculate its
/// AllPhasesChecked state.
///
[ObservableProperty] private bool _isEnabled = true;
// ── Execution state ───────────────────────────────────────────────────────
/// True while this phase is actively executing.
[ObservableProperty] private bool _isActive;
/// True when the phase completed and passed all criteria.
[ObservableProperty] private bool _isPassed;
/// True when the phase completed but failed one or more criteria.
[ObservableProperty] private bool _isFailed;
/// Short result label shown in the card (e.g. "PASS", "FAIL", "-").
[ObservableProperty] private string _resultText = string.Empty;
// ── Operation values visibility ───────────────────────────────────────────
///
/// Controls visibility of the operation values section (RPM, ME, FBKW).
/// Bound from .
///
[ObservableProperty] private bool _showOperationValues;
// ── Collections ───────────────────────────────────────────────────────────
/// Send parameters displayed in the card (RPM, ME, FBKW, etc.).
public ObservableCollection OperationValues { get; } = new();
/// Readiness conditions displayed in the card (temperature, etc.).
public ObservableCollection ReadyValues { get; } = new();
/// Graphic result indicators, one per receive parameter.
public ObservableCollection ResultIndicators { get; } = new();
// ── Back-references ───────────────────────────────────────────────────────
/// Back-reference to the model for writing enabled state changes.
internal PhaseDefinition? Source { get; set; }
///
/// Callback invoked when changes, so the parent
/// can recalculate AllPhasesChecked.
///
internal Action? EnabledChanged { get; set; }
// ── Change handlers ───────────────────────────────────────────────────────
partial void OnIsEnabledChanged(bool value)
{
// Write back to the model.
if (Source != null)
Source.Enabled = value;
ResultText = value ? "\u2013" : _loc.GetString("Common.Disabled");
// Notify parent.
EnabledChanged?.Invoke(this);
}
// ── Commands ──────────────────────────────────────────────────────────────
/// Inverts . Bound to the card's click gesture.
[RelayCommand]
private void ToggleEnabled() => IsEnabled = !IsEnabled;
// ── Public API ────────────────────────────────────────────────────────────
/// Resets execution state for a new test run.
public void Reset()
{
IsActive = false;
IsPassed = false;
IsFailed = false;
ResultText = IsEnabled ? "\u2013" : _loc.GetString("Common.Disabled");
foreach (var indicator in ResultIndicators)
indicator.Reset();
}
}
}