using Entities.DTO;
using Newtonsoft.Json;
using System.Net.Http.Json;
using TechHelper.Client.HttpRepository;

namespace TechHelper.Client.Services
{
	public class ClasssServices : IClassServices
	{
		private readonly HttpClient _client;
		private readonly IAuthenticationClientService _authenticationClientService;

		public ClasssServices(HttpClient client, IAuthenticationClientService authenticationClientService)
		{
			_client = client;
			_authenticationClientService = authenticationClientService;
		}

		public Task<ResponseDto> CreateClass(UserRegistrationToClassDto userClass)
		{
			try
			{
				return Task.FromResult(new ResponseDto { IsSuccessfulRegistration = true });	
			}
			catch (Exception ex)
			{
				// 实际应用中，这里应该加入日志记录
				Console.WriteLine($"Error in CreateClass: {ex.Message}");
				return Task.FromResult(new ResponseDto { IsSuccessfulRegistration = false, Errors = new string[] { ex.Message } });
			}
		}

		public async Task<ApiResponse> GetClassStudents()
		{
			try
			{
				var result = await _client.PostAsJsonAsync("class/getClassStudents","");
				var content = await result.Content.ReadAsStringAsync();
				var users = JsonConvert.DeserializeObject<List<StudentExamDetailDto>>(content);
				return ApiResponse.Success(result: users);
			}
			catch(Exception ex)
			{
				return ApiResponse.Error($"获取失败,{ex.Message}, InnerException: {ex.InnerException}");
			}
		}

		public async Task<ApiResponse> GetGradeClasses(byte grade)
		{
			try
			{
				var result = await _client.PostAsJsonAsync("class/GetGradeClasses", grade);
				var content = await result.Content.ReadAsStringAsync();
				Console.WriteLine($"服务器返回的原始内容是: {content}");
				var users = JsonConvert.DeserializeObject<List<byte>>(content);
				return ApiResponse.Success(result: users);
			}
			catch (Exception ex)
			{
				return ApiResponse.Error($"获取失败,{ex.Message}, InnerException: {ex.InnerException}");
			}
		}

		public StudentExamDetailDto GetStudents(byte Class)
		{
			try
			{
				var result = _client.PostAsJsonAsync("class/getClassStudents", Class);
				var content = result.Result.Content.ReadAsStringAsync();
				if (content.Result != null)
				{
					var users = JsonConvert.DeserializeObject<StudentExamDetailDto>(content.Result);
					return users;
				}
				else
				{
					return new StudentExamDetailDto();
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine($"获取失败,{ex.Message}, InnerException: {ex.InnerException}");
				return new StudentExamDetailDto();
			}
		}

		public async Task<ResponseDto> UserRegister(UserRegistrationToClassDto userRegistrationToClassDto)
		{
			try
			{
				var result = await _client.PostAsJsonAsync("class/userRegiste",
					userRegistrationToClassDto);
				var data = await result.Content.ReadAsStringAsync();

				await _authenticationClientService.RefreshTokenAsync();

				return new ResponseDto
				{
					IsSuccessfulRegistration = result.IsSuccessStatusCode,
					Errors = result.IsSuccessStatusCode ? null : new string[] { data }
				};
			}
			catch (Exception ex)
			{
				// 实际应用中，这里应该加入日志记录
				Console.WriteLine($"Error in UserRegister: {ex.Message}");
				return new ResponseDto { IsSuccessfulRegistration = false, Errors = new string[] { ex.Message } };
			}
		}
	}
}