<?php

namespace App\Service\BackendAdmin;


class ServiceService
{

    public function getListRendezVous($startDate, $endDate, $startTime, $endTime, $duree, $between, $excludeDay)
    {
        $excludeDay = ["Sun"];

        $intervalDay = new \DateInterval('P1D');
        $dateRangeDay = new \DatePeriod($startDate, $intervalDay, $endDate);

        $range = [];
        foreach ($dateRangeDay as $date) {

            $dateStr = $date->format("Y-m-d");
           

            $Indice = array_search($date->format('D'), $excludeDay);
            if(!$excludeDay || !is_numeric($Indice)){
                
                $debut = new \DateTime("$dateStr $startTime");
                $tempo = new \DateTime("$dateStr $startTime");

                $end = new \DateTime("$dateStr $endTime");
                
                do{
                    
                    $fin = clone $tempo->modify("+$duree minutes");

                    if($fin<$end){
                        $range[] = ['debut'=>$debut, 'fin'=>$fin];
                    }
                    
                    $debut = clone $tempo->modify("+$between minutes");

                }while($debut < $end);
            }
            
        }
        return $range;
    }

}