using CMSMicroservice.Protobuf.Protos.UserRole;
using Google.Protobuf;
using Grpc.Core;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using MudBlazor;

namespace BackOffice.Pages.UserRole.Components;

public partial class CreateDialog
{
    [Inject] public UserRoleContract.UserRoleContractClient UserRoleContract { get; set; }
    [Parameter] public CreateNewUserRoleRequest Model { get; set; } = new();
    [CascadingParameter] IMudDialogInstance MudDialog { get; set; }

    private bool _isLoading = false;

    public async void CallCreateMethod()
    {
        _isLoading = true;
        StateHasChanged();

        if (Model.RoleId <= 0)
        {
            Snackbar.Add("لطفا نقش را وارد کنید!", Severity.Warning);
            _isLoading = false;
            StateHasChanged();
            return;
        }
        try
        {
            await UserRoleContract.CreateNewUserRoleAsync(Model);
            Submit();

        }
        catch (RpcException ex)
        {
            Snackbar.Add("خطای سرور", Severity.Error);
        }

        _isLoading = false;
        StateHasChanged();
    }
    void Submit() => MudDialog.Close(DialogResult.Ok(true));
    void Cancel() => MudDialog.Cancel();
}