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

namespace BackOffice.Pages.UserAddress.Components
{
    public partial class UpdateDialog
    {
        private bool _isLoading = false;
        [Inject] public UserAddressContract.UserAddressContractClient UserAddressContract { get; set; }

        [Parameter]
        public UpdateUserAddressRequest 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.Address))
            {
                Snackbar.Add("لطفا ادرس را وارد کنید!", Severity.Warning);
                _isLoading = false;
                StateHasChanged();
                return;
            }
            if (string.IsNullOrWhiteSpace(Model.PostalCode))
            {
                Snackbar.Add("لطفا کد پستی را وارد کنید!", Severity.Warning);
                _isLoading = false;
                StateHasChanged();
                return;
            }

            try
            {
                await UserAddressContract.UpdateUserAddressAsync(Model);
                Submit();

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

            _isLoading = false;
            StateHasChanged();
        }

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