using Entities.DTO;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.WebUtilities;
using TechHelper.Client.HttpRepository;

namespace TechHelper.Client.Pages.Author
{
	public partial class TwoStepVerification : ComponentBase
	{
		private TwoFactorVerificationDto _twoFactorVerificationDto = new TwoFactorVerificationDto();

		private bool _showSuccess;
		private string? _error;
		private bool _showError;
		[Inject]
		public IAuthenticationClientService AuthenticationClientService { get; set; }
		[Inject]
		public NavigationManager NavigationManager { get; set; }


		[SupplyParameterFromQuery]
		[Parameter]
		public string? Email { get; set; }


		[SupplyParameterFromQuery]
		[Parameter]
		public string? Provider { get; set; }


		protected override void OnInitialized()
		{
			if(string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(Provider))
			{

				NavigationManager.NavigateTo("/");
			}
			else
			{
				_twoFactorVerificationDto.Email = Email;
				_twoFactorVerificationDto.Provider = Provider;
			}
		}

		private async Task Submit()
		{
			_showError = false;


			var result = await AuthenticationClientService.LoginVerfication(_twoFactorVerificationDto);
			if(result.IsAuthSuccessful) 
				NavigationManager.NavigateTo("/");
			else
			{
				_error = result.ErrorMessage;
				_showError = true;
			}
		}
	}
}