<?php

namespace Seh\Bundle\SehBundle\Controller;

use Doctrine\ORM\Query;
use Gedmo\Translatable\TranslatableListener;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use BeSimple\I18nRoutingBundle\Routing\I18nRoute;
use Symfony\Component\Routing\RouteCollection;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Seh\Bundle\SehBundle\Entity\Hotel;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Class BrandSheetController
 * Front Controller ;
 * Actions involving thematic should be located here
 *
 * @package Seh\Bundle\SehBundle\Controller
 *
 */
class BrandSheetController extends Controller
{
    /**
     * @Template("SehBundle:brandSheet:brand_sheet_pdj.html.twig")
     */
    public function showPdjAction()
    {
        $em = $this->getDoctrine()->getManager();
        $brand = $em->getRepository('SehBundle:Brand')->findOneByArtsysID("PDJ");

        $hotels = $em->getRepository('SehBundle:Hotel')->findAllByBrand($brand, $this->getRequest()->getLocale());

        @usort($hotels, array("Seh\Bundle\SehBundle\Controller\BrandSheetController", "sortByLatDesc"));

        return array(
            "hotels" => $hotels
        );
    }

    /**
     * @Template("SehBundle:brandSheet:brand_sheet_ih.html.twig")
     */
    public function showIhAction()
    {
        $em = $this->getDoctrine()->getManager();
        $brand = $em->getRepository('SehBundle:Brand')->findOneByArtsysID("IH");

        $hotels = $em->getRepository('SehBundle:Hotel')->findAllByBrand($brand, $this->getRequest()->getLocale());

        @usort($hotels, array("Seh\Bundle\SehBundle\Controller\BrandSheetController", "sortByLatDesc"));

        return array(
            "hotels" => $hotels
        );
    }

    /**
     * @Route("/our-brands-relais-du-silence", name="brand_sheet_rds", options={"label"="Fiche marque rds"})
     * @Template("SehBundle:brandSheet:brand_sheet_rds.html.twig")
     */
    public function showRdsAction()
    {
        $em = $this->getDoctrine()->getManager();
        $brand = $em->getRepository('SehBundle:Brand')->findOneByArtsysID("RDS");

        $hotels = $em->getRepository('SehBundle:Hotel')->findAllByBrand($brand, $this->getRequest()->getLocale());

        @usort($hotels, array("Seh\Bundle\SehBundle\Controller\BrandSheetController", "sortByLatDesc"));

        return array(
            "hotels" => $hotels
        );
    }

    /**
     * @Template("SehBundle:brandSheet:brand_sheet_qys.html.twig")
     */
    public function showQysAction()
    {
        $em = $this->getDoctrine()->getManager();
        $brand = $em->getRepository('SehBundle:Brand')->findOneByArtsysID("QYS");

        $hotels = $em->getRepository('SehBundle:Hotel')->findAllByBrand($brand, $this->getRequest()->getLocale());

        @usort($hotels, array("Seh\Bundle\SehBundle\Controller\BrandSheetController", "sortByLatDesc"));

        return array(
            "hotels" => $hotels
        );
    }

    private function sortByLatDesc($a, $b)
    {
        if($a->getLatitude() > $b->getLatitude()) {
            return 1;
        } elseif ($a->getLatitude() < $b->getLatitude()) {
            return 2;
        } else {
            return 0;
        }
    }
}
