using Hotline.Caching.Interfaces;
using Hotline.Settings;
using Hotline.Settings.TimeLimits;
using Hotline.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Cache;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace Hotline.Caching.Services
{
    public class DaySettingCacheManager : IDaySettingCacheManager, IScopeDependency
    {
        private readonly IRepository<DaySetting> _daySettingRepository;

        private readonly ITypedCache<IReadOnlyList<DaySetting>> _cacheDaySetting;

        public DaySettingCacheManager(IRepository<DaySetting> daySettingRepository, ITypedCache<IReadOnlyList<DaySetting>> cacheDaySetting)
        {
            _daySettingRepository = daySettingRepository;
            _cacheDaySetting = cacheDaySetting;
        }


        public IReadOnlyList<DaySetting> GetDaySettingCache()
        {
            var sysDicDataList = _cacheDaySetting.GetOrSet("daysetting", k =>
            {
                return _daySettingRepository.Queryable().ToList();
            });
            return sysDicDataList;
        }

        public bool IsWorkDay(DateTime date)
        {
            var model = GetDaySettingCache().FirstOrDefault(x => x.Day.Date == date.Date);
            if (model != null)
            {
                return model.IsWorkDay;
            }
            return WeekTool.IsWorkDay(date);
        }

        public void DelDaySetting()
        {
            _cacheDaySetting.Remove("daysetting");
        }
    }
}
