using System.Globalization;
using System.Net.Http.Json;
using Microsoft.Extensions.Localization;

namespace WebClientBase.Client.Localization;

public class JsonStringLocalizer : IStringLocalizer
{
    private readonly LocalizationCache _cache;
    private readonly string _resourceName;

    public JsonStringLocalizer(LocalizationCache cache, string resourceName)
    {
        _cache = cache;
        _resourceName = resourceName;
    }

    // This constructor style is used by the Factory (if we update factory)
    public JsonStringLocalizer(LocalizationCache cache) 
    {
         _cache = cache;
         _resourceName = "Shared";
    }

    public LocalizedString this[string name]
    {
        get
        {
            var value = GetString(name);
            return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
        }
    }

    public LocalizedString this[string name, params object[] arguments]
    {
        get
        {
            var format = GetString(name);
            var value = string.Format(format ?? name, arguments);
            return new LocalizedString(name, value, resourceNotFound: format == null);
        }
    }

    public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
    {
        // Not fully supported by simple cache
        return Enumerable.Empty<LocalizedString>();
    }

    private string? GetString(string name)
    {
        return _cache.GetString(name);
    }
}
