using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stripe.Terminal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace WpfApp1.transToNet
{
    internal class jsonNet : Messiah
    {
        static string https = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
        static string appid = "20230925001829663";//20230925001829663,20230923001827495
        static Random rd = new Random();
        static string salt = rd.Next(100000).ToString();
        static string key = "f7ZHpw28izQ1VU_ixoY5";//f7ZHpw28izQ1VU_ixoY5,yjvMNY33Qc1TYSBjTQYQ
        //baidu

        static string xunweb = "https://itrans.xf-yun.com/v1/its";
        static string xunid = "1299c840";
        static string xunsec = "M2Q4YzkzZTIwZjNkMGE2ZjkwZWMxNzZk";
        static string xunkey = "828cb2dc915d28464a904bda5d7b01b3";
        static string xunfl = "ja";
        static string xundl = "cn";
        static string xuntxt = "白上";


        //static HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=20230925001829663&salt=1435660288&sign=c963bc2275c0fd1ed30da014c1ef1933");

        /*
        private static HttpClient HttpRe = new HttpClient()
        {
            BaseAddress = new Uri("http://api.fanyi.baidu.com/api/trans/vip/translate?"),
        };
        
        static async Task GetAsync(HttpClient httpc)
        {
            using HttpResponseMessage response = await httpc.GetAsync(temp);

            //var value = response.EnsureSuccessStatusCode().WriteRequestToConsole();

            var jsonResponse = await response.Content.ReadAsStringAsync();//收到json

            Console.WriteLine($"{jsonResponse}\n");
        }
        */
        private HttpClient HttpRequestC(string url)
        {
            HttpClient httpClient = new HttpClient()
            {
                BaseAddress = new Uri(url)
            };
            return httpClient;
        }

        private static string EncryptString(string str)//百度MD5加密计算方式
        {
            MD5 md5 = MD5.Create();
            // 将字符串转换成字节数组
            byte[] byteOld = Encoding.UTF8.GetBytes(str);
            // 调用加密方法
            byte[] byteNew = md5.ComputeHash(byteOld);
            // 将加密结果转换为字符串
            StringBuilder sb = new StringBuilder();
            foreach (byte b in byteNew)
            {
                // 将字节转换成16进制表示的字符串，
                sb.Append(b.ToString("x2"));
            }
            // 返回加密的字符串
            return sb.ToString();
        }

        public string RequestJsonPrivateBaiDu(string input, string srcl, string dstl, string aid, string kkey)//自定义id，key以及源语言和目标语言
        {
            string sign = EncryptString(aid + input + salt + kkey);

            string temp = "q=" + HttpUtility.UrlEncode(input) + "&from=" + srcl + "&to=" + dstl + "&appid=" + aid + "&salt=" + salt + "&sign=" + sign;
            Console.WriteLine(https + temp);
            //Task t = GetAsync(HttpRe); t.Wait();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(https + temp);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = 6000;


            var response = request.GetResponse();
            var myResponseStream = response.GetResponseStream();
            var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            tempC2 temp1 = JsonConvert.DeserializeObject<tempC2>(retString);

            Trans_result result = temp1.trans_result[0];
            string s = result.dst;
            return s;
        }

        public string RequestJsonLanguageChangeBaiDu(string input, string srcl, string dstl)//可以自定义源语言与目标语言
        {
            string sign = EncryptString(appid + input + salt + key);

            string temp = "q=" + HttpUtility.UrlEncode(input) + "&from=" + srcl + "&to=" + dstl + "&appid=" + appid + "&salt=" + salt + "&sign=" + sign;
            Console.WriteLine(https + temp);
            //Task t = GetAsync(HttpRe); t.Wait();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(https + temp);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = 6000;


            var response = request.GetResponse();
            var myResponseStream = response.GetResponseStream();
            var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            tempC2 temp1 = JsonConvert.DeserializeObject<tempC2>(retString);

            Trans_result result = temp1.trans_result[0];
            string s = result.dst;
            return s;
        }

        public string RequestJsonBaiDu(string input)//默认
        {
            string sign = EncryptString(appid + input + salt + key);

            string temp = "q=" + HttpUtility.UrlEncode(input) + "&from=jp&to=zh&appid=" + appid + "&salt=" + salt + "&sign=" + sign;
            Console.WriteLine(https + temp);
            //Task t = GetAsync(HttpRe); t.Wait();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(https + temp);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = 6000;


            var response = request.GetResponse();
            var myResponseStream = response.GetResponseStream();
            var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            tempC2 temp1 = JsonConvert.DeserializeObject<tempC2>(retString);

            Trans_result result = temp1.trans_result[0];
            string s = result.dst;
            return s;
        }

        public string RequestJsonXunFei(string input)
        {
            return "error";
        }

        public string RequestJsonPrivateXunFei(string input, string srcl, string dstl, string xid, string xse , string xkey)
        {
            return "error";
        }

        public string RequestJsonLanguageChangeXunFei(string input, string srcl, string dstl)
        {
            return "error";
        }
    }
    class Trans_result
    {
        public string src { get; set; }
        public string dst { get; set; }
    }
    class tempC2
    {
        public List<Trans_result> trans_result { get; set; }
    }
}