using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.Utility
{
    public class WeiBoHelper
    {
        public static string CallBackUrl;   //成功授权后的回调地址
        public static string AppKey;        //应用的APPID
        public static string AppSercet;     //应用的APPKEY
        public static string AccessToken;   //应用的access_token

        /// <summary>
        /// 获取配置
        /// </summary>
        public WeiBoHelper()
        {
            CallBackUrl = Configs.GetValue("WeiBoCallBackUrl");
            AppKey = Configs.GetValue("WeiBoAppKey");
            AppSercet = Configs.GetValue("WeiBoAppSecret");
            AccessToken = Configs.GetValue("WeiBoAccessToken");
        }

        #region SHA1加密
        /// <summary>
        /// SHA1加密 （HASH算法没有解密）
        /// </summary>
        /// <param name="Source_String"></param>
        /// <returns></returns>
        public static string SHA1_Encrypt(string Source_String)
        {
            byte[] StrRes = Encoding.Default.GetBytes(Source_String);
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            StrRes = iSHA.ComputeHash(StrRes);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            return EnText.ToString();
        }
        #endregion

        /// <summary>
        /// 返回值是echostr的值
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="nonce"></param>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static bool ValidateSHA(string signature, string nonce, string timestamp)
        {
            string App_Sercet = Configs.GetValue("WeiBoAppSecret");
            //SHA1Encrypt sha1 = new SHA1Encrypt();
            if (signature == null || nonce == null || timestamp == null)
            {
                return false;
            }
            string sign = SHA1_Encrypt(getSignContent(nonce, timestamp, App_Sercet));
            if (!signature.Equals(sign))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 字典顺序升序构造签名串
        /// </summary>
        /// <param name="nonce"></param>
        /// <param name="timestamp"></param>
        /// <param name="app_secret"></param>
        /// <returns></returns>
        public static string getSignContent(string nonce, string timestamp, string app_secret)
        {
            Dictionary<string, string> dics = new Dictionary<string, string>();
            dics.Add("nonce", nonce);
            dics.Add("timestamp", timestamp);
            dics.Add("app_secret", app_secret);

            var vDic = (from objDic in dics orderby objDic.Key descending select objDic);
            StringBuilder str = new StringBuilder();
            foreach (KeyValuePair<string, string> kv in vDic)
            {
                string pvalue = kv.Value;
                str.Append(pvalue + "");
            }
            string result = str.ToString();
            return result;
        }

    }
}
