using CMSMicroservice.Protobuf.Protos.OtpToken;
using CMSMicroservice.Protobuf.Validator.OtpToken;
using BackOffice.Common.Utilities;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace BackOffice.Pages.Login;
public partial class LoginPage
{
    private bool _isLoading;
    private CreateNewOtpTokenRequest _request = new() { Purpose = "login" };
    private CreateNewOtpTokenRequestValidator _requestValidator = new();
    private MudForm _form;

    [Inject] public OtpTokenContract.OtpTokenContractClient OtpContract { get; set; }
    private async Task OnSubmitClick()
    {
        await _form.Validate();
        if (!_form.IsValid)
            return;

        _isLoading = true;
        StateHasChanged();

        _request.Mobile = _request.Mobile.PersianToEnglish();
        try
        {
            await OtpContract.CreateNewOtpTokenAsync(_request);
            Navigation.NavigateTo(RouteConstance.VerifyCodePage + _request.Mobile);
        }
        catch (Exception ex)
        {
            var message = ex.Message.ExtractUserFriendlyMessage();
            Snackbar.Add(message: message, severity: Severity.Error);
        }

        _isLoading = false;
        StateHasChanged();

    }
}
