using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;

namespace LSFE.MobApp.Helpers;

/// <summary>
/// Centralized helper for doctor status styling and updates
/// </summary>
public static class DoctorStatusHelper
{
    #region Status Text
    public static string GetStatusText(byte status)
    {
        return status switch
        {
            0 => "Pending",
            1 => "Started",
            2 => "Completed",
            3 => "Reschedule",
            4 => "Missed",
            _ => "Unknown"
        };
    }
    #endregion

    #region Status Colors
    public static Color GetStatusBackgroundColor(byte status)
    {
        return status switch
        {
            0 => Color.FromArgb("#FEF3C7"), // Pending - soft amber
            1 => Color.FromArgb("#D1FAE5"), // Started - mint green
            2 => Color.FromArgb("#DBEAFE"), // Completed - soft blue
            3 => Color.FromArgb("#FDE68A"), // Rescheduled - soft yellow
            4 => Color.FromArgb("#FECACA"), // Missed - gentle red
            _ => Color.FromArgb("#E5E7EB")  // Default - gray
        };
    }

    public static Color GetStatusTextColor(byte status)
    {
        return status switch
        {
            0 => Color.FromArgb("#92400E"), // Pending - amber dark
            1 => Color.FromArgb("#065F46"), // Started - deep green
            2 => Color.FromArgb("#1E40AF"), // Completed - deep blue
            3 => Color.FromArgb("#92400E"), // Rescheduled - amber dark
            4 => Color.FromArgb("#B91C1C"), // Missed - deep red
            _ => Color.FromArgb("#374151")  // Default - charcoal
        };
    }
    #endregion

    #region Status Update Broadcasting
    /// <summary>
    /// Broadcast status update to all listening ViewModels
    /// </summary>
    public static void BroadcastStatusUpdate(int doctorId)
    {
        WeakReferenceMessenger.Default.Send(new DoctorStatusUpdatedMessage(doctorId));
    }

    /// <summary>
    /// Register a ViewModel to listen for status updates
    /// </summary>
    public static void RegisterForStatusUpdates(object recipient, Action<int> onStatusUpdate)
    {
        WeakReferenceMessenger.Default.Register<DoctorStatusUpdatedMessage>(recipient, (r, message) =>
        {
            onStatusUpdate(message.Value);
        });
    }

    /// <summary>
    /// Unregister a ViewModel from status updates
    /// </summary>
    public static void UnregisterFromStatusUpdates(object recipient)
    {
        WeakReferenceMessenger.Default.Unregister<DoctorStatusUpdatedMessage>(recipient);
    }
    #endregion
}

/// <summary>
/// Message for broadcasting doctor status updates
/// </summary>
public class DoctorStatusUpdatedMessage : ValueChangedMessage<int>
{
    public DoctorStatusUpdatedMessage(int doctorId) : base(doctorId) { }
}