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



namespace TechHelper.Client.Pages.Author
{
	public partial class ResetPassword : ComponentBase
	{
		private readonly ResetPasswordDto _resetPassDto = new ResetPasswordDto();

		private bool _showSuccess;
		private bool _showError;
		private IEnumerable<string> _errors;


		[Inject]
		public IAuthenticationClientService AuthenticationClientService { get; set; }
		[Inject]
		public NavigationManager NavigationManager { get; set; }

		protected override void OnInitialized()
		{
			var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
			var queryStrings = QueryHelpers.ParseQuery(uri.Query);
			if (queryStrings.TryGetValue("email", out var email) &&
				queryStrings.TryGetValue("token", out var token))
			{
				_resetPassDto.Email = email;
				_resetPassDto.Token = token;
			}
			else
			{
				NavigationManager.NavigateTo("/");
			}
		}

		private async Task Submit()
		{
			_showSuccess = _showError = false;
			var result = await AuthenticationClientService.ResetPasswordAsync(_resetPassDto);

			if(result.IsResetPasswordSuccessful)
				_showSuccess = true;
			else
			{
				_errors = result.Errors;
				_showError = true;	
			}
			NavigationManager.NavigateTo("/");
		}
	}
}