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

namespace BackOffice.Pages.Role;
public partial class RoleMainPage
{
    BasePageComponent _basePage;

    public async Task CreateNew()
    {
        var dialog = await DialogService.ShowAsync<CreateDialog>($"افزودن نقش", new DialogParameters<CreateDialog>() { { x => x.Model, new CreateNewRoleRequest() } }, new DialogOptions() { CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.Small });
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            ReLoadData();
            Snackbar.Add("عملیات با موفقیت انجام شد", Severity.Success);
        }
    }
    [Inject] public RoleContract.RoleContractClient RoleContract { get; set; }
    private bool _isLoading = true;
    private MudDataGrid<DataModel> _gridData;

    private GetAllRoleByFilterRequest _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;

        var result = await RoleContract.GetAllRoleByFilterAsync(_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<UpdateRoleRequest>() } };

        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 RoleContract.DeleteRoleAsync(new()
            {
                Id = model.Id
            });
            ReLoadData();
        }
        StateHasChanged();
    }
    public async void ReLoadData()
    {
        if (_gridData != null)
            await _gridData.ReloadServerData();
    }
    public async Task OnFilterSubmit()
    {
        _basePage.IsFiltered = true;
        StateHasChanged();
        ReLoadData();
    }

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

    private async Task EditPermissions(DataModel model)
    {
        var parameters = new DialogParameters<RolePermissionsDialog>
        {
            { x => x.RoleName, model.Name }
        };

        var options = new DialogOptions
        {
            CloseButton = true,
            FullWidth = true,
            MaxWidth = MaxWidth.Medium
        };

        var dialog = await DialogService.ShowAsync<RolePermissionsDialog>($"دسترسی‌های نقش {model.Title}", parameters, options);
        await dialog.Result;
    }
}
