<?php

namespace App\Controller\Frontend;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

use App\Entity\Article;
use App\Entity\Service;
use App\Entity\ServiceRdvReservation;

use App\Repository\ArticleRepository;
use App\Repository\ServiceRepository;
use App\Repository\RdvTempoRepository;
use App\Repository\ServiceRdvReservationRepository;

use Symfony\Component\HttpFoundation\JsonResponse;

use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\ExpressionLanguage\Expression;

#[Route('/frontend/arti-servi')]
class ArticleServiceController extends AbstractController
{
    /**
     * @var ArticleRepository $articleRepository
     */
    private $articleRepository;

    /**
     * @var ServiceRepository $serviceRepository
     */
    private $serviceRepository;
    
    /**
     * @var RdvTempoRepository $rdvTempoRepository
     */
    private $rdvTempoRepository;

    /**
     * @param ArticleRepository $articleRepository
     * @param PositionRepository $positionRepository
     * @param RdvTempoRepository $rdvTempoRepository
     * @return $this
     */
    public function __construct(
        ArticleRepository $articleRepository,
        ServiceRepository $serviceRepository,
        RdvTempoRepository $rdvTempoRepository
    ){
        $this->articleRepository = $articleRepository;
        $this->serviceRepository = $serviceRepository;
        $this->rdvTempoRepository = $rdvTempoRepository;
    }


    #[Route('/index', name: 'frontend_article_service_index')]
    public function index(): Response
    {
        $promos = [];

        $articles = $this->articleRepository->show();

        $services = $this->serviceRepository->show();
        
        $leng = (count($articles)>= count($services)) ? count($articles) : count($services);

        for($i=0; $i<$leng; $i++)
        {
            if(isset($articles[$i])){
                $promos[] = $articles[$i];
            }

            if(isset($services[$i])){
                $promos[] = $services[$i];
            }
        }
        
        return $this->render('frontend/article_service/index.html.twig', [
            'leng' => $leng,
            'articles' => $articles,
            'services' => $services,
        ]);
    }

    
    #[Route('/article/detail/{id}', name: 'frontend_article_detail')]
    #[IsGranted(new Expression('is_granted("ROLE_USER")'))]
    public function detail_article(Article $article): Response
    {
        return $this->render('frontend/article_service/article_detail.html.twig', [
            'article' => $article,
        ]);
    }


    #[Route('/service/detail/{id}', name: 'frontend_service_detail')]
    #[IsGranted(new Expression('is_granted("ROLE_USER")'))]
    public function detail_service(Service $service): Response
    {
        return $this->render('frontend/article_service/service_detail.html.twig', [
            'service' => $service
        ]);
    }


    #[Route('/service/getlistrdv/{id}', name: 'frontend_service_get_list_rdv')]
    #[IsGranted(new Expression('is_granted("ROLE_USER")'))]
    public function get_list_rdv(Service $service, ServiceRdvReservationRepository $serviceRdvReservationRepository)
    {
        $serviceRdvReservations = $serviceRdvReservationRepository->findBy(['service'=>$service, 'disponible'=> true]);

        $ListIdRdvEncours = $this->rdvTempoRepository->RdvEncoursListId();

        $RendezVous = [];
        foreach($serviceRdvReservations as $reservation){
            
            $idRdv = $reservation->getId();

            $Indice = array_search($idRdv, $ListIdRdvEncours);
            if(!is_numeric($Indice)){
                $RendezVous[$reservation->getDateDebut()->format('d/m/Y')][] = ['idRdv'=>$idRdv, 'debut'=>$reservation->getDateDebut()->format('H:i'), 'fin'=>$reservation->getDateFin()->format('H:i')];
            }	    
        }

        return new JsonResponse($RendezVous);
    }





    #[Route('/service/select/rdv', name: 'frontend_service_select_rdv')]
    #[IsGranted(new Expression('is_granted("ROLE_USER")'))]
    public function service_select_rdv(Request $request, ServiceRdvReservationRepository $serviceRdvReservationRepository): Response
    {
        $id = $request->get('id');

        $user = $this->getUser();

        $serviceRdvReservation = $serviceRdvReservationRepository->find($id);

        $this->rdvTempoRepository->RdvEncoursAdd($user, $serviceRdvReservation);

        return $this->render('frontend/article_service/service_select_rdv.html.twig', [
            'serviceRdvReservation'=>$serviceRdvReservation,
            'user'=> $user
        ]);
    }

    
}