using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.Tools;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Ashermed.BaseFramework;
using ASHermed.SinoInterface.IFramework;
using ASHermed.SinoInterface.Api.ContractModel.App;
using Microsoft.Extensions.Logging;
using PoJun.Util.Webs.Clients;
using System.Net.Http;

namespace ASHermed.SingleSignOnFramework
{
    /// <summary>
	/// TC 转TCcervice里
	/// </summary>
	public class RbAptService : IRbAptService
    {
        AppConfigurtaion appConfigurtaion;
        private IHttpClientFactory _httpClientFactory;
        ILogger<TencentOutService> logger;
        public RbAptService( AppConfigurtaion _appConfigurtaion,
            ILogger<TencentOutService> _logger, IHttpClientFactory httpClientFactory)
        {
            appConfigurtaion = _appConfigurtaion;
            logger = _logger;
            _httpClientFactory = httpClientFactory;
        }
        /// <summary>
        /// GET请求[异步]
        /// </summary>
        /// <typeparam name="T">只能传入DTO对象</typeparam>
        /// <param name="host">域名</param>
        /// <param name="apiName">接口名称(接口地址)</param>
        /// <param name="headers">请求头</param>
        /// <param name="timeout">请求响应超时时间，单位/s(默认100秒)</param>
        /// <returns></returns>
        public async Task<T> HttpGetAsync<T>(string host, string apiName, Dictionary<string, string> headers = null, int timeout = 100)
        {
            var client = _httpClientFactory.CreateClient("base");
            {
                #region Http基础设置

                //设置HTTP请求头
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> header in headers)
                    {
                        client.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                }
                //设置接口超时时间
                if (timeout > 0)
                {
                    client.Timeout = new TimeSpan(0, 0, timeout);
                }

                #endregion

                #region 新增请求日志

                //var log_request_param = new AddRequestLogParam();
                //log_request_param.APIName = apiName.ToString().ToLower();
                //log_request_param.ClientHost = SysUtil.Ip;
                //log_request_param.RequestTime = DateTime.Now;
                //log_request_param.ServerHost = host;
                //log_request_param.SystemID = SysUtil.GetSystemId();
                //log_request_param.TraceID = PoJun.Util.Helpers.Id.GetGuidBy32();
                //log_request_param.Level = 2;
                //log_request_param.ParentTraceID = SysUtil.GetTraceId();
                //log_request_param.RequestBody = null;
                //if (SysUtil.GetTraceId() != null)
                //{
                //    log_request_param.Level = 2;
                //    log_request_param.ParentTraceID = SysUtil.GetTraceId();
                //}
                //else
                //{
                //    log_request_param.Level = 1;
                //}
                //var log_request_result = await apiLogService.AddRequestLogAsync(log_request_param);

                #endregion

                #region 发起Http请求

                var url = $"{host}{apiName}";
                Byte[] resultBytes = await client.GetByteArrayAsync(url);
                var result = Encoding.UTF8.GetString(resultBytes);

                #endregion

                #region 新增响应日志

                //var log_response_param = new AddResponseLogParam();
                //log_response_param.IsError = false;
                //log_response_param.ParentTraceID = log_request_param.TraceID;
                //log_response_param.ResponseBody = result;
                //log_response_param.ResponseTime = DateTime.Now;
                //log_response_param.TimeCost = Convert.ToInt32((log_response_param.ResponseTime - log_request_param.RequestTime).TotalMilliseconds);

                //await apiLogService.AddResponseLogAsync(log_response_param);

                #endregion

                #region 返回结果

                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(result);

                #endregion
            }
        }

        /// <summary>
        /// Post请求[异步]
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="host">域名</param>
        /// <param name="apiName">接口名称(接口地址)</param>
        /// <param name="dicParameters">接口参数</param>
        /// <param name="headers">请求头</param>
        /// <param name="timeout">请求响应超时时间，单位/s(默认100秒)</param>
        /// <param name="contentType">请求类型</param>
        /// <param name="isMultilevelNestingJson">是否为多层嵌套json</param>
        /// <returns></returns>
        public async Task<T> HttpPostAsync<T>(string host, string apiName, IDictionary<string, string> dicParameters, Dictionary<string, string> headers = null, int timeout = 100, HttpContentType contentType = HttpContentType.Json, bool isMultilevelNestingJson = false)
        {
            var client = _httpClientFactory.CreateClient("base");
            {
                #region Http基础设置

                //设置接口超时时间
                if (timeout > 0)
                {
                    client.Timeout = new TimeSpan(0, 0, timeout);
                }

                HttpContent content = null;
                var _contentType = (contentType == HttpContentType.Json ? "application/json" : "application/x-www-form-urlencoded");
                if (contentType == HttpContentType.Json)
                {
                    if (isMultilevelNestingJson)
                        content = new StringContent(dicParameters["json"]);
                    else
                        content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(dicParameters));
                }
                else
                {
                    content = new FormUrlEncodedContent(dicParameters);
                }
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(_contentType);
                //设置HTTP请求头
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> header in headers)
                    {
                        content.Headers.Add(header.Key, header.Value);
                    }
                }

                #endregion

                #region 新增请求日志

                //var log_request_param = new AddRequestLogParam();
                //log_request_param.APIName = apiName.ToString().ToLower();
                //log_request_param.ClientHost = SysUtil.Ip;
                //log_request_param.RequestTime = DateTime.Now;
                //log_request_param.ServerHost = host;
                //log_request_param.SystemID = SysUtil.GetSystemId();
                //log_request_param.TraceID = PoJun.Util.Helpers.Id.GetGuidBy32();
                //log_request_param.Level = 2;
                //log_request_param.ParentTraceID = SysUtil.GetTraceId();
                //log_request_param.RequestBody = Newtonsoft.Json.JsonConvert.SerializeObject(dicParameters);
                //if (SysUtil.GetTraceId() != null)
                //{
                //    log_request_param.Level = 2;
                //    log_request_param.ParentTraceID = SysUtil.GetTraceId();
                //}
                //else
                //{
                //    log_request_param.Level = 1;
                //}
                //var log_request_result = await apiLogService.AddRequestLogAsync(log_request_param);

                #endregion

                #region 发起Http请求

                var url = $"{host}{apiName}";
                //异步发送请求
                var events = await client.PostAsync(url, content);
                //异步获取请求的结果
                var strResult = await events.Content.ReadAsStringAsync();

                #endregion

                #region 新增响应日志

                //var log_response_param = new AddResponseLogParam();
                //log_response_param.IsError = false;
                //log_response_param.ParentTraceID = log_request_param.TraceID;
                //log_response_param.ResponseBody = strResult;
                //log_response_param.ResponseTime = DateTime.Now;
                //log_response_param.TimeCost = Convert.ToInt32((log_response_param.ResponseTime - log_request_param.RequestTime).TotalMilliseconds);

                //await apiLogService.AddResponseLogAsync(log_response_param);

                #endregion

                #region 返回结果

                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(strResult);

                #endregion
            }
        }


    }
}
