using APT.Infrastructure.Core;
using APT.Utility;
using APT.FM.WebApi.Models;
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Threading;

namespace APT.FM.WebApi.Providers
{
    /// <summary>
    /// WebApi Session实现类
    /// </summary>
    public class AspNetSessionProvider : ISession, ISessionProvider
    {
        private readonly SessionData sessionData;
        public AspNetSessionProvider()
        {
            try
            {
                var token = Infrastructure.Core.HttpContext.Current.Request.Headers["Authorization"].ToString().Replace("Bearer ", string.Empty);
                var userId = Infrastructure.Core.HttpContext.Current.Request.Headers["userid"].ToString();
                var orgId = Infrastructure.Core.HttpContext.Current.Request.Headers["orgId"].ToString();
                if (!string.IsNullOrEmpty(token) && !string.IsNullOrEmpty(userId) && userId != "null")
                {
                    JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
                    //var user = handler.ReadJwtToken(token);
                    //var info = handler.ReadToken(token);
                    //var UserClaim = user.Payload["UserData"] == null ? "{}" : user.Payload["UserData"].ToString();
                    sessionData = new SessionData
                    {
                        UserId = new Guid(userId),
                        OrgId = string.IsNullOrEmpty(orgId) ? null : new Guid(orgId),
                    };
                }

            }
            catch { }

            //throw new Exception("令牌过期");

        }

        #region ISessionProvider 成员

        /// <summary>
        /// 获取会话对象
        /// </summary>
        /// <returns></returns>
        public ISession GetSession()
        {
            return this;
        }

        #endregion

        #region ISession 成员

        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId
        {
            get
            {
                return sessionData == null || sessionData.UserId == null ? null : sessionData.UserId.ToString();
            }
            set
            {
            }
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid? UserID
        {
            get
            {
                if (string.IsNullOrEmpty(this.UserId))
                    return null;
                return Guid.Parse(this.UserId);
            }
            set
            {

            }
        }

        /// <summary>
        /// 用户编号
        /// </summary>
        public string UserCode
        {
            get
            {
                return sessionData == null ? null : sessionData.UserCode;
            }
            set { }
        }

        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName
        {
            get
            {
                return sessionData == null ? null : sessionData.UserName;
            }
            set
            {
            }
        }

        /// <summary>
        /// 部门ID
        /// </summary>
        public string DepartmentId
        {
            get
            {

                return sessionData == null || sessionData.DepartmentId == null ? null : sessionData.DepartmentId.ToString();
            }
            set
            {
            }
        }

        /// <summary>
        /// 部门ID
        /// </summary>
        public Guid? DepartmentID
        {
            get
            {
                if (string.IsNullOrEmpty(this.DepartmentId))
                    return null;
                return Guid.Parse(this.DepartmentId);
            }
            set
            {

            }
        }



        public Guid? OrgId
        {
            get
            {
                return sessionData == null ? null : sessionData.OrgId;
            }
            set
            {

            }
        }
        public Guid? RootOrgId
        {
            get
            {
                return sessionData == null ? null : sessionData.RootOrgId;
            }
            set
            {
                if (sessionData != null) sessionData.RootOrgId = value;
            }
        }
        #endregion
    }
}