<?php
namespace App\Controller\Www;

use App\Entity\Event;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

#[Route(name: "www_")]
class SpectaclesController extends AbstractController
{
    #[Route("/", name: "home")]
    public function index(EntityManagerInterface $manager) {
        $events = $manager->getRepository(Event::class)->findForPage("home", 6, 1);

        return $this->render("www/home.html.twig",
            [
                "events" => $events,
            ]
        );
    }

    #[Route("/spectacles", name: "spectacles")]
    public function spectacles(EntityManagerInterface $manager) {
        return $this->_spectacles("spectacles", 1, $manager);
    }

    #[Route("/spectacles/{page}", 
        name: "spectacles_page",
        requirements:[ 
            "page" => "\d+"
        ]
    )]     
    public function spectacles_page(string $page, EntityManagerInterface $manager) {
        $ipage = intval($page);
        return $this->_spectacles("spectacles", $ipage, $manager);
    }

    #[Route("/autres-rendez-vous", name: "other_shows")]
    public function autres(EntityManagerInterface $manager) {
        return $this->_spectacles("other-shows", 1, $manager);
    }

    #[Route("/autres-rendez-vous/{page}", 
        name: "other_shows_page",
        requirements:[ 
            "page" => "\d+"
        ]
    )]     
    public function others(string $page, EntityManagerInterface $manager) {
        $ipage = intval($page);
        return $this->_spectacles("other-shows", $ipage, $manager);
    }

    public function _spectacles(string $page_name, int $page_number, EntityManagerInterface $manager) {
        $spectacles = $manager->getRepository(Event::class)->findForPage($page_name, 10, $page_number);

        $route_name = "";
        switch ($page_name) {
            case "other-shows":
                $route_name = "other_shows";
                break;
            case "spectacles":
                $route_name = "spectacles";
                break;
        }

        return $this->render("www/spectacles.html.twig",
            [
                "events" => $spectacles->results,
                "page"   => $page_name,
                "prev_page" => ($spectacles->prev_page_number > 0) ? $this->generateUrl('www_' . $route_name . '_page', ["page" => $spectacles->prev_page_number]) : "",
                "next_page" => ($spectacles->next_page_number > 0) ? $this->generateUrl('www_' . $route_name . '_page', ["page" => $spectacles->next_page_number]) : "",
            ]
        );
    }
}