using CMSMicroservice.Protobuf.Protos.UserAddress;
using BackOffice.Common.BaseComponents;
using BackOffice.Pages.UserAddress.Components;
using Mapster;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using DataModel = CMSMicroservice.Protobuf.Protos.UserAddress.GetAllUserAddressByFilterResponseModel;

namespace BackOffice.Pages.UserAddress;
public partial class UserAddressMainPage
{
    [Parameter] public long UserId { get; set; }
    [Inject] public UserAddressContract.UserAddressContractClient UserAddressContract { get; set; }
    private bool _isLoading = true;
    private MudDataGrid<DataModel> _gridData;
    BasePageComponent _basePage;
    private GetAllUserAddressByFilterRequest _request = new() { Filter = new() };
    private async Task<GridData<DataModel>> ServerReload(GridState<DataModel> state)
    {
        _request.Filter ??= new();
        _request.PaginationState ??= new();
        _request.PaginationState.PageNumber = state.Page + 1;
        _request.PaginationState.PageSize = state.PageSize;

        _request.Filter.UserId = UserId;


        var result = await UserAddressContract.GetAllUserAddressByFilterAsync(_request);
        if (result != null && result.Models != null && result.Models.Any())
        {
            return new GridData<DataModel>() { Items = result.Models.ToList(), TotalItems = (int)result.MetaData.TotalCount };
        }

        return new GridData<DataModel>();
    }
    public async Task Update(DataModel model)
    {
        var parameters = new DialogParameters<UpdateDialog> { { x => x.Model, model.Adapt<UpdateUserAddressRequest>() } };

        var dialog = await DialogService.ShowAsync<UpdateDialog>($"ویرایش آدرس", parameters, new DialogOptions() { CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.Small });
        var result = await dialog.Result;

        if (!result.Canceled)
        {
            ReLoadData();
            //Reload Data
            Snackbar.Add("عملیات با موفقیت انجام شد", Severity.Success);

        }
    }
    private async Task OnDelete(DataModel model)
    {
        var options = new DialogOptions { CloseOnEscapeKey = true, MaxWidth = MaxWidth.Small };
        bool? result = await DialogService.ShowMessageBox(
            "اخطار",
            $"آیا از حذف آدرس «{model.Title}» مطمئن هستید؟",
            yesText: "حذف", cancelText: "لغو",
            options: options);
        if (result != null && result.Value)
        {
            await UserAddressContract.DeleteUserAddressAsync(new()
            {
                Id = model.Id
            });
            ReLoadData();
        }
        StateHasChanged();
    }
    public async void ReLoadData()
    {
        if (_gridData != null)
            await _gridData.ReloadServerData();
    }
    public async Task CreateNew()
    {
        var dialog = await DialogService.ShowAsync<CreateDialog>($"افزودن آدرس", new DialogParameters<CreateDialog>() { { x => x.Model, new CreateNewUserAddressRequest() { UserId = UserId } } }, new DialogOptions() { CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.Small });
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            ReLoadData();
            Snackbar.Add("عملیات با موفقیت انجام شد", Severity.Success);
        }
    }
    public async Task OnFilterSubmit()
    {
        _basePage.IsFiltered = true;
        StateHasChanged();
        ReLoadData();
    }

    public async Task OnFilterCleared()
    {
        _basePage.IsFiltered = false;
        StateHasChanged();
        _request = new() { Filter = new() { } };
        ReLoadData();
    }
}
