<?php

namespace App\Service;

use App\Entity\Research;
use App\Service\DataGrid\DataGridInterface;
use Doctrine\ORM\EntityNotFoundException;

final class ResearchService
{
    /**
     * @var ResearchRepositoryInterface
     */
    private $repository;

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

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

        return $research;
    }

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

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

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

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

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

        return $criteria;
    }

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

        return $research;
    }

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

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