<?php

namespace App\Service\Campaign\Report;

use App\Entity\Campaign\Report;
use App\Entity\Campaign\Report\Element;
use App\Service\DataGrid\DataGridInterface;
use Doctrine\ORM\EntityNotFoundException;

final class ElementService
{
    /**
     * @var ElementRepositoryInterface
     */
    private $repository;

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

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

        return $element;
    }

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

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

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

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

        return $criteria;
    }

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

        return $element;
    }

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

    public function getSubjects(): array
    {
        return $this->repository->getSubjects();
    }

    public function getSubjectIds(): array
    {
        return $this->repository->getSubjectIds();
    }

    public function getReportElements(Report $report): array
    {
        return $this->repository->getReportElements($report);
    }
}
