using OASystem.Domain.Entities.PersonnelModule;
using OASystem.Domain.Entities.System;
using OASystem.Domain.ViewModels.PersonnelModule;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace OASystem.API.OAMethodLib
{
    public static class GeneralMethod
    {

        #region 消息



        #endregion

        #region md5 加密

        /// <summary>
        /// MD5加密,和动网上的16/32位MD5加密结果相同,
        /// 使用的UTF8编码
        /// </summary>
        /// <param name="source">待加密字串</param>
        /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
        /// <returns>加密后的字串</returns>
        public static string Encrypt(string source, int length = 32)
        {
            if (string.IsNullOrWhiteSpace(source))
                return string.Empty;
            HashAlgorithm hashAlgorithm = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
            byte[] bytes = Encoding.UTF8.GetBytes(source);
            byte[] hashValue = hashAlgorithm.ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            switch (length)
            {
                case 16://16位密文是32位密文的9到24位字符
                    for (int i = 4; i < 12; i++)
                    {
                        sb.Append(hashValue[i].ToString("x2"));
                    }
                    break;
                case 32:
                    for (int i = 0; i < 16; i++)
                    {
                        sb.Append(hashValue[i].ToString("x2"));
                    }
                    break;
                default:
                    for (int i = 0; i < hashValue.Length; i++)
                    {
                        sb.Append(hashValue[i].ToString("x2"));
                    }
                    break;
            }
            return sb.ToString();
        }


        #endregion

        #region jwt

        /// <summary>
        ///  获取token 
        /// </summary>
        /// <param name="_config"></param>
        /// <param name="Number"></param>
        /// <param name="exp"></param>
        /// <returns></returns>
        public static string GetToken(IConfiguration _config,string Number,DateTime exp) 
        {
            var claims = new[] {
                new Claim(ClaimTypes.NameIdentifier, "Future"),
                new Claim("Number",Number)
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"]));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var token = new JwtSecurityToken(
                issuer: "OASystem.com",
                audience: "OASystem.com",
                claims: claims,
                expires: exp,
                signingCredentials: creds);
           
            
            return   new JwtSecurityTokenHandler().WriteToken(token);

        }


        #endregion

        #region  数据类型转换

        /// <summary>
        /// object 转 Int
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int GetInt(this object obj)
        {
            if (obj == null)
                return 0;
            int _number = 0;
            bool reslut = Int32.TryParse(obj.ToString(), out _number);
            return _number;

        }

        private static DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
        private static long longTime = 621355968000000000;
        private static int samllTime = 10000000;
        
        /// <summary>
        /// 时间戳 转 datetime
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime GetTimeSpmpToDate(this object timeStamp)
        {
            if (timeStamp == null) return dateStart;
            DateTime dateTime = new DateTime(longTime + Convert.ToInt64(timeStamp) * samllTime, DateTimeKind.Utc).ToLocalTime();
            return dateTime;
        }

        #endregion

    }
}
