using Atomx.Common.Constant;
using Atomx.Common.Entities;
using Microsoft.EntityFrameworkCore;

namespace Atomx.Data.CacheServices
{
    public partial interface ICacheService
    {
        /// <summary>
        /// 通过文化名城获取对应的本地化译文
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        Task<Dictionary<string, string>> GetLocaleResourcesAsync(string culture);

        /// <summary>
        /// 添加或更新文化本地化信息
        /// </summary>
        /// <param name="localeResource"></param>
        /// <returns></returns>
        Task SetLocaleResources(LocaleResource localeResource);

        /// <summary>
        /// 删除文化本地化资源信息
        /// </summary>
        /// <param name="localeResource"></param>
        /// <returns></returns>
        Task DeleteLocaleResources(LocaleResource localeResource);

        /// <summary>
        /// 重载本地化资源
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        Task ReloadLocaleResources(string culture);

        /// <summary>
        /// 移除文化本地化资源缓存
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        Task RemoveLocaleResources(string culture);

        /// <summary>
        /// 指定文化本地化资源获和名城获取译文
        /// </summary>
        /// <param name="culture"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        Task<string> GetLocaleResourcesString(string culture, string name);
    }
    public partial class CacheService : ICacheService
    {
        /// <summary>
        /// 通过文化名城获取对应的本地化译文，如果缓存不存在则从数据库加载
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        public async Task<Dictionary<string, string>> GetLocaleResourcesAsync(string culture)
        {
            var key = $"{CacheKeys.LocaleResourcePrefix}{culture}";
            var cacheData = GetCache<Dictionary<string, string>>(key);
            if (cacheData == null)
            {
                var language = await GetLanguageByCulture(culture);
                if (language == null)
                {
                    return null;
                }
                var data = await _dbContext.LocaleResources.Where(p => p.LanguageId == language.Id).ToDictionaryAsync(p => p.Name, p => p.Value);
                if (data.Any())
                {
                    cacheData = data;
                    SetCache(key, cacheData);
                }
            }
            return cacheData;
        }


        /// <summary>
        /// 添加或更新文化本地化信息
        /// </summary>
        /// <param name="localeResource"></param>
        /// <returns></returns>
        public async Task SetLocaleResources(LocaleResource localeResource)
        {
            var language = await GetLanguageById(localeResource.LanguageId);
            if (language == null)
            {
                return;
            }
            var key = $"{CacheKeys.LocaleResourcePrefix}{language.Culture}";
            var data = await GetLocaleResourcesAsync(language.Culture);
            if (data.ContainsKey(localeResource.Name))
            {
                data[localeResource.Name] = localeResource.Value;
            }
            else
            {
                data.Add(localeResource.Name, localeResource.Value);
            }
            SetCache(key, data);
        }

        /// <summary>
        /// 删除文化本地化资源信息
        /// </summary>
        /// <param name="localeResource"></param>
        /// <returns></returns>
        public async Task DeleteLocaleResources(LocaleResource localeResource)
        {
            var language = await GetLanguageById(localeResource.LanguageId);
            if (language == null)
            {
                return;
            }
            var key = $"{CacheKeys.LocaleResourcePrefix}{language.Culture}";
            var data = await GetLocaleResourcesAsync(language.Culture);
            data.Remove(localeResource.Name);
        }


        /// <summary>
        /// 重载本地化资源
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        public async Task ReloadLocaleResources(string culture)
        {
            var key = $"{CacheKeys.LocaleResourcePrefix}{culture}";
            var cacheData = await GetCacheAsync<Dictionary<string, string>>(key);
            var language = await GetLanguageByCulture(culture);
            if (language == null)
            {
                return;
            }

            var data = await _dbContext.LocaleResources.Where(p => p.LanguageId == language.Id).ToDictionaryAsync(p => p.Name, p => p.Value);

            if (data.Any())
            {
                cacheData = data;
                await SetCacheAsync(key, cacheData);
            }
        }

        /// <summary>
        /// 移除文化本地化资源缓存
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        public async Task RemoveLocaleResources(string culture)
        {
            var key = $"{CacheKeys.LocaleResourcePrefix}{culture}";
            await Remove(key);
        }

        /// <summary>
        /// 指定文化本地化资源获和名城获取译文
        /// </summary>
        /// <param name="culture"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<string> GetLocaleResourcesString(string culture, string name)
        {
            var key = $"{CacheKeys.LocaleResourcePrefix}{culture}";
            var cacheData = await GetCacheAsync<Dictionary<string, string>>(key);
            if (cacheData == null)
            {
                return string.Empty;
            }
            if (cacheData.TryGetValue(name, out string value))
            {
                return value;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
