using Microsoft.AspNetCore.Localization;

namespace QRRapidoApp.Services
{
    public class RouteDataRequestCultureProvider : RequestCultureProvider
    {
        public override Task<ProviderCultureResult?> DetermineProviderCultureResult(HttpContext httpContext)
        {
            if (httpContext?.Request?.RouteValues == null)
                return Task.FromResult<ProviderCultureResult?>(null);

            var routeValues = httpContext.Request.RouteValues;
            if (!routeValues.ContainsKey("culture"))
                return Task.FromResult<ProviderCultureResult?>(null);

            var culture = routeValues["culture"]?.ToString();
            if (string.IsNullOrEmpty(culture))
                return Task.FromResult<ProviderCultureResult?>(null);

            var supportedCultures = new[] { "pt-BR", "es", "en" };
            if (!supportedCultures.Contains(culture))
                return Task.FromResult<ProviderCultureResult?>(null);

            return Task.FromResult<ProviderCultureResult?>(new ProviderCultureResult(culture));
        }
    }
}