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

namespace BackOffice.Pages.Package;
public partial class PackageMainPage
{
    [Inject] public PackageContract.PackageContractClient PackageContract { get; set; }
    private bool _isLoading = true;
    private MudDataGrid<DataModel> _gridData;
    BasePageComponent _basePage;
    private GetAllPackageByFilterRequest _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 PackageContract.GetAllPackageByFilterAsync(_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<UpdatePackageRequest>() } };

        var dialog = await DialogService.ShowAsync<UpdateDialog>($"ویرایش پکیج", parameters, new DialogOptions() { CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.Medium });
        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 PackageContract.DeletePackageAsync(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 CreateNewPackageRequest() { IsActive = true, SupportsDirectPurchase = true, DiscountMultiplier = 2.0, MagicWalletMultiplier = 2.5, MaxBalancesPerLeg = 300, MaxNetworkLevel = 15, MagicWalletMaxDeposit = 1_000_000_000, MagicWalletMaxCredit = 2_500_000_000 } } }, new DialogOptions() { CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.Medium });
        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();
    }
}
