using Hotline.Caching.Interfaces;
using Hotline.Settings;
using XF.Domain.Cache;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using XF.Domain.Repository;

namespace Hotline.Caching.Services
{
    public class SystemSettingCacheManager : ISystemSettingCacheManager, IScopeDependency
    {

        private readonly ITypedCache<SystemSetting> _cacheSystemSetting;
        private readonly IRepository<SystemSetting> _systemSettingRepository;

        public SystemSettingCacheManager(ITypedCache<SystemSetting> cacheSystemSetting, IRepository<SystemSetting> systemSettingRepository)
        {
            _cacheSystemSetting = cacheSystemSetting;
            _systemSettingRepository = systemSettingRepository;
        }

        public SystemSetting? GetSetting(string code)
        {
            var setting = _cacheSystemSetting.GetOrSet(code, x =>
            {
                var dbsetting = _systemSettingRepository.GetAsync(d => d.Code == code).GetAwaiter().GetResult();
                if (dbsetting == null)
                    throw new UserFriendlyException($"{code} 无效系统设置");
                return dbsetting;
            });
            return setting;
        }

        public void DelSystemSetting(string code)
        {
            _cacheSystemSetting.Remove(code);
        }

        public string GetOrDefault(string code, string name, string defaultValue, string remark)
        {
            try
            {
                var value = GetSetting(code)?.SettingValue[0];
                if (value == null) return defaultValue;
                return value.Trim();
            }
            catch (UserFriendlyException e)
            {
                if (e.Message.Contains("无效系统设置"))
                {
                    _systemSettingRepository.AddAsync(new SystemSetting
                    {
                        Code = code,
                        SettingName = name,
                        SettingValue = [defaultValue],
                        Remark = remark
                    }).GetAwaiter().GetResult();
                }
            }
            return defaultValue;
        }

        public int EffectiveTimes
            => int.Parse(GetSetting(SettingConstants.EffectiveTimes)?.SettingValue[0]);

        public int ConnectByeTimes
            => int.Parse(GetSetting(SettingConstants.ConnectByeTimes)?.SettingValue[0]);

        public int NoConnectByeTimes => int.Parse(GetSetting(SettingConstants.NoConnectByeTimes)?.SettingValue[0]);

        public int CallInOverConnRingTime => int.Parse(GetSetting(SettingConstants.CallInOverConnRingTime)?.SettingValue[0]);

        public int SeatChaoTime => int.Parse(GetSetting(SettingConstants.SeatChaoTime)?.SettingValue[0]);

        public int RingTimes => int.Parse(GetSetting(SettingConstants.RingTimes)?.SettingValue[0]);

        public string RecordPrefix => GetSetting(SettingConstants.RecordPrefix)?.SettingValue[0].Trim().ToString();

        public string DefaultVisitEmployeeId => GetSetting(SettingConstants.DefaultVisitEmployeeId)?.SettingValue[0].Trim().ToString();

        public int FixedQueryCount => int.Parse(GetSetting(SettingConstants.FixedQueryCount)?.SettingValue[0]);

        public bool AutomaticPublishOrder
        {
            get
            {
                var value = GetOrDefault(SettingConstants.AutomaticPublishOrder, "自动发布中心直办归档工单", "false", "用于中心直办件归档后默认自动发布, 开启就自动发布.true 或者 false");
                if (value == "true")
                {
                    return true;
                }
                return false;
            }
        }

        public bool CancelPublishOrderEnabled
        {
            get
            {
                var value = GetOrDefault(SettingConstants.CancelPublishOrderEnabled, "取消发布功能开关", "false", "取消发布功能总开关, 关闭后取消发布功能的所有代码都不会执行.true 或者 false");
                if (value == "true")
                {
                    return true;
                }
                return false;
            }
        }
    }
}
