using Hotline.Caching.Interfaces;
using Hotline.Caching.Services;
using Hotline.Configurations;
using Hotline.Settings;
using Hotline.Users;
using Newtonsoft.Json;
using XF.Domain.Authentications;
using XF.Domain.Dependency;


namespace Hotline.Authentications
{
    public interface ISessionContextCreator
    {
        Task CreateSessionContextAsync(string source, CancellationToken cancellation);
    }

    public class SessionContextCreator : ISessionContextCreator, IScopeDependency
    {
        private readonly ISystemSettingCacheManager _systemSettingCacheManager;
        private readonly ISessionContext _sessionContext;

        public SessionContextCreator(
            ISystemSettingCacheManager systemSettingCacheManager,
            ISessionContext sessionContext
            )
        {
            _systemSettingCacheManager = systemSettingCacheManager;
            _sessionContext = sessionContext;
        }

        //public static void CreateSessionContext(ISessionContextProvider sessionContextProvider, string source)
        //{
        //    switch (source)
        //    {
        //        case "province":
        //            sessionContextProvider.SetContext(ProvinceSessionContext.Key);
        //            break;
        //        case "110":
        //            sessionContextProvider.SetContext(Police110SessionContext.Key);
        //            break;
        //        case "yb-enterprise":
        //            sessionContextProvider.SetContext(YbEnterpriseSessionContext.Key);
        //            break;
        //        case "zzpt":
        //            sessionContextProvider.SetContext(ZzptSessionContext.Key);
        //            break;
        //        default:
        //            throw new ArgumentOutOfRangeException(nameof(source), source, null);
        //    }
        //}

        public async Task CreateSessionContextAsync(string source, CancellationToken cancellation)
        {
            var setting = _systemSettingCacheManager.GetSetting(SettingConstants.CityBaseConfiguration)?.SettingValue[0];
            var cityBase = JsonConvert.DeserializeObject<CityBaseConfiguration>(setting);
            DefaultCityBaseConfiguration config = source switch
            {
                "province" => cityBase.CityProvince,
                "110" => cityBase.PublicSecurity,
                "yb-enterprise" => cityBase.CityEnterprise,
                "zzpt" => cityBase.ComprehensiveTreatment,
                _ => throw new ArgumentOutOfRangeException(nameof(source), source, null)
            };

            //_sessionContext.ChangeSession(config.UserId, config.UserName, config.OrgId, config.OrgName, config.OrgId.CalcOrgLevel());
        }
    }
}