<?php

namespace Club\BookingBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * FieldRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class FieldRepository extends EntityRepository
{
  public function getDayData(\Club\UserBundle\Entity\Location $location, \DateTime $date)
  {
    $fields = $this->_em->getRepository('ClubBookingBundle:Field')->findBy(array(
      'location' => $location->getId()
    ));

    $start_time = clone $date;
    $start_time->setTime(23,59,59);
    $end_time = clone $date;
    $end_time->setTime(0,0,0);

    foreach ($fields as $field) {
      $intervals = $this->_em->getRepository('ClubBookingBundle:Interval')->findValidByField($field, $date->format('N'));
      foreach ($intervals as $interval) {
        if ($interval->getStartTime()->format('His') < $start_time->format('His')) {
          $start_time->setTime(
            $interval->getStartTime()->format('H'),
            $interval->getStartTime()->format('i'),
            $interval->getStartTime()->format('s')
          );
        }

        if ($interval->getStopTime()->format('His') > $end_time->format('His')) {
          $end_time->setTime(
            $interval->getStopTime()->format('H'),
            $interval->getStopTime()->format('i'),
            $interval->getStopTime()->format('s')
          );
        }
      }
    }

    $res = array(
      'start_time' => $start_time,
      'end_time' => $end_time
    );

    return $res;
  }

  public function getFieldsOverview(\Club\UserBundle\Entity\Location $location, \DateTime $date)
  {
    $fields = $this->_em->createQueryBuilder()
      ->select('f')
      ->from('ClubBookingBundle:Field', 'f')
      ->where('f.location = :location')
      ->andWhere('(f.open <= :date OR f.open IS NULL)')
      ->andWhere('(f.close >= :date OR f.close IS NULL)')
      ->setParameter('location', $location->getId())
      ->setParameter('date', $date->format('Y-m-d'))
      ->getQuery()
      ->getResult();

    $res = array();
    foreach ($fields as $field) {
      $intervals = $this->_em->getRepository('ClubBookingBundle:Interval')->findValidByField($field, $date->format('N'));

      foreach ($intervals as $interval) {
        $interval->getStartTime()->setDate(
          $date->format('Y'),
          $date->format('m'),
          $date->format('d')
        );
        $interval->getStopTime()->setDate(
          $date->format('Y'),
          $date->format('m'),
          $date->format('d')
        );
      }
      $field->setTimes($intervals);
    }

    return $fields;
  }

  public function getFieldsBooking(\Club\UserBundle\Entity\Location $location, \DateTime $date)
  {
    $fields = $this->getFieldsOverview($location, $date);
    $bookings = $this->_em->getRepository('ClubBookingBundle:Booking')->getAllByLocationDate($location, $date);

    foreach ($fields as $field) {
      foreach ($field->getTimes() as $interval) {
        foreach ($bookings as $booking) {
          if ($booking->getInterval()->getId() == $interval->getId())
            $interval->setBooking($booking);
        }
      }
    }

    return $fields;
  }

  public function getNextPosition(\Club\UserBundle\Entity\Location $location)
  {
    $r = $this->_em->createQueryBuilder()
      ->select('f')
      ->from('ClubBookingBundle:Field', 'f')
      ->where('f.location = :location')
      ->orderBy('f.position', 'DESC')
      ->setMaxResults(1)
      ->setParameter('location', $location->getId())
      ->getQuery()
      ->getOneOrNullResult();

    $ret = ($r) ? $r->getPosition()+1 : 1;

    return $ret;
  }

  public function getLocationWithFields()
  {
    $res = $this->_em->createQueryBuilder()
      ->select('DISTINCT l.id')
      ->from('ClubBookingBundle:Field', 'f')
      ->leftJoin('f.location', 'l')
      ->getQuery()
      ->getResult();

    $qb = $this->_em->createQueryBuilder()
      ->select('l')
      ->from('ClubUserBundle:Location', 'l');

    foreach ($res as $r) {
      $qb
        ->orWhere('l.id = ?'.$r['id'])
        ->setParameter($r['id'], $r['id']);
    }

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

  public function getAll()
  {
      return $this->createQueryBuilder('f')
          ->orderBy('f.location', 'ASC')
          ->addOrderBy('f.position', 'ASC')
          ->getQuery()
          ->getResult();
  }
}
