using System;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HC_APTBS.Services;
namespace HC_APTBS.ViewModels
{
///
/// ViewModel for the bench-page interlock banner.
/// Surfaces two soft safety warnings inline (dismissible), matching the
/// Bench page guideline in docs/ui-structure.md ยง2:
///
/// - Oil pump off while RPM > threshold.
/// - RPM above configured AppSettings.MaxRpm safety limit.
///
///
public sealed partial class InterlockBannerViewModel : ObservableObject
{
private const double OilPumpRpmThreshold = 300.0;
private readonly IConfigurationService _config;
/// Text shown in the banner. Empty string hides the banner.
[ObservableProperty] private string _message = string.Empty;
/// True when the banner is currently shown.
[ObservableProperty] private bool _isVisible;
/// True when the banner is showing a critical warning (red background).
[ObservableProperty] private bool _isCritical;
private bool _dismissed;
private int _dismissedFor; // Bit flags: 1=oil, 2=rpm
/// Configuration service (source of MaxRpm).
public InterlockBannerViewModel(IConfigurationService configService)
{
_config = configService;
}
///
/// Recomputes the banner message from live bench state.
/// Called from the page's refresh tick handler.
///
/// Current bench motor RPM.
/// True when the oil pump relay is energised.
public void Update(double benchRpm, bool isOilPumpOn)
{
int maxRpm = Math.Max(1, _config.Settings.MaxRpm);
bool oilWarn = !isOilPumpOn && benchRpm > OilPumpRpmThreshold;
bool rpmWarn = benchRpm > maxRpm;
int activeFlags = (oilWarn ? 1 : 0) | (rpmWarn ? 2 : 0);
// Reset dismissal when the condition actually clears.
if (activeFlags == 0)
{
_dismissed = false;
_dismissedFor = 0;
Message = string.Empty;
IsCritical = false;
IsVisible = false;
return;
}
// If the operator already dismissed this exact condition set, stay hidden.
if (_dismissed && _dismissedFor == activeFlags)
return;
_dismissed = false;
if (rpmWarn)
{
Message = $"RPM {benchRpm:F0} exceeds safety limit ({maxRpm}).";
IsCritical = true;
}
else
{
Message = $"Oil pump is OFF while bench is running at {benchRpm:F0} RPM.";
IsCritical = false;
}
IsVisible = true;
_dismissedFor = activeFlags;
}
/// Hides the banner until the underlying condition changes.
[RelayCommand]
private void Dismiss()
{
_dismissed = true;
IsVisible = false;
}
}
}