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

namespace Hotline.Authentications
{
    public class ZzptSessionContext : ISessionContext
    {
        public const string Key = "ZzptSessionContext";
        public ZzptSessionContext(ISystemSettingCacheManager systemSettingCacheManager)
        {
            var setting = systemSettingCacheManager.GetSetting(SettingConstants.CityBaseConfiguration)?.SettingValue[0];
            var cityBase = JsonConvert.DeserializeObject<CityBaseConfiguration>(setting);
            var config = cityBase.ComprehensiveTreatment;

            UserId = config.UserId;
            UserName = config.UserName;
            OrgId = config.OrgId;
            OrgName = config.OrgName;
            OrgLevel = 1;
        }

        /// <summary>
        /// Id of current tenant or null for host
        /// </summary>
        public string? UserId { get; init; }

        /// <summary>
        /// Id of current user or throw Exception for guest
        /// </summary>
        public string RequiredUserId => UserId ?? throw new ArgumentNullException();
        public string? UserName { get; init; }
        public string? Phone { get; init; }

        /// <summary>
        /// Roles
        /// </summary>
        public string[] Roles { get; init; }
        public string? OrgId { get; init; }
        public string RequiredOrgId => OrgId ?? throw new ArgumentNullException();
        public string? OrgName { get; init; }
        public int OrgLevel { get; init; }
        public string? OrgAreaCode { get; init; }
        public bool OrgIsCenter { get; init; }

        /// <summary>
        /// 部门行政区划名称
        /// </summary>
        public string? OrgAreaName { get; init; }
        public string? AreaId { get; init; }
        public string? ClientId { get; init; }

        /// <summary>
        /// 工号
        /// </summary>
        public string? StaffNo { get; init; }

        /// <summary>
        /// 第三方平台用户唯一标识
        /// 例如: 微信的OpenId
        /// </summary>
        public string? OpenId { get; init; }

        public void ChangeSession(string id)
        {
            throw new NotImplementedException();
        }

        public async Task ChangeSessionAsync(string userId, CancellationToken cancellation)
        {
            throw new NotImplementedException();
        }

        public void ChangeSession(string userId, string username, string orgId, string orgname, int orgLevel)
        {
            throw new NotImplementedException();
        }
    }
}
