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

namespace APT.Utility
{
    /// <summary>
    /// WebApi Session实现类
    /// </summary>
    public class AspNetSessionProvider : ISession, ISessionProvider
    {
        private readonly SessionData sessionData;
        public AspNetSessionProvider()
        {
            try
            {

                var token = Infrastructure.Api.HttpContext.Current.Request.Headers["Authorization"].ToString().Replace("Bearer ", string.Empty);
                var userId = Infrastructure.Api.HttpContext.Current.Request.Headers["userid"].ToString();
                var userName = Infrastructure.Api.HttpContext.Current.Request.Headers["username"].ToString();
                var orgIdStr = Infrastructure.Api.HttpContext.Current.Request.Headers["orgId"].ToString();
                var departmentId = Infrastructure.Api.HttpContext.Current.Request.Headers["departmentId"].ToString();
                var mineType = Infrastructure.Api.HttpContext.Current.Request.Headers["minetype"].ToString();
                var dataRule = Infrastructure.Api.HttpContext.Current.Request.Headers["DataRule"].ToString()?.Split(new char[] { ','}).ToArray();
                if (mineType == "null")
                {
                    mineType = "";
                }
                if (!string.IsNullOrEmpty(token) && !string.IsNullOrEmpty(userId) && userId != "null")
                {
                    Guid? orgId = null;
                    if (!string.IsNullOrEmpty(orgIdStr))
                        orgId = new Guid(orgIdStr);
                    JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
                    sessionData = new SessionData
                    {
                        UserId = new Guid(userId),
                        UserName = userName,
                        DepartmentId = (string.IsNullOrEmpty(departmentId) || departmentId == "null") ? Guid.Empty : new Guid(departmentId),
                        OrgId = orgId,
                        MineType = mineType,
                        DataRule = dataRule,
                    };

                }
            }
            catch { }
        }

        #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;
            }
        }

        /// <summary>
        /// 生产单元类型 wywAdd   
        /// </summary>
        public string MineType
        {
            get
            {
                return sessionData == null ? null : sessionData.MineType;
            }
            set
            {
                if (sessionData != null) sessionData.MineType = value;
            }
        }
        /// <summary>
        /// 生产单元类型 wywAdd   
        /// </summary>
        public string[] DataRule
        {
            get
            {
                return sessionData == null ? null : sessionData.DataRule;
            }
            set
            {
                if (sessionData != null) sessionData.DataRule = value;
            }
        }
        #endregion
    }
}