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

namespace BackOffice.Pages.UserRole;
public partial class UserRoleMainPage
{
    [Parameter] public long UserId { get; set; }
    [Inject] public UserRoleContract.UserRoleContractClient UserRoleContract { get; set; }
    private bool _isLoading = true;
    private MudDataGrid<DataModel> _gridData;
    BasePageComponent _basePage;
    private GetAllUserRoleByFilterRequest _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 UserRoleContract.GetAllUserRoleByFilterAsync(_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<UpdateUserRoleRequest>() } };

        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.Id}» مطمئن هستید؟",
            yesText: "حذف", cancelText: "لغو",
            options: options);
        if (result != null && result.Value)
        {
            await UserRoleContract.DeleteUserRoleAsync(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 CreateNewUserRoleRequest() { 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();
    }
}
