<?php

namespace App\Service;

use App\Entity\CommentContent;
use App\Repository\CommentContent\CommentContentRepositoryInterface;
use App\Service\DataGrid\DataGridInterface;
use Doctrine\ORM\EntityNotFoundException;

final class CommentContentService
{
    /**
     * @var CommentContentRepositoryInterface
     */
    private $repository;

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

    /**
     * @param int $id
     *
     * @return CommentContent
     *
     * @throws EntityNotFoundException
     */
    public function get(int $id): CommentContent
    {
        $commentContent = $this->repository->findById($id);
        if (!$commentContent) {
            throw new EntityNotFoundException('CommentContent 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 CommentContent $commentContent
     *
     * @return CommentContent
     */
    public function save(CommentContent $commentContent): CommentContent
    {
        $this->repository->save($commentContent);

        return $commentContent;
    }

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

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

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

        return $criteria;
    }

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