using Atomx.Common.Constant;
using Atomx.Common.Entities;

namespace Atomx.Data.CacheServices
{
    public partial interface ICacheService
    {
        /// <summary>
        /// 获取语言信息
        /// </summary>
        /// <param name="reload"></param>
        /// <returns></returns>
        Task<List<Language>> GetLanguages(bool? reload = null);

        /// <summary>
        /// 通过ID获取或更新缓存
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        Task<Language> GetLanguageById(long id, Language? data = null);

        /// <summary>
        /// 通过代码获取语言
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        Task<Language> GetLanguageByCulture(string culture);
    }


    public partial class CacheService : ICacheService
    {
        public async Task<List<Language>> GetLanguages(bool? reload = null)
        {
            var cacheData = await GetCacheAsync<List<Language>>(CacheKeys.Languages);
            if (cacheData == null || reload.HasValue)
            {
                var data = _dbContext.Languages.Where(p => p.Enabled).ToList();
                if (data != null)
                {
                    cacheData = data;
                    await SetCacheAsync(CacheKeys.Languages, cacheData);
                }
            }

            return cacheData;
        }

        public async Task<Language> GetLanguageById(long id, Language? data = null)
        {
            var cacheData = await GetLanguages();
            if (data != null)
            {
                var language = cacheData.SingleOrDefault(p => p.Id == id);
                if (language != null)
                {
                    cacheData.Remove(language);
                    cacheData.Add(data);
                    await SetCacheAsync(CacheKeys.Languages, cacheData);
                }
                else
                {
                    cacheData.Add(data);
                    await SetCacheAsync(CacheKeys.Languages, cacheData);
                }
                return data;
            }
            else
            {
                var language = cacheData.SingleOrDefault(p => p.Id == id);
                if (language != null)
                {
                    return language;
                }
                return new Language();
            }
        }

        /// <summary>
        /// 通过代码获取语言
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        public async Task<Language> GetLanguageByCulture(string culture)
        {
            var cacheData = await GetLanguages();

            var language = cacheData.SingleOrDefault(p => p.Culture == culture);
            if (language == null)
            {
                language = _dbContext.Languages.SingleOrDefault(p => p.Culture == culture);
                if (language != null)
                {
                    cacheData.Add(language);
                    await SetCacheAsync(CacheKeys.Languages, cacheData);
                }
            }
            return language;

        }
    }
}
