using SimpleCore.Library;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Threading.Tasks;

namespace StandardScene.Utils
{
    public class WebAPIHelper
    {
        private static WebAPIHelper instance = null;

        public static WebAPIHelper Instance
        {
            get
            {
                return instance ?? (instance = new WebAPIHelper());
            }
        }

        private ConcurrentDictionary<string, HttpClient> clientPool = new ConcurrentDictionary<string, HttpClient>();

        private HttpClient getClient(string urlString)
        {
            var url = new Uri(urlString);
            var key = $"{url.Host}:{url.Port}";
            return clientPool.GetOrAdd(key, _ => createClient());
        }

        private HttpClient createClient()
        {
            var client = new HttpClient();
            client.Timeout = TimeSpan.FromSeconds(3);
            try
            {
                client.DefaultRequestHeaders.Add("User-Agent", "MDS/1.1");
                client.DefaultRequestHeaders.Add("Accept", "*/*");
            }
            catch (Exception e)
            {
                Diagnosis.Log($"fail to add http client default header:{e}", "task", true);
            }
            return client;
        }

        public async Task<string> GetStringAsync(string url)
        {
            return await getClient(url).GetStringAsync(url);
        }

        // /// <summary>
        // /// 读取HTTP Get响应包文。
        // /// </summary>
        // /// <typeparam name="T">返回结果的类型</typeparam>
        // /// <param name="uriString">资源地址</param>
        // /// <param name="headers">可选的HTTP头列表</param>
        // /// <returns>响应结果</returns>
        // public async Task<T> GetAsync<T>(string uriString, Dictionary<string, string> headers = null)
        // {
        //     T retv = default;
        //
        //     HttpClient hc = getClient(uriString);
        //     var request = new HttpRequestMessage(HttpMethod.Get, uriString);
        //     if (headers != null)
        //     {
        //         foreach (var key in headers.Keys)
        //         {
        //             request.Headers.Add(key, headers[key]);
        //         }
        //     }
        //     var resp = await hc.SendAsync(request);
        //     resp.EnsureSuccessStatusCode();
        //
        //     var opt = new JsonSerializerOptions
        //     {
        //         PropertyNameCaseInsensitive = true
        //     };
        //     opt.Converters.Add(new MyDateTimeConverter());
        //     var json = await resp.Content.ReadAsStringAsync();
        //     retv = JsonSerializer.Deserialize<T>(json, opt);
        //
        //     return retv;
        // }
        //
        // private static JsonSerializerOptions jsonOptions = new JsonSerializerOptions
        // {
        //     PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        //     WriteIndented = true
        // };

        // /// <summary>
        // /// 读取HTTP POST结果。
        // /// </summary>
        // /// <typeparam name="T1">响应结果类型</typeparam>
        // /// <typeparam name="T2">POST对象类型</typeparam>
        // /// <param name="uriString">资源地址</param>
        // /// <param name="data">请求包文对象</param>
        // /// <param name="headers">可选HTTP头列表</param>
        // /// <param name="logging">是否写入日志，默认为真</param>
        // /// <returns>响应对象</returns>
        // public async Task<T1> PostAsync<T1, T2>(string uriString, T2 data, Dictionary<string, string> headers = null, bool logging = true)
        // {
        //     //if (logging)
        //     Diagnosis.Log($"post to {uriString}\n{JsonSerializer.Serialize(data)}", "task", true);
        //     T1 retv = default;
        //
        //     HttpClient hc = getClient(uriString);
        //     var request = new HttpRequestMessage(HttpMethod.Post, uriString);
        //     if (headers != null)
        //     {
        //         foreach (var key in headers.Keys)
        //         {
        //             request.Headers.Add(key, headers[key]);
        //         }
        //     }
        //
        //     HttpContent content = new StringContent(JsonSerializer.Serialize(
        //         data,
        //         jsonOptions
        //     ));
        //     content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
        //     request.Content = content;
        //     var resp = await hc.SendAsync(request);
        //     resp.EnsureSuccessStatusCode();
        //     if (logging)
        //         Diagnosis.Post(JsonSerializer.Serialize(resp));
        //
        //     var opt = new JsonSerializerOptions
        //     {
        //         PropertyNameCaseInsensitive = true
        //     };
        //     opt.Converters.Add(new MyDateTimeConverter());
        //     var json = await resp.Content.ReadAsStringAsync();
        //     Diagnosis.Log($"got post response\n{json}", "task", true);
        //    
        //     if (resp.StatusCode != System.Net.HttpStatusCode.OK)
        //         Diagnosis.Log($"post {uriString} return not ok\nStatusCode={resp.StatusCode.ToString()}\njson", "task", true);
        //     retv = JsonSerializer.Deserialize<T1>(json, opt);
        //
        //     return retv;
        // }
    }

    // public class MyDateTimeConverter : JsonConverter<DateTime>
    // {
    //     public override bool CanConvert(Type typeToConvert)
    //     {
    //         return typeToConvert == typeof(DateTime);
    //     }
    //
    //     public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    //     {
    //         return DateTime.ParseExact(reader.GetString(), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
    //     }
    //
    //     public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    //     {
    //         writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
    //     }
    // }
}
