using RestSharp;
using Fw.Utility.UnifyResponse;
using System.Text;
using System.Security.Cryptography;
using Hotline.Share.Dtos.Quality;
using Hotline.Ai.Quality;

namespace Hotline.Ai.XingTang
{
    public class AiQualityService : IAiQualityService
    {
        private readonly RestClient _client;
        private readonly string _baseUrl;

        public AiQualityService(string baseUrl)
        {
            _client = new RestClient();
            _baseUrl = baseUrl;
        }

        public async Task CreateAiOrderQualityTask(
            string id,
            string audioFile,
            string fromNo,
            DateTime? callStartTime,
            string viteRecordPrefix,
            string ywlxString,
            CancellationToken cancellationToken)
        { 
        }


		public async Task<string> CreateAiOrderQualityTask(string filename, CancellationToken cancellationToken)
        {
            var url = _baseUrl + "/offlinerecog?filename=" + filename;
			var baseUrl = new Uri(url);
            return  await ExecuteAsync(baseUrl.ToString(), Method.Get, "", cancellationToken);
        }

        public async Task<ApiResponse<TResponse>> ExecuteAsync<TRequest, TResponse>(string path, Method httpMethod,
            TRequest request, CancellationToken cancellationToken)
            where TRequest : class
        {
            var req = new RestRequest(path, httpMethod);
            if (httpMethod is Method.Get)
            {
                req.AddObject(request);
            }
            else
            {
                req.AddJsonBody(request);
            }

            try
            {
                var response = await _client.ExecuteAsync<ApiResponse<TResponse>>(req, cancellationToken);
                return response.Data;
            }
            catch (Exception e)
            {
                throw new HttpRequestException($"智能质检平台错误，Error: {e.Message}");
            }
        }

        public async Task<string> ExecuteAsync<TRequest>(string path, Method httpMethod, TRequest request,
            CancellationToken cancellationToken)
            where TRequest : class
        {
            var req = new RestRequest(path, httpMethod);
            req.Timeout = new TimeSpan(0,30,0);

            try
            {
                var response = await _client.ExecuteAsync<ApiResponse>(req, cancellationToken);
                return response.Content;
            }
            catch (Exception e)
            {
                throw new HttpRequestException($"智能质检平台错误，Error: {e.Message}");
            }
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input">需要加密的字符串</param>
        /// <returns></returns>
        private static string MD5Encrypt(string? input)
        {
            using var md5 = MD5.Create();
            var t = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
            var sb = new StringBuilder(32);
            for (var i = 0; i < t.Length; i++)
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            return sb.ToString();
        }

        private static string Base64En(string? model)
        {
            var bytes = Encoding.UTF8.GetBytes(model);
            return Convert.ToBase64String(bytes);
        }
    }
}
