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

namespace Hotline.Tests.Controller;
public class TestSessionContext : ISessionContext, IScopeDependency
{
    private readonly IHttpContextAccessor _contextAccessor;
    private readonly ISessionContextProvider _changeSessionProvider;
    public TestSessionContext(IHttpContextAccessor httpContextAccessor, IServiceProvider serviceProvider)
    {
        _contextAccessor = httpContextAccessor;
        _changeSessionProvider = serviceProvider.GetService<ISessionContextProvider>();
    }

    public string? OpenId { get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.OpenId); } init { } }

    /// <summary>
    /// Id of current tenant or null for host
    /// </summary>
    public string? UserId
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); }
        init { }
    }

    /// <summary>
    /// Id of current user or throw Exception for guest
    /// </summary>
    /// <exception cref="AuthenticationException"></exception>
    public string RequiredUserId => _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier);
    public string? UserName
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.UserDisplayName); }
        init { }
    }

    public string? Phone
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(JwtClaimTypes.PhoneNumber); }
        init { }
    }

    /// <summary>
    /// Roles
    /// </summary>
    public string[] Roles
    {
        get { return _contextAccessor.HttpContext?.User.Claims.Where(d => d.Type == JwtClaimTypes.Role).Select(d => d.Value).ToArray(); }
        init { }
    }

    public string? OrgId
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId); }
        init { }
    }

    public string RequiredOrgId => _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId);
    public string? OrgName
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentName); }
        init { }
    }

    public int OrgLevel
    {
        get { return _contextAccessor.HttpContext?.User.FindIntValue(AppClaimTypes.DepartmentLevel) ?? 0; }
        init { }
    }

    public string? OrgAreaCode
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentAreaCode); }
        init { }
    }

    public bool OrgIsCenter
    {
        get { return _contextAccessor.HttpContext?.User.FindBoolValue(AppClaimTypes.DepartmentIsCenter) ?? false; }
        init { }
    }

    /// <summary>
    /// 部门行政区划名称
    /// </summary>
    public string? OrgAreaName
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentAreaName); }
        init { }
    }

    public string? AreaId
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.AreaId); }
        init { }
    }
    public string? ClientId
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(JwtClaimTypes.ClientId); }
        init { }
    }

    /// <summary>
    /// 工号
    /// </summary>
    public string? StaffNo
    {
        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.StaffNo); }
        init { }
    }
}
