using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using BBWY.Common.Extensions;
using Newtonsoft.Json;

namespace BBWY.Test
{
    public class DingDingAPITest
    {
        public DingDingAPITest()
        {

        }

        public void Send()
        {
            var contentStrBuilder = new StringBuilder();
            contentStrBuilder.Append("店铺名:布莱特玩具专营店");
            contentStrBuilder.AppendLine();
            for (var i = 0; i < 1; i++)
            {
                //contentStrBuilder.Append("店铺名:布莱特玩具专营店\n");
                contentStrBuilder.Append("SKU:123456\n");
                contentStrBuilder.Append("商品状态:稳定期\n");
                contentStrBuilder.Append("近7天销量:100件\n");
                contentStrBuilder.Append("XX仓库存:20件\n");
                contentStrBuilder.Append("XXX仓库存:50件\n");
                contentStrBuilder.Append("低于安全周转天数,建议备货500件");
                contentStrBuilder.AppendLine();
            }
          

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);


            var secret = "SEC11920bc0b0f57a963f9d7b7dea58e66737706ae39adf4754cc4ea795008ac645";
            var timestamp = DateTime.Now.DateTimeToStamp();
            var stringToSign = timestamp + "\n" + secret;
            var sign = EncryptWithSHA256(stringToSign, secret);

            var url = new Uri($"https://oapi.dingtalk.com/robot/send?access_token=9869fe02f4a423cbe6700867894d221ed535de3cca1a11113b83945be1170e6a&timestamp={timestamp}&sign={sign}");

            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, url))
                {
                    request.Content = new StringContent(JsonConvert.SerializeObject(new
                    {
                        msgtype = "text",
                        text = new
                        {
                            content = contentStrBuilder.ToString()
                        }
                    }), Encoding.UTF8, "application/json"); ;

                    using (var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result)
                    {
                        var resultStr = response.Content.ReadAsStringAsync().Result;
                        Console.Write(resultStr);
                    }
                }
            }
        }


        /// <summary>
        /// Base64 SHA256
        /// </summary>
        /// <param name="data">待加密数据</param>
        /// <param name="secret">密钥</param>
        /// <returns></returns>
        public static string EncryptWithSHA256(string data, string secret)
        {
            secret = secret ?? "";

            // 1、string 转换成 utf-8 的byte[]
            var encoding = Encoding.UTF8;
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] dataBytes = encoding.GetBytes(data);

            // 2、 HMACSHA256加密
            using (var hmac256 = new HMACSHA256(keyByte))
            {
                byte[] hashData = hmac256.ComputeHash(dataBytes);
                // 3、转换成base64
                var base64Str = Convert.ToBase64String(hashData);
                // 4、urlEncode编码
                return System.Web.HttpUtility.UrlEncode(base64Str, Encoding.UTF8);
            }
        }
    }
}