using System.Collections.Generic;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using HC_APTBS.Models;
using HC_APTBS.Services;
namespace HC_APTBS.ViewModels
{
///
/// A single result row shown in the results grid for one receive parameter.
///
public sealed partial class ResultRowViewModel : ObservableObject
{
private readonly ILocalizationService _loc;
/// Initialises a new result row with a localization service.
public ResultRowViewModel(ILocalizationService loc) => _loc = loc;
[ObservableProperty] private string _phaseName = string.Empty;
[ObservableProperty] private string _parameterName = string.Empty;
[ObservableProperty] private double _target;
[ObservableProperty] private double _tolerance;
[ObservableProperty] private double _average;
[ObservableProperty] private bool _passed;
/// Localised "PASS" or "FAIL" label.
public string ResultLabel => Passed ? _loc.GetString("Common.Pass") : _loc.GetString("Common.Fail");
}
///
/// ViewModel for the ResultDisplay user control.
///
///
/// Shows a flat table of all phase/parameter combinations with their
/// measured average, target, tolerance, and pass/fail result.
///
///
public sealed partial class ResultDisplayViewModel : ObservableObject
{
private readonly ILocalizationService _loc;
/// Initialises a new result display with a localization service.
public ResultDisplayViewModel(ILocalizationService loc) => _loc = loc;
// ── Properties ────────────────────────────────────────────────────────────
/// Name of the test whose results are displayed.
[ObservableProperty] private string _testName = string.Empty;
/// Overall pass/fail for all displayed results.
[ObservableProperty] private bool _overallPassed;
/// All result rows.
public ObservableCollection Results { get; } = new();
// ── Public API ────────────────────────────────────────────────────────────
///
/// Populates the results table from a completed .
///
public void LoadResults(TestDefinition test)
{
TestName = test.Name;
Results.Clear();
bool allPassed = true;
foreach (var phase in test.Phases)
{
if (!phase.Enabled || phase.Receives == null) continue;
foreach (var tp in phase.Receives)
{
if (tp.Result == null) continue;
allPassed = allPassed && tp.Result.Passed;
Results.Add(new ResultRowViewModel(_loc)
{
PhaseName = phase.Name,
ParameterName = tp.Name,
Target = tp.Value,
Tolerance = tp.Tolerance,
Average = tp.Result.Average,
Passed = tp.Result.Passed
});
}
}
OverallPassed = allPassed && Results.Count > 0;
}
/// Adds or updates a live measurement row during test execution.
public void UpdateLiveValue(string phaseName, string paramName, double currentValue,
double target, double tolerance)
{
foreach (var row in Results)
{
if (row.PhaseName == phaseName && row.ParameterName == paramName)
{
row.Average = currentValue;
return;
}
}
Results.Add(new ResultRowViewModel(_loc)
{
PhaseName = phaseName,
ParameterName = paramName,
Target = target,
Tolerance = tolerance,
Average = currentValue
});
}
///
/// Populates the results table from all completed tests in the pump's test list.
/// Clears existing results first, then appends rows from every test that has results.
///
/// All test definitions for the current pump.
public void LoadAllResults(IReadOnlyList tests)
{
Results.Clear();
bool allPassed = true;
foreach (var test in tests)
{
foreach (var phase in test.Phases)
{
if (!phase.Enabled || phase.Receives == null) continue;
foreach (var tp in phase.Receives)
{
if (tp.Result == null) continue;
allPassed = allPassed && tp.Result.Passed;
Results.Add(new ResultRowViewModel(_loc)
{
PhaseName = phase.Name,
ParameterName = tp.Name,
Target = tp.Value,
Tolerance = tp.Tolerance,
Average = tp.Result.Average,
Passed = tp.Result.Passed
});
}
}
}
TestName = tests.Count > 0 ? _loc.GetString("Result.AllTests") : string.Empty;
OverallPassed = allPassed && Results.Count > 0;
}
/// Clears all results.
public void Clear()
{
Results.Clear();
OverallPassed = false;
}
}
}