using Entities.DTO;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using MudBlazor;
using System.Collections.Frozen;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
using TechHelper.Client.HttpRepository;
using TechHelper.Client.Services;

namespace TechHelper.Client.Pages.Manage
{
	public partial class Class
	{
		private UserRegistrationToClassDto _userRegistrationToClassDto = new UserRegistrationToClassDto();
		private string? username;
		private string? phoneNumber;

		[Inject]
		public ISnackbar Snackbar { get; set; }
		[Inject]
		public IAuthenticationClientService Authentication  { get; set; }
		[Inject]
		public IClassServices ClassServices { get; set; }

		[CascadingParameter]
		private Task<AuthenticationState> authenticationStateTask { get; set; }

		[SupplyParameterFromForm]
		private InputModel Input { get; set; } = new();

		protected override async Task OnInitializedAsync()
		{
			username = authenticationStateTask.Result.User.Identity.Name;
			phoneNumber = authenticationStateTask.Result.User.Identity.IsAuthenticated.ToString();

			Input.PhoneNumber ??= phoneNumber;
		}

		private async Task Register()
		{
			_userRegistrationToClassDto.User = authenticationStateTask.Result.User.Identity.Name?? "";
			var result = await ClassServices.UserRegister(_userRegistrationToClassDto);
			if (result.IsSuccessfulRegistration)
			{
				var token = await Authentication.RefreshTokenAsync();
				Snackbar.Add("°à¼¶×¢²á³É¹¦", Severity.Info);
			}
			else
			{
				Snackbar.Add(result.Errors.First(), Severity.Error);
			}

		}

		private sealed class InputModel
		{
			[Phone]
			[Display(Name = "Phone number")]
			public string? PhoneNumber { get; set; }
		}
	}
}