using JSMR.Domain.Enums;
using NTextCat;
using System.Reflection;

namespace JSMR.Infrastructure.Common.Languages;

public class LanguageIdentifier : ILanguageIdentifier
{
    private readonly string[] _languages =
    [
        "eng",
        "jpn",
        "kor",
        "zho"
    ];

    private readonly RankedLanguageIdentifier _identifier;

    public LanguageIdentifier()
    {
        RankedLanguageIdentifierFactory factory = new();

        using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("JSMR.Infrastructure.Common.Languages.Language.xml");
        _identifier = factory.Load(stream);
    }

    public Language GetLanguage(string text)
    {
        if (string.IsNullOrWhiteSpace(text))
            return Language.Unknown;

        var rankedLanguages = _identifier.Identify(text).Where(x => _languages.Contains(x.Item1.Iso639_3));
        var identifiedLanguage = rankedLanguages.OrderBy(x => x.Item2).FirstOrDefault();

        if (identifiedLanguage == null)
            return Language.Unknown;

        return identifiedLanguage.Item1.Iso639_3 switch
        {
            "jpn" => Language.Japanese,
            "eng" => Language.English,
            "kor" => Language.Korean,
            "zho" => Language.ChineseTraditional,// Or ChineseSimplified?
            _ => Language.Unknown,
        };
    }
}