<?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
 *
 * @package App\Http\Livewire\Frontend\Informacia\Components
 */
final class Zoznam extends Component
{
    use WithPagination;

    /**
     *
     */
    private const PER_PAGE = 15;

    /**
     * @var int
     */
    public int $sort = 0;

    /**
     * @var string
     */
    protected string $paginationTheme = 'bootstrap';

    /**
     * @var \App\Repositories\InformaciaRepositoryContract
     */
    protected InformaciaRepositoryContract $informaciaRepository;

    /**
     * @param \App\Repositories\InformaciaRepositoryContract $informaciaRepository
     *
     * @return void
     */
    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);
    }
}
