using IRaCIS.Core.Application.Helper;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infra.EFCore;
using IRaCIS.Core.Infrastructure.Extention;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;

namespace IRaCIS.Core.API
{
    public static class LocalizationConfig
    {

        public static async Task UseLocalization(this IApplicationBuilder app,IServiceProvider serviceProvider)
        {
            var supportedCultures = new List<CultureInfo>
            {

                new CultureInfo(StaticData.CultureInfo.en_US),
                new CultureInfo(StaticData.CultureInfo.zh_CN)
            };

            var options = new RequestLocalizationOptions
            {
                //DefaultRequestCulture = new RequestCulture("en-US"),
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures,

                ApplyCurrentCultureToResponseHeaders = true
            };

            //options.RequestCultureProviders.RemoveAt(0);
            //options.RequestCultureProviders.RemoveAt(1);

            app.UseRequestLocalization(options);

            //设置国际化I18n 
            var localizer = serviceProvider.GetRequiredService<IStringLocalizer>();
            I18n.SetLocalizer(localizer);

            //初始化国际化

            var _internationalizationRepository = serviceProvider.GetRequiredService<IRepository<Internationalization>>();

            //查询数据库的数据
            var toJsonList = await _internationalizationRepository.Where(t => t.InternationalizationType == 1).Select(t => new IRCGlobalInfoDTO()
            {
                Code = t.Code,
                Value = t.Value,
                ValueCN = t.ValueCN,
                Description = t.Description
            }).ToListAsync();


           await  InternationalizationHelper.BatchAddJsonKeyValueAsync(toJsonList);


        }
    }
}