using YMhut.Box.Core.Tools;

namespace YMhut.Box.WinUI;

internal static class ToolText
{
    internal static string Name(IToolModule module) => Name(module.Id, module.Metadata.Name);

    internal static string Name(string id, string fallback = "")
    {
        return AppLocalizer.ToolName(id, fallback);
    }

    internal static string NameIn(string id, bool english) => AppLocalizer.ToolNameIn(id, english);

    internal static string Description(IToolModule module)
    {
        return module switch
        {
            ExternalToolModule external => external.Tool.Description,
            BuiltinReferenceToolModule builtin => builtin.Definition.Description,
            _ => AppLocalizer.ToolDescription(module)
        };
    }

    internal static string CategoryLabel(ToolCategory category) => AppLocalizer.CategoryLabel(category);

    internal static string CategoryHint(ToolCategory category) => AppLocalizer.CategoryHint(category);

    internal static string CategoryGlyph(ToolCategory category)
    {
        return category switch
        {
            ToolCategory.Plugin => "\uECAA",
            ToolCategory.Dev => "\uE943",
            ToolCategory.Network => "\uE774",
            ToolCategory.Security => "\uE72E",
            ToolCategory.Data => "\uE9D9",
            ToolCategory.Calculator => "\uE8EF",
            ToolCategory.Text => "\uE8D2",
            ToolCategory.Image => "\uEB9F",
            ToolCategory.Design => "\uE790",
            ToolCategory.Life => "\uE753",
            ToolCategory.System => "\uE950",
            _ => "\uE80A"
        };
    }

    internal static bool Matches(IToolModule module, string query)
    {
        if (string.IsNullOrWhiteSpace(query))
        {
            return true;
        }

        var normalized = query.Trim();
        return module.Id.Contains(normalized, StringComparison.OrdinalIgnoreCase)
            || module.Metadata.Name.Contains(normalized, StringComparison.OrdinalIgnoreCase)
            || module.Metadata.Description.Contains(normalized, StringComparison.OrdinalIgnoreCase)
            || NameIn(module.Id, english: false).Contains(normalized, StringComparison.OrdinalIgnoreCase)
            || NameIn(module.Id, english: true).Contains(normalized, StringComparison.OrdinalIgnoreCase)
            || Description(module).Contains(normalized, StringComparison.OrdinalIgnoreCase)
            || CategoryLabel(module.Metadata.Category).Contains(normalized, StringComparison.OrdinalIgnoreCase)
            || module.Metadata.Keywords.Any(keyword => keyword.Contains(normalized, StringComparison.OrdinalIgnoreCase));
    }
}
