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

namespace EggymonLandingPage.Client.Localization;

/// <summary>
/// EN: JSON-based string localizer for Blazor WASM.
/// VI: String localizer dựa trên JSON cho Blazor WASM.
/// </summary>
public class JsonStringLocalizer : IStringLocalizer
{
    private readonly LocalizationCache _cache;
    private readonly string _resourceName;

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

    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)
    {
        return Enumerable.Empty<LocalizedString>();
    }

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