using TechHelper.Client.HttpRepository;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using System.Text.RegularExpressions;
using TechHelper.Features;
using Entities.Contracts;
using TechHelper.Client.Services;
using Entities.DTO;

namespace TechHelper.Client.Pages.Author
{
	public partial class Registration
	{

		private UserForRegistrationDto _userForRegistration = new UserForRegistrationDto();
		private GradeEnum grade = GradeEnum.Unknown;
		[Inject]
		public IAuthenticationClientService AuthenticationService { get; set; }

		[Inject]
		public NavigationManager NavigationManager { get; set; }
		[Inject]
		public IClassServices ClassServices { get; set; }


		public bool Basic_CheckBox2 { get; set; } = true;
		public byte selectclass { get; set; } = 0;
		public List<byte> Classes { get; set; } = new List<byte>();

		public bool ShowRegistrationErrors { get; set; }
		public string[] Errors { get; set; }
		private bool success;

		public async Task Register()
		{
			ShowRegistrationErrors = false;

			var result = await AuthenticationService.RegisterUserAsync(_userForRegistration);
			if (!result.IsSuccessfulRegistration)
			{
				int index = 0;
				foreach (var error in result.Errors)
				{
					Errors[index] = error;
					index++;
				}
				ShowRegistrationErrors = true;
				Snackbar.Add(Errors.ToString());
			}
			else
			{
				NavigationManager.NavigateTo("/");
			}
		}


		[Inject]
		public IEmailSender emailSender { get; set; }

		private async void HandleSelectedValuesChanged(GradeEnum selectedValues)
		{
			grade = selectedValues;
			Snackbar.Add("请等待,正在获取改年级班级", Severity.Info);
			var result = await ClassServices.GetGradeClasses((byte)selectedValues);
			if (result.Status)
			{
				Classes = result.Result as List<byte> ?? new List<byte>();
				Snackbar.Add("获取成功", Severity.Success);
				return;
			}
			Snackbar.Add("获取失败", Severity.Error);
		}
		private void HandleListSelectedValuesChanged(byte selectedValues)
		{
			selectclass = selectedValues;

		}

		public async void SendEmail()
		{
			string eamilTo = "1928360026@qq.com";
			string authCode = "123456";

			await emailSender.SendEmailAuthcodeAsync(eamilTo, authCode);
		}
	}
}