using Entities.DTO;
using System.Net.Http.Json;
using TechHelper.Client.AI;
using TechHelper.Services;

namespace TechHelper.Client.Services
{
	public class NoteService : INoteService
	{
		private readonly HttpClient _client;

		public NoteService(HttpClient client)
		{
			_client = client;
		}

		/// <summary>
		/// 添加一个新笔记
		/// </summary>
		/// <param name="dto">包含笔记数据的数据传输对象</param>
		/// <returns>操作结果</returns>
		public async Task<ApiResponse> AddNote(GlobalDto dto)
		{
			try
			{
				var response = await _client.PostAsJsonAsync("note", dto);

				if (response.IsSuccessStatusCode)
				{
					var result = await response.Content.ReadFromJsonAsync<ApiResponse>();
					return result ?? ApiResponse.Success("操作成功。");
				}
				else
				{
					var errorContent = await response.Content.ReadAsStringAsync();
					return ApiResponse.Error($"添加失败。状态码: {response.StatusCode}。详情: {errorContent}");
				}
			}
			catch (HttpRequestException ex)
			{
				return ApiResponse.Error($"网络请求错误: {ex.Message}");
			}
		}

		/// <summary>
		/// 根据 ID 删除一个笔记
		/// </summary>
		/// <param name="id">要删除的笔记的 ID</param>
		/// <returns>操作结果</returns>
		public async Task<ApiResponse> DeleteNote(byte id)
		{
			try
			{
				var response = await _client.DeleteAsync($"note/{id}");

				if (response.IsSuccessStatusCode)
				{
					var result = await response.Content.ReadFromJsonAsync<ApiResponse>();
					return result ?? ApiResponse.Success("删除成功。");
				}
				else
				{
					var errorContent = await response.Content.ReadAsStringAsync();
					return ApiResponse.Error($"删除失败。状态码: {response.StatusCode}。详情: {errorContent}");
				}
			}
			catch (HttpRequestException ex)
			{
				return ApiResponse.Error($"网络请求错误: {ex.Message}");
			}
		}

		/// <summary>
		/// 获取所有笔记
		/// </summary>
		/// <returns>包含所有笔记列表的操作结果</returns>
		public async Task<ApiResponse> GetAllNotes()
		{
			try
			{
				var response = await _client.GetAsync("note");

				if (response.IsSuccessStatusCode)
				{
					var result = await response.Content.ReadFromJsonAsync<ApiResponse>();
					return result ?? ApiResponse.Success("获取成功。");
				}
				else
				{
					var errorContent = await response.Content.ReadAsStringAsync();
					return ApiResponse.Error($"获取失败。状态码: {response.StatusCode}。详情: {errorContent}");
				}
			}
			catch (HttpRequestException ex)
			{
				return ApiResponse.Error($"网络请求错误: {ex.Message}");
			}
		}

		/// <summary>
		/// 根据 ID 获取单个笔记
		/// </summary>
		/// <param name="id">要获取的笔记的 ID</param>
		/// <returns>包含单个笔记数据的操作结果</returns>
		public async Task<ApiResponse> GetNote(byte id)
		{
			try
			{
				var response = await _client.GetAsync($"note/{id}");

				if (response.IsSuccessStatusCode)
				{
					var result = await response.Content.ReadFromJsonAsync<ApiResponse>();
					return result ?? ApiResponse.Success("获取成功。");
				}
				else
				{
					var errorContent = await response.Content.ReadAsStringAsync();
					return ApiResponse.Error($"获取失败。状态码: {response.StatusCode}。详情: {errorContent}");
				}
			}
			catch (HttpRequestException ex)
			{
				return ApiResponse.Error($"网络请求错误: {ex.Message}");
			}
		}

		public async Task<ApiResponse> UpdateNote(GlobalDto dto)
		{
			try
			{
				var response = await _client.PutAsJsonAsync("note", dto);

				if (response.IsSuccessStatusCode)
				{
					var result = await response.Content.ReadFromJsonAsync<ApiResponse>();
					return result ?? ApiResponse.Success("更新成功。");
				}
				else
				{
					var errorContent = await response.Content.ReadAsStringAsync();
					return ApiResponse.Error($"更新失败。状态码: {response.StatusCode}。详情: {errorContent}");
				}
			}
			catch (HttpRequestException ex)
			{
				return ApiResponse.Error($"网络请求错误: {ex.Message}");
			}
		}
	}
}
