using Microsoft.AspNetCore.Http;
using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace ASHermed.SinoInterface.Tools
{
    /// <summary>
    /// 通用方法
    /// </summary>
    public class Common
    {


        /// <summary>
        /// 返回2点距离 千米
        /// </summary>
        /// <param name="lat1"></param>
        /// <param name="lng1"></param>
        /// <param name="lat2"></param>
        /// <param name="lng2"></param>
        /// <returns></returns>
        public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
        {
            double EARTH_RADIUS = 6378137;
            double radLat1 = Rad(lat1);
            double radLng1 = Rad(lng1);
            double radLat2 = Rad(lat2);
            double radLng2 = Rad(lng2);
            double a = radLat1 - radLat2;
            double b = radLng1 - radLng2;
            double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS;
            return Math.Round(result / 1000, 0);
        }

        public static bool IsChineseName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return true;
            }
            var nameLenth = name.Length;
            for (int i = 0; i < nameLenth; i++)
            {
                var na= name.Substring(i,1).IsChinese();
                if (na)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 经纬度转化成弧度
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        private static double Rad(double d)
        {
            return (double)d * Math.PI / 180d;
        }


        public static bool ContainArray(string[] a, string[] b)
        {
            if (a == null || b == null)
            {
                return false;
            }
            Array.Sort(a);
            Array.Sort(b);

            int start = 0;
            int match = 0;
            for (int i = 0; i < b.Length; i++)
            {
                for (int j = start; j < a.Length; j++)
                {
                    if (b[i] == (a[j]))
                    {
                        start = j + 1;
                        match++;
                        break;
                    }
                }
            }
            return match == b.Length;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="hashText"></param>
        /// <returns></returns>
        public static string GetMD5(string hashText)
        {
            MD5 mp = MD5.Create();
            byte[] _byte = mp.ComputeHash(Encoding.UTF8.GetBytes(hashText));
            mp.Dispose();
            string keyMd5 = string.Empty;
            for (int i = 0; i < _byte.Length; i++)
            {
                keyMd5 += _byte[i].ToString("x2");
            }
            return keyMd5;
        }


        public static string[] chars = new string[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };


        public static string CreateCode(int len)
        {
            string uuid = (Guid.NewGuid() + "").Replace("-", "");
            var str = uuid.Substring(0, len);
            return str;
        }

        /// <summary>
        /// URL编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEncode(string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }
            return (sb.ToString());
        }
        /// <summary>
        /// URL解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlDecode(string str)
        {
            char[] cr = str.ToCharArray();

            byte[] byStr = new byte[cr.Length / 2];

            int j = 0;
            for (int i = 0; i < byStr.Length; i++)
            {
                byStr[i] = byte.Parse(cr[j] + "" + cr[j + 1], System.Globalization.NumberStyles.HexNumber);
                j = j + 2;
            }
            str = System.Text.Encoding.UTF8.GetString(byStr);

            return (str);
        }
        /// <summary>
        /// 随机数7位
        /// </summary>
        /// <returns></returns>
        public static string NewCode()
        {
            string str = "";
            Random rnd = new Random();
            str = rnd.Next(1000000, 9999999).ToString();
            return str;
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

        /// <summary>  
        /// 时间戳Timestamp转换成日期  
        /// </summary>  
        /// <param name="timeStamp"></param>  
        /// <returns></returns>  
        public static DateTime GetDateTime(int timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = ((long)timeStamp * 10000000);
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime targetDt = dtStart.Add(toNow);
            return targetDt;
        }


        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="publickey"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RSAEncrypt(string publickey, string content)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.FromXmlString(publickey);
            cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);

            return Convert.ToBase64String(cipherbytes);
        }

        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="privatekey"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RSADecrypt(string privatekey, string content)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.FromXmlString(privatekey);
            cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);

            return Encoding.UTF8.GetString(cipherbytes);
        }

        /// <summary>
        /// 链接生成二维码
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        public static string MakeQrCode(string link)
        {

            var generator = new QRCodeGenerator();
            var codeData = generator.CreateQrCode(link, QRCodeGenerator.ECCLevel.M, true);
            var qrcode = new QRCode(codeData);
            var qrImage = qrcode.GetGraphic(100, Color.Black, Color.White, true);
            var ms = new MemoryStream();
            //图片格式指定为png
            qrImage.Save(ms, ImageFormat.Png);
            byte[] bytes = ms.GetBuffer();
            ms.Close();
            return Convert.ToBase64String(bytes);
        }

        public static string GetWeekDay(int week, int lan)
        {
            //var retStr = string.Empty;
            switch (week)
            {
                case 0:
                    return lan == 1 ? "星期天" : "Sunday";
                case 1:
                    return lan == 1 ? "星期一" : "Monday";
                case 2:
                    return lan == 1 ? "星期二" : "Tuesday";
                case 3:
                    return lan == 1 ? "星期三" : "Wednesday";
                case 4:
                    return lan == 1 ? "星期四" : "Thursday";
                case 5:
                    return lan == 1 ? "星期五" : "Friday";
                case 6:
                    return lan == 1 ? "星期六" : "Saturday";
                default:
                    return "";
            }

        }
    }
}
