<?php

namespace App\Service;

use App\Entity\CommentTask;
use App\Repository\CommentTask\CommentTaskRepositoryInterface;
use App\Service\DataGrid\DataGridInterface;
use Doctrine\ORM\EntityNotFoundException;

final class CommentTaskService
{
    /**
     * @var CommentTaskRepositoryInterface
     */
    private $repository;

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

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

        return $commentContent;
    }

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

    /**
     * @param DataGridInterface $dataGrid
     *
     * @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 int
     */
    public function countByGrid(DataGridInterface $dataGrid): int
    {
        $criteria = $this->getCriteria($dataGrid);

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

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

        return $commentContent;
    }

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

    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('createdBy') && $dataGrid->getFilters()->get('createdBy')->isValid()) {
            $criteria += ['createdBy' => $dataGrid->getFilters()->get('createdBy')->getValue()];
        }

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

        return $criteria;
    }

    public function moveCommentsBetweenUsers(int $from, int $to)
    {
        return $this->repository->moveCommentsBetweenUsers($from, $to);
    }
}
