<?php

namespace App\Service;

use App\Entity\Content;
use App\Repository\Content\ContentRepositoryInterface;
use App\Service\DataGrid\DataGridInterface;
use Doctrine\ORM\EntityNotFoundException;

final class ContentService
{
    /**
     * @var ContentRepositoryInterface
     */
    private $contentRepository;

    /**
     * ContentService constructor.
     *
     * @param ContentRepositoryInterface $contentRepository
     */
    public function __construct(ContentRepositoryInterface $contentRepository)
    {
        $this->contentRepository = $contentRepository;
    }

    /**
     * @param int $contentId
     *
     * @return Content
     *
     * @throws EntityNotFoundException
     */
    public function getContent(int $contentId): Content
    {
        $content = $this->contentRepository->findById($contentId);
        if (!$content) {
            throw new EntityNotFoundException('Content with id '.$contentId.' does not exist!');
        }

        return $content;
    }

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

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

        return $this->contentRepository->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->contentRepository->count($criteria);
    }

    /**
     * @param Content $content
     *
     * @return Content
     */
    public function saveContent(Content $content): Content
    {
        $this->contentRepository->save($content);

        return $content;
    }

    /**
     * @param Content $content
     */
    public function deleteContent(Content $content): void
    {
        $this->contentRepository->delete($content);
    }

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

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

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

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

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

        return $criteria;
    }
}
