<?php

namespace App\Service;

use App\Entity\Negotiation;
use App\Repository\Negotiation\NegotiationRepositoryInterface;
use App\Service\DataGrid\DataGridInterface;
use Doctrine\ORM\EntityNotFoundException;

final class NegotiationService
{
    /**
     * @var NegotiationRepositoryInterface
     */
    private $repository;

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

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

        return $negotiation;
    }

    /**
     * @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 Negotiation $negotiation
     *
     * @return Negotiation
     */
    public function save(Negotiation $negotiation): Negotiation
    {
        $this->repository->save($negotiation);

        return $negotiation;
    }

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

    /**
     * @param DataGridInterface $dataGrid
     *
     * @return array
     */
    private function getCriteria(DataGridInterface $dataGrid): array
    {
        $criteria = [];
        if ($dataGrid->getFilters()->containsKey('q') && $dataGrid->getFilters()->get('q')->isValid()) {
            $criteria += ['title' => $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('campaign') && $dataGrid->getFilters()->get('campaign')->isValid()) {
            $criteria += ['campaign' => $dataGrid->getFilters()->get('campaign')->getValue()];
        }

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

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

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

        return $criteria;
    }

    /**
     * @param string $email
     *
     * @return Negotiation
     *
     * @throws \Exception
     */
    public function getByEmail(string $email): Negotiation
    {
        if (null === $this->repository->getByEmail($email)) {
            throw new \Exception('There is no Negotiation for given Email');
        }

        return $this->repository->getByEmail($email);
    }

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