using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Net;
using System.Text;
using System.IO;
using RestSharp;

namespace BLWWS_BLL
{
    public static class Tools
    {
        /// <summary>
        /// 验证指定的号码是否是手机号码
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static bool IsMobileNumber(string number)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(number, @"1[0-9]{10}");
        }
        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="length">验证码长度</param>
        /// <returns></returns>
        public static string CreateValidateCode(int length)
        {
            int[] randMembers = new int[length];
            int[] validateNums = new int[length];
            System.Text.StringBuilder validateNumberStr = new System.Text.StringBuilder();
            //生成起始序列值
            int seekSeek = unchecked((int)DateTime.Now.Ticks);
            Random seekRand = new Random(seekSeek);
            int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
            int[] seeks = new int[length];
            for (int i = 0; i < length; i++)
            {
                beginSeek += 10000;
                seeks[i] = beginSeek;
            }
            //生成随机数字
            for (int i = 0; i < length; i++)
            {
                Random rand = new Random(seeks[i]);
                int pownum = 1 * (int)Math.Pow(10, length);
                randMembers[i] = rand.Next(pownum, Int32.MaxValue);
            }
            //抽取随机数字
            for (int i = 0; i < length; i++)
            {
                string numStr = randMembers[i].ToString();
                int numLength = numStr.Length;
                Random rand = new Random();
                int numPosition = rand.Next(0, numLength - 1);
                validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
            }
            //生成验证码
            for (int i = 0; i < length; i++)
            {
                validateNumberStr.Append(validateNums[i].ToString());
            }
            return validateNumberStr.ToString();
        }
        /// <summary>
        /// post数据接口
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="method">post和put，默认post</param>
        /// <returns></returns>
        public static string PostWebRequest(string url, string postData)
        {
            string result = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.ContentType = "application/json";
            req.Method = "POST";
            //req.Timeout = 10000;//设置请求超时时间，单位为毫秒
            byte[] data = Encoding.UTF8.GetBytes(postData);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            using (Stream stream = resp.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();//获取响应内容
                }
            }
            return result;

        }


        public static string PostWebRequestNew(string url, string postData)
        {
            string content = "";
            try
            {
                var client1 = new RestClient(url);
                var request1 = new RestRequest("", Method.POST);
                request1.AddJsonBody(postData);
                var Response = client1.Execute(request1).Content;
                content = Response;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.Message);
            }
            return content;
        }

        public static string ByteToString(byte[] bytesData)
        {
            StringBuilder result = new StringBuilder();
            foreach (byte r in bytesData)
            {
                result.Append(r.ToString("X2") + " ");
            }
            return result.ToString().Trim();
        }

        /// <summary>
        /// 16进制 字符串 转换成 字节 数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] HEXString2ByteArray(string hexString)
        {
            char[] hexCharacters = hexString.ToCharArray();
            byte[] byteArray = new byte[hexCharacters.Length / 2];

            for (int i = 0; i < byteArray.Length; i++)
            {
                string hexVal = string.Concat(hexCharacters[i * 2], hexCharacters[i * 2 + 1]);
                byteArray[i] = Convert.ToByte(hexVal, 16);
            }
            return byteArray;

        }

        public static long GetCurrentTimeStamp(DateTime dt)
        {
            TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
            long current_timestamp = Convert.ToInt64(ts.TotalSeconds);
            return current_timestamp;
        }
        public static DateTime GetCurrentDateTime(long timestampMilliseconds)
        {
            DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);
            DateTime utcTime = epoch.AddSeconds(timestampMilliseconds);
            return utcTime;
        }
    }
}