using System.Security.Authentication;
using System.Security.Claims;
using IdentityModel;
using Microsoft.AspNetCore.Http;
using XF.Domain.Dependency;
using XF.Domain.Extensions;

namespace XF.Domain.Authentications
{
    public class DefaultSessionContext : ISessionContext, IScopeDependency
    {
        public DefaultSessionContext(IHttpContextAccessor httpContextAccessor)
        {
            var httpContext = httpContextAccessor.HttpContext;
            if (httpContext is null)
                //throw new ArgumentNullException(nameof(httpContext));
                return;

            var user = httpContext.User;
            //UserId = user.FindFirstValue(JwtClaimTypes.Id);
            UserId = user.FindFirstValue(ClaimTypes.NameIdentifier);
            UserName = user.FindFirstValue(AppClaimTypes.UserDisplayName);
            Phone = user.FindFirstValue(JwtClaimTypes.PhoneNumber);
            //Roles = user.Claims.Where(d => d.Type == JwtClaimTypes.Role).Select(d => d.Value).ToArray();
            Roles = user.Claims.Where(d => d.Type == ClaimTypes.Role).Select(d => d.Value).ToArray();
            OrgId = user.FindFirstValue(AppClaimTypes.DepartmentId);
            OrgName = user.FindFirstValue(AppClaimTypes.DepartmentName);
            OrgLevel = user.FindIntValue(AppClaimTypes.DepartmentLevel);
            OrgAreaCode = user.FindFirstValue(AppClaimTypes.DepartmentAreaCode);
            OrgAreaName = user.FindFirstValue(AppClaimTypes.DepartmentAreaName);
            OrgIsCenter = user.FindBoolValue(AppClaimTypes.DepartmentIsCenter);
            AreaId = user.FindFirstValue(AppClaimTypes.AreaId);
            ClientId = user.FindFirstValue(JwtClaimTypes.ClientId);
            StaffNo = user.FindFirstValue(AppClaimTypes.StaffNo);
        }

        /// <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>
        /// <exception cref="AuthenticationException"></exception>
        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? 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 RequiredOrgId => OrgId ?? throw new ArgumentNullException();
        
        public string? ClientId { get; init; }

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

    public static class ClaimsPrincipalExtensions
    {
        public static int FindIntValue(this ClaimsPrincipal claims, string claimType)
        {
            var value = claims.FindFirstValue(claimType);
            return int.TryParse(value, out int result) ? result : default;
        }

        public static bool FindBoolValue(this ClaimsPrincipal claims, string claimType)
        {
            var value = claims.FindFirstValue(claimType);
            return bool.TryParse(value, out bool result) ? result : default;
        }
    }
}