using System.Globalization;
using Avalonia.Data.Converters;
using Avalonia.Media;

namespace DodoSSH.Client.App.Views;

/// <summary>
/// Turns a host card's <c>MonogramHue</c> string into the background or foreground brush the mock pairs it
/// with.
/// </summary>
/// <remarks>
/// <para>
/// The four pairs are the design's own and are not in <c>Palette.axaml</c>: that file is another wave's, and
/// none of the shared palette's existing keys is this hue ramp — a monogram's colour is a property of the
/// four-way hash in <c>HostRowViewModel.MonogramHue</c>, not a fact the rest of the theme has any use for.
/// Held here, beside the one screen that reads it, rather than invented as four new shared resources for a
/// single card.
/// </para>
/// <para>
/// Two instances rather than one converter taking a "which half" parameter, so the markup names each with a
/// <c>StaticResource</c>-shaped reference instead of a converter parameter that means nothing without
/// reading this file.
/// </para>
/// </remarks>
internal sealed class MonogramBrushConverter(bool background) : IValueConverter
{
    /// <summary>The fill behind the two letters.</summary>
    internal static MonogramBrushConverter Background { get; } = new(background: true);

    /// <summary>The colour of the two letters themselves.</summary>
    internal static MonogramBrushConverter Foreground { get; } = new(background: false);

    /// <summary>violet, green, amber, gray — the four pairs the mock assigns a monogram.</summary>
    private static readonly Dictionary<string, (string Background, string Foreground)> Pairs = new(
        StringComparer.Ordinal)
    {
        ["violet"] = ("#241C4F", "#A78BFA"),
        ["green"] = ("#103A2F", "#34D399"),
        ["amber"] = ("#423211", "#F5B942"),
        ["gray"] = ("#1E1E2C", "#9C9EB4"),
    };

    /// <inheritdoc />
    public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        var pair = value is string hue && Pairs.TryGetValue(hue, out var found) ? found : Pairs["gray"];

        return new SolidColorBrush(Color.Parse(background ? pair.Background : pair.Foreground));
    }

    /// <inheritdoc />
    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
        throw new NotSupportedException("The monogram hue is read-only.");
}
