using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static IRaCIS.Core.Application.Service.Common.SystemMonitor;

namespace IRaCIS.Core.Application.Helper
{
    public static class InternationalizationHelper
    {
        public static string JsonFileFolder = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources);

        private static void VerifyFolder()
        {
            if (!Directory.Exists(JsonFileFolder) ||
               !Directory.GetFiles(JsonFileFolder).Any(filePath => Path.GetExtension(filePath).Equals(".json", StringComparison.OrdinalIgnoreCase)))
            {
                throw new BusinessValidationFailedException(StaticData.International("InternationalizationHelper_PathError"));
            }
        }

        public static async Task BatchAddJsonKeyValueAsync(List<BatchAddInternationalizationDto> batchAddDtos)
        {
            VerifyFolder();

            var usJsonPath = Path.Combine(JsonFileFolder, StaticData.En_US_Json);
            var cnJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json);


            //更新json 文件  同时更新内存缓存的数据
            foreach (var filePath in new string[] { usJsonPath, cnJsonPath })
            {
                var json = await File.ReadAllTextAsync(filePath);

                JObject jsonObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load });

                // 添加或更新指定的键值对

                if (filePath.Contains(StaticData.En_US_Json))
                {
                    foreach (var item in batchAddDtos)
                    {
                        jsonObject[item.Code] = item.Value;

                        StaticData.En_US_Dic[item.Code] = item.Value;
                    }                   
                }
                else
                {
                    foreach (var item in batchAddDtos)
                    {
                        jsonObject[item.Code] = item.Value;

                        StaticData.Zh_CN_Dic[item.Code] = item.Value;
                    }                      
                }


                await File.WriteAllTextAsync(filePath, jsonObject.ToString());

            }
        }

        public static async Task AddOrUpdateJsonKeyValueAsync(string key, string value, string valueCN)
        {

            VerifyFolder();

            var usJsonPath = Path.Combine(JsonFileFolder, StaticData.En_US_Json);
            var cnJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json);


            //更新json 文件  同时更新内存缓存的数据
            foreach (var filePath in new string[] { usJsonPath, cnJsonPath })
            {
                var json = await File.ReadAllTextAsync(filePath);

                JObject jsonObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load });

                // 添加或更新指定的键值对

                if (filePath.Contains(StaticData.En_US_Json))
                {
                    jsonObject[key] = value;

                    StaticData.En_US_Dic[key] = value;
                }
                else
                {
                    jsonObject[key] = valueCN;

                    StaticData.Zh_CN_Dic[key] = valueCN;
                }


                await File.WriteAllTextAsync(filePath, jsonObject.ToString());

            }

        }




    }
}