using CMSMicroservice.Protobuf.Protos.Role;
using Google.Protobuf;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using MudBlazor;

namespace BackOffice.Pages.Role.Components
{
    public partial class UpdateDialog
    {
        private bool _isLoading = false;
        [Inject] public RoleContract.RoleContractClient RoleContract { get; set; }

        [Parameter]
        public UpdateRoleRequest Model { get; set; } = new();

        [CascadingParameter]
        IMudDialogInstance MudDialog { get; set; }
    
        public async void CallUpdateMethod()
        {
            _isLoading = true;
            StateHasChanged();
            if (string.IsNullOrWhiteSpace(Model.Title))
            {
                Snackbar.Add("لطفا عنوان را وارد کنید!", Severity.Warning);
                _isLoading = false;
                StateHasChanged();
                return;
            }
            if (string.IsNullOrWhiteSpace(Model.Name))
            {
                Snackbar.Add("لطفا عنوان لاتین را وارد کنید!", Severity.Warning);
                _isLoading = false;
                StateHasChanged();
                return;
            }
           

            try
            {
                await RoleContract.UpdateRoleAsync(Model);
                Submit();

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

            _isLoading = false;
            StateHasChanged();
        }

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