using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace QYZH.InteractiveMagazine.Common.Helpers
{
    /// <summary>
    /// HTTP请求工具类
    /// </summary>
    public static class HttpHelper
    {
        private static readonly HttpClient _httpClient = new HttpClient();

        /// <summary>
        /// 设置默认请求头
        /// </summary>
        static HttpHelper()
        {
            _httpClient.Timeout = TimeSpan.FromSeconds(30);
        }

        /// <summary>
        /// 发送GET请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <returns>响应内容字符串</returns>
        public static async Task<string> GetAsync(string url)
        {
            HttpResponseMessage response = await _httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 发送GET请求并反序列化为指定类型
        /// </summary>
        /// <typeparam name="T">响应类型</typeparam>
        /// <param name="url">请求地址</param>
        /// <returns>反序列化后的对象</returns>
        public static async Task<T?> GetAsync<T>(string url)
        {
            string json = await GetAsync(url);
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 发送POST请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="data">请求数据</param>
        /// <returns>响应内容字符串</returns>
        public static async Task<string> PostAsync(string url, object data)
        {
            string json = JsonConvert.SerializeObject(data);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await _httpClient.PostAsync(url, content);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 发送POST请求并反序列化为指定类型
        /// </summary>
        /// <typeparam name="T">响应类型</typeparam>
        /// <param name="url">请求地址</param>
        /// <param name="data">请求数据</param>
        /// <returns>反序列化后的对象</returns>
        public static async Task<T?> PostAsync<T>(string url, object data)
        {
            string json = await PostAsync(url, data);
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 发送DELETE请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <returns>响应内容字符串</returns>
        public static async Task<string> DeleteAsync(string url)
        {
            HttpResponseMessage response = await _httpClient.DeleteAsync(url);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 发送PUT请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="data">请求数据</param>
        /// <returns>响应内容字符串</returns>
        public static async Task<string> PutAsync(string url, object data)
        {
            string json = JsonConvert.SerializeObject(data);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await _httpClient.PutAsync(url, content);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }
    }
}
