<?php

namespace App\Http\Livewire\Frontend\Informacia\Components;

use App\Repositories\Eloquent\Criteria\AnyRolePouzivatelaCriteria;
use App\Repositories\Eloquent\Criteria\OrderByCasZverejneniaAscCriteria;
use App\Repositories\Eloquent\Criteria\OrderByCasZverejneniaDescCriteria;
use App\Repositories\Eloquent\Criteria\ZverejneneCriteria;
use App\Repositories\InformaciaRepositoryContract;
use Livewire\Component;
use Livewire\WithPagination;
use Prettus\Repository\Exceptions\RepositoryException;

/**
 * Class Zoznam
 */
final class Zoznam extends Component
{
    use WithPagination;

    private const PER_PAGE = 15;

    public int $sort = 0;

    protected string $paginationTheme = 'bootstrap';

    protected InformaciaRepositoryContract $informaciaRepository;

    public function boot(InformaciaRepositoryContract $informaciaRepository): void
    {
        $this->informaciaRepository = $informaciaRepository;
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     *
     * @throws RepositoryException
     */
    public function render()
    {
        $informacie = $this->informaciaRepository
            ->pushCriteria(ZverejneneCriteria::class)
            ->pushCriteria(AnyRolePouzivatelaCriteria::class)
            ->pushCriteria(
                ($this->sort == 1) ? OrderByCasZverejneniaAscCriteria::class : OrderByCasZverejneniaDescCriteria::class,
            )
            ->paginate(self::PER_PAGE);

        return view('livewire.frontend.informacie.zoznam')->withInformacie($informacie);
    }
}
