<?php

namespace Lc\SovBundle\Repository\Reminder;

use Knp\Component\Pager\PaginatorInterface;
use Lc\SovBundle\Repository\AbstractRepositoryQuery;

class ReminderRepositoryQuery extends AbstractRepositoryQuery implements ReminderRepositoryQueryInterface
{
    public function __construct(ReminderRepository $repository, PaginatorInterface $paginator)
    {
        parent::__construct($repository, 'r', $paginator);
    }

    public function filterDone($done = false)
    {
        return $this
                ->andWhere('.done = :done')
                ->setParameter(':done', $done);
    }

    public function filterUser($user)
    {
        return $this
                ->leftJoin('.users', 'u')
                ->having('COUNT(u.id) = 0')
                ->orHaving(':user MEMBER OF .users')
                ->setParameter(':user', $user)
                ->groupBy('.id');
    }

    public function filterCrudAction($crudAction)
    {
        if(is_null($crudAction)) {
            return $this
                    ->andWhere('.crudControllerFqcn IS NULL');
        }
        else {
            return $this
                    ->andWhere('.crudAction = :crudAction')
                    ->setParameter(':crudAction', $crudAction);
        }
    }

    public function filterCrudControllerFqcn($crudControllerFqcn)
    {
        if(is_null($crudControllerFqcn)) {
            return $this
                    ->andWhere('.crudControllerFqcn IS NULL');
        }
        else {
            return $this
                    ->andWhere('.crudControllerFqcn = :crudControllerFqcn')
                    ->setParameter(':crudControllerFqcn', $crudControllerFqcn);
        }
    }

    public function filterEntityId($entityId)
    {
        if(is_null($entityId)) {
            return $this
                    ->andWhere('.entityId IS NULL');
        }
        else {
            return $this
                    ->andWhere('.entityId = :entityId')
                    ->setParameter(':entityId', $entityId);
        }
    }

    public function orderDefault()
    {
        return $this
                ->orderBy('.dateReminder', 'ASC');
    }

}