using MySql.Data.MySqlClient.Memcached;
using Org.BouncyCastle.Asn1.Crmf;
using RestSharp;
using System.IO;

namespace OASystem.API.OAMethodLib.BaiduApi
{
    public class BaiduApiHelper
    {
        const string API_KEY = "0OuunXiLEX3OL6hB4bhXvKyS";
        const string SECRET_KEY = "GZ6HwIGxQlGH1O4MADm0DpO5BcLyhpx2";

        public static string _apiTableV2(string Base64)
        {
            var client = new RestClient($"https://aip.baidubce.com/rest/2.0/ocr/v1/table?access_token={GetAccessToken()}");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            request.AddHeader("Accept", "application/json");
            // image 可以通过 GetFileBase64Content('C:\fakepath\tb.jpg') 方法获取
            request.AddParameter("image", Base64);
            IRestResponse response = client.Execute(request);
            return response.Content;
        }

        /**
        * 使用 AK，SK 生成鉴权签名（Access Token）
        * @return 鉴权签名信息（Access Token）
        */
        static string GetAccessToken()
        {
            var client = new RestClient($"https://aip.baidubce.com/oauth/2.0/token");
            //client.Timeout = -1;
            var request = new RestRequest("",Method.POST);
            request.AddParameter("grant_type", "client_credentials");
            request.AddParameter("client_id", API_KEY);
            request.AddParameter("client_secret", SECRET_KEY);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);
            var result = JsonConvert.DeserializeObject<dynamic>(response.Content);
            return result.access_token.ToString();
        }
    }
}
