@using System.Text.RegularExpressions

<div title="@SenderEmail" class="justify-content-center align-middle" style="padding-top:10px;font-size:18px;color:white;text-align:center;border-radius:30px;width: 50px;height: 50px;background-color:@SenderNameColor;">@SenderNameLetters</div>

@code {
    /// <summary>
    /// The name of the sender.
    /// </summary>
    [Parameter]
    public string SenderName { get; set; } = string.Empty;

    /// <summary>
    /// The email of the sender.
    /// </summary>
    [Parameter]
    public string SenderEmail { get; set; } = string.Empty;

    private string SenderNameLetters { get; set; } = string.Empty;
    private string SenderNameColor { get; set; } = "#666";

    /// <summary>
    /// Mappping of alphabet letters to HSL colors.
    /// </summary>
    private static readonly Dictionary<string, string> AlphabetColors = new Dictionary<string, string>
    {
        { "A", "hsl(175, 50%, 50%)" },
        { "B", "hsl(234, 50%, 50%)" },
        { "C", "hsl(278, 50%, 50%)" },
        { "D", "hsl(191, 50%, 50%)" },
        { "E", "hsl(215, 50%, 50%)" },
        { "F", "hsl(315, 50%, 50%)" },
        { "G", "hsl(247, 50%, 50%)" },
        { "H", "hsl(259, 50%, 50%)" },
        { "I", "hsl(289, 50%, 50%)" },
        { "J", "hsl(206, 50%, 50%)" },
        { "K", "hsl(124, 50%, 50%)" },
        { "L", "hsl(129, 50%, 50%)" },
        { "M", "hsl(69, 50%, 50%)" },
        { "N", "hsl(38, 50%, 50%)" },
        { "O", "hsl(352, 50%, 50%)" },
        { "P", "hsl(311, 50%, 50%)" },
        { "Q", "hsl(332, 50%, 50%)" },
        { "R", "hsl(344, 50%, 50%)" },
        { "S", "hsl(357, 50%, 50%)" },
        { "T", "hsl(23, 50%, 50%)" },
        { "U", "hsl(16, 50%, 50%)" },
        { "V", "hsl(304, 50%, 50%)" },
        { "W", "hsl(300, 50%, 50%)" },
        { "X", "hsl(332, 50%, 50%)" },
        { "Y", "hsl(48, 50%, 50%)" },
        { "Z", "hsl(9, 50%, 50%)" }
    };

    /// <inheritdoc />
    protected override void OnParametersSet()
    {
        GenerateSenderInitials();
        GenerateSenderColor();
    }

    /// <summary>
    /// Extract the initials of the sender.
    /// </summary>
    private void GenerateSenderInitials()
    {
        string senderName = Regex.Replace(SenderName, "[^a-zA-Z ]", "", RegexOptions.NonBacktracking);

        SenderNameLetters = string.Concat(senderName
            .Split(' ')
            .Select(n => n.Length > 0 ? n[0].ToString() : "")
            );
        SenderNameLetters = SenderNameLetters.Substring(0, Math.Min(2, SenderNameLetters.Length)).ToUpper();

        if (string.IsNullOrEmpty(SenderNameLetters))
        {
            SenderNameLetters = "?";
        }
    }

    /// <summary>
    /// Pick a unique color based on the first letter of the sender name.
    /// </summary>
    private void GenerateSenderColor()
    {
        string firstLetter = SenderNameLetters.Substring(0, 1);
        SenderNameColor = AlphabetColors.TryGetValue(firstLetter, out var color) ? color : "#666";
    }
}
