<?php

namespace Club\TeamBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * ScheduleRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ScheduleRepository extends EntityRepository
{
  public function getAllFuture(\Club\TeamBundle\Entity\Schedule $schedule)
  {
    $parent = ($schedule->getSchedule()) ? $schedule->getSchedule() : $schedule;

    return $this->_em->createQueryBuilder()
      ->select('s')
      ->from('ClubTeamBundle:Schedule','s')
      ->where('s.first_date >= :date')
      ->andWhere('(s.schedule = :id OR s.id = :id)')
      ->setParameter('date', $schedule->getFirstDate()->format('Y-m-d H:i:s'))
      ->setParameter('id', $parent->getId())
      ->getQuery()
      ->getResult();
  }

  public function getAllPast(\Club\TeamBundle\Entity\Schedule $schedule)
  {
    $parent = ($schedule->getSchedule()) ? $schedule->getSchedule() : $schedule;

    return $this->_em->createQueryBuilder()
      ->select('s')
      ->from('ClubTeamBundle:Schedule','s')
      ->where('s.first_date < :date')
      ->andWhere('(s.schedule = :id OR s.id = :id)')
      ->orderBy('s.first_date')
      ->setParameter('date', $schedule->getFirstDate()->format('Y-m-d H:i:s'))
      ->setParameter('id', $parent->getId())
      ->getQuery()
      ->getResult();
  }

  public function getAllBetween(\DateTime $start, \DateTime $end, \Club\UserBundle\Entity\User $user=null, \Club\UserBundle\Entity\Location $location=null, \Club\BookingBundle\Entity\Field $field=null)
  {
    $qb = $this->_em->createQueryBuilder()
      ->select('s')
      ->from('ClubTeamBundle:Schedule','s')
      ->where('(s.first_date <= :start and s.end_date >= :end)')
      ->orWhere('(s.first_date <= :start and s.end_date <= :end and s.end_date >= :start)')
      ->orWhere('(s.first_date >= :start and s.end_date >= :end and s.first_date < :end)')
      ->orWhere('(s.first_date >= :start and s.end_date <= :end and s.end_date >= :start)')
      ->orderBy('s.first_date')
      ->setParameter('start', $start->format('Y-m-d H:i:s'))
      ->setParameter('end', $end->format('Y-m-d H:i:s'));

    if (isset($user)) {
      $qb
        ->leftJoin('s.users', 'u')
        ->andWhere('u.user = :user')
        ->setParameter('user', $user->getId());
    }

    if (isset($location)) {
      $qb
        ->leftJoin('s.location', 'l')
        ->andWhere('l.id = :location')
        ->setParameter('location', $location->getId());
    }

    if (isset($field)) {
      $qb
        ->leftJoin('s.fields', 'f')
        ->andWhere('f.id = :field')
        ->setParameter('field', $field->getId());
    }

    return $qb
      ->getQuery()
      ->getResult();
  }

  public function getAllParent()
  {
    return $this->_em->createQueryBuilder()
      ->select('s')
      ->from('ClubTeamBundle:Schedule', 's')
      ->where('s.schedule IS NULL')
      ->getQuery()
      ->getResult();
  }

  public function getPrevSchedule(\Club\TeamBundle\Entity\Schedule $schedule)
  {
    $parent = ($schedule->getSchedule()) ? $schedule->getSchedule() : $schedule;

    return $this->_em->createQueryBuilder()
      ->select('s')
      ->from('ClubTeamBundle:Schedule','s')
      ->where('s.first_date <= :date')
      ->andWhere('(s.id = :id OR s.schedule = :id)')
      ->setMaxResults(1)
      ->orderBy('s.first_date', 'DESC')
      ->setParameter('date', date('Y-m-d H:i:s'))
      ->setParameter('id', $parent->getId())
      ->getQuery()
      ->getSingleResult();
  }

  public function getLatest(\Club\UserBundle\Entity\User $user, $limit=10)
  {
    $date = new \DateTime();

    return $this->_em->createQueryBuilder()
      ->select('s')
      ->from('ClubTeamBundle:Schedule', 's')
      ->leftJoin('s.users', 'u')
      ->where('u.user = :user')
      ->andWhere('s.first_date < :date')
      ->setParameter('user', $user->getId())
      ->setParameter('date', $date)
      ->setMaxResults($limit)
      ->getQuery()
      ->getResult();
  }

  public function getNotProcessed()
  {
    return $this->_em->createQueryBuilder()
      ->select('s')
      ->from('ClubTeamBundle:Schedule','s')
      ->where('s.end_date < CURRENT_TIMESTAMP()')
      ->andWhere('s.processed=0')
      ->getQuery()
      ->getResult();
  }
}
