@if (this.productCards == null)
{
    <div class="loader"></div>
}
else
{
    <div class="latest_product_inner">
        <div class="row">
            @foreach (var product in this.productCards.Skip(this.skipQuantity).Take(this.takeQuantity))
            {
                <ProductCard Card="@product"></ProductCard>
            }
        </div>
    </div>
    <br />
    <Pagination TotaPagesQuantity="this.TotalPagesQuantity"
                CurrentPage="this.currentPage"
                Radius="GlobalConstants.PaginationRadius"
                SelectedPage="this.SelectedPage">
    </Pagination>
}

@code {
    private int currentPage = 1;
    private int skipQuantity = 0;
    private int takeQuantity = 6;
    private List<ProductCardViewModel> productCards;

    [Parameter]
    public string Id { get; set; }

    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public string Sorting { get; set; }

    [Parameter]
    public int TotalPagesQuantity { get; set; }

    [Parameter]
    public int ProductsForShowing { get; set; }

    private void SelectedPage(int page)
    {
        this.currentPage = page;
        this.skipQuantity = (this.currentPage - 1) * this.takeQuantity;
    }

    protected override async Task OnParametersSetAsync()
    {
        if (Id == null)
        {
            this.productCards = await this.ProductService.ExtractAllProducts(this.Sorting);
        }
        else
        {
            this.productCards = await this.ProductService.ExtractProductsByCategoryId(this.Id, this.Sorting);
        }

        this.takeQuantity = this.ProductsForShowing;
        this.skipQuantity = 0;
        this.TotalPagesQuantity = Math.Max(1, (int)Math.Ceiling(this.productCards.Count / (decimal)this.takeQuantity));
        this.currentPage = 1;
    }
}