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 Newtonsoft.Json;
using ASHermed.SinoInterface.Api.ContractModel;
using System.Net.Http;
using System.Net;
using System.Xml;

namespace ASHermed.SingleSignOnFramework
{
    /// <summary>
	/// 外部接口服务
	/// </summary>
	public class OutService : IOutService
    {
        private string fileDownloadUrl;
        ILogger<OutService> _logger;
        HttpClient _httpClient;
        HttpClientHelper _httpClientHelper;
        /// <summary>
        /// 外部接口服务
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="httpClientHelper"></param>

        public OutService(ILogger<OutService> logger, IHttpClientFactory httpClientFactory, HttpClientHelper httpClientHelper)
        {
            fileDownloadUrl = "cf/fileDownload.do";
            _logger = logger;
            _httpClient = httpClientFactory.CreateClient(HttpClientConst.CFFileDown_CLIENT_NAME);
            _httpClientHelper = httpClientHelper;
        }

        public string HttpPost(string url, string data)
        {
            
            var result = _httpClientHelper.HttpPost<object>(url, data);
            return result?.ToJson();
        }

        public T HttpPost<T>(string url, string data)
        {
            var result = _httpClientHelper.HttpPost<T>(url, data);
            return result;
        }

        public string HttpGet(string url)
        {
            var result = _httpClientHelper.HttpGet(url);
            return result;
        }
        
        public T HttpGet<T>(string url)
        {
            var result = _httpClientHelper.HttpGet<T>(url);
            return result;
        }
        public async Task<Meta> Post(string url, Dictionary<string, object> dic)
        {
            Meta res = new Meta();
            XmlDocument resXml = new XmlDocument();
            var resStr = string.Empty;
            var data = string.Empty;
            foreach (var item in dic)
            {
                if (string.IsNullOrEmpty(data))
                {
                    data += item.Key + "=" + item.Value;
                }
                else
                {
                    data += "&" + item.Key + "=" + item.Value;
                }
            }
            var hc = new StringContent(data, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
            var response = await _httpClient.PostAsync(fileDownloadUrl, hc);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                resStr = response.Content.ReadAsStringAsync().Result.Replace("&lt;", "<").Replace("&gt;", ">");
                var begin = resStr.IndexOf("<Meta>");
                if (begin < 0)
                {
                    return res;
                }
                var end = resStr.IndexOf("</Meta>") + 7;
                var a = resStr.Substring(begin, resStr.Length - begin - (resStr.Length - end));
                resXml.LoadXml(a);
                res.UpdateDate = resXml.SelectSingleNode("Meta/UpdateDate")?.InnerText;
                res.PublicationDate = resXml.SelectSingleNode("Meta/PublicationDate")?.InnerText;
                res.ContentType = resXml.SelectSingleNode("Meta/ContentType")?.InnerText;
                res.Title = resXml.SelectSingleNode("Meta/Title")?.InnerText;
                res.Abstract = resXml.SelectSingleNode("Meta/Abstract")?.InnerText;

                begin = resStr.IndexOf("<Body>");
                if (begin < 0)
                {
                    return res;
                }
                end = resStr.IndexOf("</Body>") + 7;
                var b = resStr.Substring(begin, resStr.Length - begin - (resStr.Length - end));
                resXml.LoadXml(b);
                XmlNodeList list = resXml.SelectNodes("Body/Section");
                List<Section> section = new List<Section>();
                foreach (XmlNode item in list)
                {
                    Section model = new Section();
                    foreach (XmlNode item1 in item)
                    {
                        switch (item1.Name)
                        {
                            case "SectionHead":
                                model.SectionHead = item1?.InnerText;
                                break;
                            case "HTML":
                                model.HTML = item1?.InnerXml;
                                break;
                                //case "HTML":
                                //    model.PopupMedia = item1?.InnerXml;
                                //    break;
                        }
                    }
                    section.Add(model);
                }
                res.Body.Section = section;
            }
            return res;
        }






    }
}
