using System.Windows.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using PSLauncher.Core.Health;
using PSLauncher.Models;

namespace PSLauncher.App.ViewModels;

/// <summary>
/// Représente un indicateur visuel (pill colorée) dans le bandeau de santé
/// système. Mis à jour périodiquement par <see cref="MainViewModel"/> à
/// partir du résultat de <see cref="ISystemHealthService.RunCheckAsync"/>.
/// </summary>
public sealed partial class HealthIndicatorViewModel : ObservableObject
{
    public HealthCheckEntry Entry { get; }

    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(StatusBrush))]
    [NotifyPropertyChangedFor(nameof(BorderBrush))]
    [NotifyPropertyChangedFor(nameof(IconForeground))]
    [NotifyPropertyChangedFor(nameof(Tooltip))]
    private HealthSeverity _severity = HealthSeverity.Unknown;

    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(Tooltip))]
    private string _detail = "Pas encore vérifié";

    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(Tooltip))]
    private DateTime _lastCheckedUtc = DateTime.MinValue;

    public string Name => Entry.Name;
    public string Icon => Entry.Icon;

    public Brush StatusBrush => Severity switch
    {
        // Pill background : un peu transparent pour que le bandeau reste discret
        HealthSeverity.Ok      => new SolidColorBrush(Color.FromArgb(0x40, 0x16, 0xA3, 0x4A)), // vert
        HealthSeverity.Warning => new SolidColorBrush(Color.FromArgb(0x50, 0xF5, 0x9E, 0x0B)), // orange
        HealthSeverity.Error   => new SolidColorBrush(Color.FromArgb(0x50, 0xEF, 0x44, 0x44)), // rouge
        _                      => new SolidColorBrush(Color.FromArgb(0x30, 0x80, 0x80, 0x90)), // gris
    };

    public Brush BorderBrush => Severity switch
    {
        HealthSeverity.Ok      => new SolidColorBrush(Color.FromRgb(0x16, 0xA3, 0x4A)),
        HealthSeverity.Warning => new SolidColorBrush(Color.FromRgb(0xF5, 0x9E, 0x0B)),
        HealthSeverity.Error   => new SolidColorBrush(Color.FromRgb(0xEF, 0x44, 0x44)),
        _                      => new SolidColorBrush(Color.FromRgb(0x60, 0x60, 0x70)),
    };

    public Brush IconForeground => Severity switch
    {
        HealthSeverity.Ok      => new SolidColorBrush(Color.FromRgb(0x4A, 0xDE, 0x80)),
        HealthSeverity.Warning => new SolidColorBrush(Color.FromRgb(0xFB, 0xBF, 0x24)),
        HealthSeverity.Error   => new SolidColorBrush(Color.FromRgb(0xF8, 0x71, 0x71)),
        _                      => new SolidColorBrush(Color.FromRgb(0xA0, 0xA0, 0xA8)),
    };

    public string Tooltip
    {
        get
        {
            var status = Severity switch
            {
                HealthSeverity.Ok      => "✓ OK",
                HealthSeverity.Warning => "⚠ Limitation détectée",
                HealthSeverity.Error   => "⛔ Problème",
                _                      => "❓ Non vérifié",
            };
            var ts = LastCheckedUtc == DateTime.MinValue
                ? ""
                : $"\n\nDernière vérif : {LastCheckedUtc.ToLocalTime():HH:mm:ss}";
            var kindLabel = Entry.Kind?.Equals("ping", StringComparison.OrdinalIgnoreCase) == true
                ? $"Ping {Entry.Target}"
                : $"Processus {Entry.Target}";
            return $"{Entry.Icon}  {Entry.Name} — {status}\n{kindLabel}\n\n{Detail}{ts}";
        }
    }

    public HealthIndicatorViewModel(HealthCheckEntry entry)
    {
        Entry = entry;
    }

    public void Apply(HealthResult result)
    {
        Severity = result.Severity;
        Detail = result.Detail;
        LastCheckedUtc = DateTime.UtcNow;
    }
}
