<?php

namespace App\Service;

use App\Entity\ContentVersion;
use App\Repository\ContentVersion\ContentVersionRepositoryInterface;
use App\Service\DataGrid\DataGridInterface;
use Doctrine\ORM\EntityNotFoundException;

final class ContentVersionService
{
    /**
     * @var ContentVersionRepositoryInterface
     */
    private $contentVersionRepository;

    /**
     * ContentVersionService constructor.
     *
     * @param ContentVersionRepositoryInterface $contentVersionRepository
     */
    public function __construct(ContentVersionRepositoryInterface $contentVersionRepository)
    {
        $this->contentVersionRepository = $contentVersionRepository;
    }

    /**
     * @param int $contentVersionId
     *
     * @return ContentVersion
     *
     * @throws EntityNotFoundException
     */
    public function getContentVersion(int $contentVersionId): ContentVersion
    {
        $contentVersion = $this->contentVersionRepository->findById($contentVersionId);
        if (!$contentVersion) {
            throw new EntityNotFoundException('ContentVersion with id '.$contentVersionId.' does not exist!');
        }

        return $contentVersion;
    }

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

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

        return $this->contentVersionRepository->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 countContentVersionsByGrid(DataGridInterface $dataGrid): int
    {
        $criteria = $this->getCriteria($dataGrid);

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

    /**
     * @param ContentVersion $contentVersion
     *
     * @return ContentVersion
     */
    public function saveContentVersion(ContentVersion $contentVersion): ContentVersion
    {
        $this->contentVersionRepository->save($contentVersion);

        return $contentVersion;
    }

    /**
     * @param ContentVersion $contentVersion
     */
    public function deleteContentVersion(ContentVersion $contentVersion): void
    {
        $this->contentVersionRepository->delete($contentVersion);
    }

    /**
     * @param DataGridInterface $dataGrid
     *
     * @return array
     */
    private function getCriteria(DataGridInterface $dataGrid): array
    {
        $criteria = [];

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

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

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

        return $criteria;
    }

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