using Atomx.Common.Utils;
using Atomx.Utils.Extension;

namespace Atomx.Admin.Services
{
    /// <summary>
    /// 
    /// </summary>
    public interface IIdentityService
    {
        /// <summary>
        /// 获取用户ID
        /// </summary>
        /// <returns></returns>
        long GetUserId();

        /// <summary>
        /// 获取用户角色ID
        /// </summary>
        /// <returns></returns>
        int GetUserRoleId();

        /// <summary>
        /// 获取客户端IP
        /// </summary>
        /// <returns></returns>
        string GetClientIp();

        /// <summary>
        /// 获取用户设置的时区
        /// </summary>
        /// <returns></returns>
        int GetTimeZone();
    }

    /// <summary>
    /// 
    /// </summary>
    public class IdentityService : IIdentityService
    {
        IHttpContextAccessor _httpContextAccessor;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        public IdentityService(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 获取用户ID
        /// </summary>
        /// <returns></returns>
        public long GetUserId()
        {
            //var userIdClaim = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimKeys.Id);
            //return userIdClaim != null ? long.Parse(userIdClaim.Value) : 0;

            var id = _httpContextAccessor.HttpContext?.User?.Claims?.SingleOrDefault(p => p.Type == ClaimKeys.Id)?.Value ?? "0";
            return id.ToLong();
        }

        /// <summary>
        /// 获取用户角色ID
        /// </summary>
        /// <returns></returns>
        public int GetUserRoleId()
        {
            //var userIdClaim = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimKeys.Role);
            //return userIdClaim != null ? int.Parse(userIdClaim.Value) : 0;

            var id = _httpContextAccessor.HttpContext?.User?.Claims?.SingleOrDefault(p => p.Type == ClaimKeys.Role)?.Value ?? "0";
            return id.ToInt();
        }

        /// <summary>
        /// 获取客户端IP
        /// </summary>
        /// <returns></returns>
        public string GetClientIp()
        {
            var ipAddress = _httpContextAccessor.HttpContext?.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (ipAddress == null)
            {
                ipAddress = _httpContextAccessor.HttpContext?.Request.Headers["X-Real-IP"].FirstOrDefault();
                if (ipAddress == null)
                {
                    ipAddress = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress?.ToString() ?? "";
                }
            }
            return ipAddress;
        }

        /// <summary>
        /// 获取用户设置的时区
        /// </summary>
        /// <returns></returns>
        public int GetTimeZone()
        {
            var timeZone = _httpContextAccessor.HttpContext?.User?.Claims?.SingleOrDefault(p => p.Type == "TimeZone")?.Value ?? "0";
            return timeZone.ToInt();
        }
    }
}
