using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace Common.system
{
    public class DataProvider
    {
        ///3DES解密   
        public static string TripleDESDecrypt(string pToDecrypt, string sKey)
        {
            sKey = sKey.Substring(0, 24);
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider
            {
                Mode = CipherMode.ECB,
                Padding = PaddingMode.Zeros
            };
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = new byte[] { 0, 00, 00, 00, 00, 00, 00, 00, };
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();

            return System.Text.Encoding.Default.GetString(ms.ToArray()).Replace("\0", "");
        }
        /// <summary>
        /// 返回一个时间戳到秒
        /// </summary>
        /// <returns></returns>
        public static long TimestampTotalSeconds()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            long timestr = Convert.ToInt64(ts.TotalSeconds);
            return timestr;
        }
        /// <summary>
        /// 将字典类型序列化为json字符串
        /// </summary>
        /// <typeparam name="TKey">字典key</typeparam>
        /// <typeparam name="TValue">字典value</typeparam>
        /// <param name="dict">要序列化的字典数据</param>
        /// <returns>json字符串</returns>
        public static string SerializeDictionaryToJsonString<TKey, TValue>(Dictionary<TKey, TValue> dict)
        {
            if (dict.Count == 0)
            {
                return "";
            }

            string jsonStr = JsonConvert.SerializeObject(dict);
            return jsonStr;
        }
        /// <summary>
        ///调用Post接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string HttpPost(string body, string Url)
        {
            try
            {
                Encoding encoding = Encoding.UTF8;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.Accept = "text/html,application/xhtml+xml,*/*";
                request.ContentType = "application/json";

                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                // LogHelper.Instance.Debug($"POST接口连接失败，请求参数：{ex.Message}");
                return "POST报错:" + ex.Message;
            }
        }
    }
}
