<?php

namespace App\Service;

use App\Entity\FileAssociationComment;
use App\Service\DataGrid\DataGridInterface;
use Doctrine\ORM\EntityNotFoundException;

final class FileAssociationCommentTaskService
{
    /**
     * @var FileAssociationCommentTaskRepositoryInterface
     */
    private $repository;

    /**
     * FileAssociationCommentTaskService constructor.
     *
     * @param FileAssociationCommentTaskRepositoryInterface $repository
     */
    public function __construct(FileAssociationCommentTaskRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

    /**
     * @param int $id
     *
     * @return FileAssociationComment
     *
     * @throws EntityNotFoundException
     */
    public function get(int $id): FileAssociationComment
    {
        $fileAssociationCommentTask = $this->repository->findById($id);
        if (!$fileAssociationCommentTask) {
            throw new EntityNotFoundException('FileAssociationComment with id '.$id.' does not exist!');
        }

        return $fileAssociationCommentTask;
    }

    /**
     * @return array|null
     */
    public function getAll(): ?array
    {
        return $this->repository->findAll();
    }

    /**
     * @return array|null
     */
    public function getByGrid(DataGridInterface $dataGrid): ?array
    {
        $criteria = $this->getCriteria($dataGrid);

        return $this->repository->findBy(
            $criteria,
            [
                $dataGrid->getSorters()->first()->getSort() => $dataGrid->getSorters()->first()->getOrder(),
            ],
            $dataGrid->getNavigation()->getRpp(),
            $dataGrid->getNavigation()->getRpp() * $dataGrid->getNavigation()->getPage());
    }

    /**
     * @param DataGridInterface $dataGrid
     *
     * @return array
     */
    private function getCriteria(DataGridInterface $dataGrid): array
    {
        $criteria = [];
        if ($dataGrid->getFilters()->containsKey('q') && $dataGrid->getFilters()->get('q')->isValid()) {
            $criteria += ['q' => $dataGrid->getFilters()->get('q')->getValue()];
        }

        if ($dataGrid->getFilters()->containsKey('comment') && $dataGrid->getFilters()->get('comment')->isValid()) {
            $criteria += ['comment' => $dataGrid->getFilters()->get('comment')->getValue()];
        }

        return $criteria;
    }

    /**
     * @param DataGridInterface $dataGrid
     *
     * @return int
     */
    public function countByGrid(DataGridInterface $dataGrid): int
    {
        $criteria = $this->getCriteria($dataGrid);

        return $this->repository->count($criteria);
    }

    /**
     * @param FileAssociationComment $fileAssociationCommentTask
     *
     * @return FileAssociationComment
     */
    public function save(FileAssociationComment $fileAssociationCommentTask): FileAssociationComment
    {
        $this->repository->save($fileAssociationCommentTask);

        return $fileAssociationCommentTask;
    }

    /**
     * @param FileAssociationComment $fileAssociationCommentTask
     */
    public function delete(FileAssociationComment $fileAssociationCommentTask): void
    {
        $this->repository->delete($fileAssociationCommentTask);
    }
}
