using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace XYY.Service.Standard.Trans
{


    public class BaiDuTrans
    {

        private readonly IDistributedCache _cache;

        public BaiDuTrans(IDistributedCache cache)
        {
            _cache = cache;
        }

        string key = @"trans";
        public async System.Threading.Tasks.Task<string> GetZHAsCache(string ori, string from, string to)
        {
            string en = "";
            if (_cache == null)
                en = await GetZH(ori, from, to);
            else
            {
                string tkey = key + "_" + ori;
                string value = _cache.GetString(tkey);
                try
                {
                    if (value != null)
                    {
                        en = value;
                    }
                    else
                    {
                        en = await GetZH(ori, from, to);
                        _cache.SetString(tkey, en);
                    }
                }
                catch (Exception ex)
                {
                    en = ori;
                }
            }
            if (string.IsNullOrEmpty(en))
                return ori;
            else
                return en;
        }

        public async System.Threading.Tasks.Task<string> GetZH(string ori, string from, string to)
        {
            if (!string.IsNullOrEmpty(ori))
            {
                System.Threading.Thread.Sleep(1000);
                ori = ori.Trim().ToLower();
                string host = "https://fanyi-api.baidu.com/api/trans/vip/translate";
                string q = ori;
                string appId = "20210319000734273";
                string signKey = "1wv0ws3ZZZFyWVeRlQtl";
                string salt = "xyy";
                string signBody = appId + q + salt + signKey;
                string sign = EncryptString(signBody);
                host += "?q=" + System.Web.HttpUtility.UrlEncode(q);
                host += "&from=" + from;
                host += "&to=" + to;
                host += "&appid=" + appId;
                host += "&salt=" + salt;
                host += "&sign=" + sign;
                System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                string str = await client.GetStringAsync(host);
                Trans_Result result = Newtonsoft.Json.JsonConvert.DeserializeObject<Trans_Result>(str);
                if (!string.IsNullOrEmpty(result.error_msg))
                {
                    throw new Exception(result.error_msg);
                }
                else
                {
                    return result.trans_result[0].dst;
                }
                return str;
            }
            return ori;

        }
        public class Trans_ResultItem
        {
            /// <summary>
            /// 
            /// </summary>
            public string src { get; set; }
            /// <summary>
            /// 苹果
            /// </summary>
            public string dst { get; set; }
        }

        public class Trans_Result
        {
            /// <summary>
            /// 
            /// </summary>
            public string from { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string to { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public List<Trans_ResultItem> trans_result { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string error_code { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string error_msg { get; set; }
        }
        // 计算MD5值
        public static string EncryptString(string str)
        {
            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();
        }
    }
}
