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

namespace Atomx.Data.CacheServices
{
    public partial interface ICacheService
    {
        /// <summary>
        /// 获取国家数据
        /// </summary>
        /// <returns></returns>
        Task<List<Area>> GetCountry(bool? reload = false);

        /// <summary>
        /// 通过ID获取国家名称
        /// </summary>
        /// <param name="countryId"></param>
        /// <returns></returns>
        Task<string> GetCountryName(long countryId);

        /// <summary>
        /// 根据上级ID获取下级数据
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        Task<List<Area>> GetAreas(long parentId, bool? reload = false);

        /// <summary>
        /// 更新调整缓存数据
        /// </summary>
        /// <param name="area"></param>
        /// <returns></returns>
        Task ResetArea(Area area);
    }
    public partial class CacheService : ICacheService
    {
        public async Task<List<Area>> GetCountry(bool? reload = false)
        {
            bool reloadData = reload.HasValue ? reload.Value : false;

            var cacheData = await GetCacheAsync<List<Area>>(CacheKeys.Country);
            if(cacheData == null || reloadData)
            {
                var countries = (from p in _dbContext.Areas
                             where p.ParentId == 0
                             select p).ToList();
                await SetCacheAsync(CacheKeys.Country, countries);
                return countries;
                     
            }
            return cacheData;
        }

        public async Task<string> GetCountryName(long countryId)
        {
            var cacheData = await GetCountry();
            var data = cacheData.SingleOrDefault(p => p.Id == countryId);
            if (data == null)
            {
                return string.Empty;
            }
            return data.Name;
        }

        public async Task<List<Area>> GetAreas(long parentId, bool? reload = false)
        {
            if (parentId == 0)
            {
                return await GetCountry();
            }
            var cacheData = await GetCacheAsync<List<Area>>($"{CacheKeys.Area}.{parentId}");
            bool reloadData = reload.HasValue ? reload.Value : false;
            if (cacheData == null || reloadData)
            {
                var data = (from p in _dbContext.Areas
                                 where p.ParentId == 0
                                 select p).ToList();
                await SetCacheAsync(CacheKeys.Country, data);
                return data;

            }
            return cacheData;
        }

        public async Task ResetArea(Area area)
        {
            var cacheData = await GetAreas(area.ParentId);
            var data = cacheData.Where(p => p.Id == area.Id).SingleOrDefault();
            if (data != null)
            {
                cacheData.Remove(data);
            }
            cacheData.Add(area);
            await SetCacheAsync($"{CacheKeys.Area}.{area.ParentId}", cacheData);
        }
    }
}
