using System;

namespace HellionChat._Helpers;

// UI-7 pure decision helper: builds the display string for a sender's name +
// world per the user's NameFormMode / WorldSuffixMode. Dalamud-free so the
// Build Suite can cover every combination; SenderNameDisplay feeds it the name
// and world it pulled from the PlayerPayload.
// TEST-MIRROR: ../../../Hellion Build test/Ui/SenderNameFormatterTests.cs
public static class SenderNameFormatter
{
    public static string Format(
        string fullName,
        string worldName,
        bool isHomeWorld,
        NameFormMode formMode,
        WorldSuffixMode suffixMode
    )
    {
        var name = FormatName(fullName, formMode);

        var showWorld = suffixMode switch
        {
            WorldSuffixMode.Never => false,
            WorldSuffixMode.Always => true,
            WorldSuffixMode.OtherWorldOnly => !isHomeWorld,
            _ => !isHomeWorld,
        };

        return showWorld && !string.IsNullOrEmpty(worldName) ? $"{name}@{worldName}" : name;
    }

    private static string FormatName(string fullName, NameFormMode mode)
    {
        if (mode == NameFormMode.Full)
            return fullName;

        // FFXIV character names are two words. Anything else (one word, odd
        // spacing) falls back to the full name rather than risking an empty or
        // misleading render.
        var parts = fullName.Split(' ', StringSplitOptions.RemoveEmptyEntries);
        if (parts.Length < 2)
            return fullName;

        return mode switch
        {
            NameFormMode.FirstNameOnly => parts[0],
            NameFormMode.Initials => $"{parts[0][0]}. {parts[^1][0]}.",
            _ => fullName,
        };
    }
}
