<?php

namespace App\Controller\BackendAdmin;

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

use Doctrine\ORM\EntityManagerInterface;

use App\Entity\Service;

use App\Repository\ServiceRepository;

use App\Form\BackendAdmin\ServiceType;


#[Route('/backend/admin/service')]
class ServiceController extends AbstractController
{

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

    /**
     * @var EntityManagerInterface $em
     */
    private $em;

    /**
     * @param AnnonceRepository $serviceRepository
     * @param EntityManagerInterface $em
     * @return $this
     */
    public function __construct(
        ServiceRepository $serviceRepository,
        EntityManagerInterface $em
    ){
        $this->serviceRepository = $serviceRepository;
        $this->em = $em;
    }
    
    
    #[Route('/index', name: 'backend_admin_service_index')]
    public function index(): Response
    {
        return $this->render('backend_admin/service/index.html.twig', ['services'=> $this->serviceRepository->findBy([], ["id"=>'DESC'])]);
    }

    #[Route('/add', name: 'backend_admin_service_add')]
    public function add(Request $request): Response
    {
        $service = new Service();
        
        $form = $this->createForm(ServiceType::class, $service);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $service = $form->getData();

            if( ($service->getLimitType() == 0) && ($service->getLimitDate() == null) ){
                $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Limitation Par Date vide</b>"]);
                return $this->redirectToRoute('backend_admin_service_add', ['form'=>$form]);
            }

            $service->setCreateBy($this->getUser());
            $this->em->persist($service);
            $this->em->flush();

            $this->addFlash('info', ['title'=>'Info', 
                                    'message'=>"Le service a été bien créer. pour qu'il soit active il faut ajouter les images et les rendez-vous "]);
        

            return $this->redirectToRoute('backend_admin_service_uploder', ['id' => $service->getId()]);
        }

        return $this->render('backend_admin/service/add.html.twig', ['form'=>$form->createView()] );
    }


    #[Route('/activation/{id}', name: 'backend_admin_service_activation')]
    public function activation(Service $service, Request $request)
    {
        $activation = $request->get('activation');

        if($activation == 'true'){
            $service->setActive(true);
        }else if($activation == 'false'){
            $service->setActive(false);
        }

        $this->em->persist($service);
        $this->em->flush();

        return $this->redirectToRoute('backend_admin_service_index');
    }


    #[Route('/update/{id}', name: 'backend_admin_service_update')]
    public function update(Service $service, Request $request): Response
    {
        $form = $this->createForm(ServiceType::class, $service);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
           
            $service = $form->getData();
           
            if( ($service->getLimitType() == 0) && ($service->getLimitDate() == null) ){
                $this->addFlash('danger', ['title'=>'Erreur', 'message'=>"<b>Limitation Par Date vide</b>"]);
                return $this->redirectToRoute('backend_admin_service_add', ['form'=>$form]);
            }
            
            $service->gestionActive();
           
            $this->em->persist($service);
            $this->em->flush();
            
        }

        return $this->render('backend_admin/service/detail.html.twig', ['form'=>$form->createView(), 'service'=>$service] );
    }
    

    #[Route('/uploder/{id}', name: 'backend_admin_service_uploder')]
    public function uploder(Service $service)
    {
        return $this->render('backend_admin/service/uploder.htm.twig', ['service'=>$service]);
    }


    #[Route('/uploder_image/{id}', name: 'backend_admin_service_uploder_add')]
    public function uploder_add(Service $service, Request $request)
    {
        $idSociete = $service->getSociete()->getId();
        $idService = $service->getId();

        $type = $request->request->get('type');
        
        $directory = $this->getParameter('services_directory');
        
        $base64_image = $request->request->get('image64');
                          
        $posJpeg = strpos($base64_image, 'data:image/jpeg;base64,');
        if ($posJpeg !== false){
            $base64_img = str_replace('data:image/jpeg;base64,', '', $base64_image);
            $extension = ".jpeg";
        }
        
        $posPng = strpos($base64_image,'data:image/png;base64,');
        if ($posPng !== false){
            $base64_img = str_replace('data:image/png;base64,', '', $base64_image);
            $extension = ".png";
        }

        $posJpg = strpos($base64_image, 'data:image/jpg;base64,');
        if ($posJpg !== false){
            $base64_img = str_replace('data:image/jpg;base64,', '', $base64_image);
            $extension = ".jpg";
        }

        $posGif = strpos($base64_image,'data:image/gif;base64,');
        if ($posGif !== false){
            $base64_img = str_replace('data:image/gif;base64,', '', $base64_image);
            $extension = ".gif";
        }

        $namefile = "$idSociete-$idService-$type-".md5(rand()).$extension;

        $image = base64_decode($base64_img);
       
        if($image){ 

            $result = file_put_contents($directory . $namefile, $image);

            if($result){
                if($type == 'default'){
                    if($service->getImageDefault() && file_exists($directory . $service->getImageDefault()) ){ 
                        unlink($directory . $service->getImageDefault());
                    }
                    $service->setImageDefault($namefile);
                }elseif($type == 'detail1'){
                    if($service->getImageDetail1() && file_exists($directory . $service->getImageDetail1()) ){ 
                        unlink($directory . $service->getImageDetail1());
                    }
                    $service->setImageDetail1($namefile);
                }
                elseif($type == 'detail2'){
                    if($service->getImageDetail2() && file_exists($directory . $service->getImageDetail2()) ){ 
                        unlink($directory . $service->getImageDetail2());
                    }
                    $service->setImageDetail2($namefile);
                }
                elseif($type == 'detail3'){
                    if($service->getImageDetail3() && file_exists($directory . $service->getImageDetail3()) ){ 
                        unlink($directory . $service->getImageDetail3());
                    }
                    $service->setImageDetail3($namefile);
                }
                elseif($type == 'detail4'){
                    if($service->getImageDetail4() && file_exists($directory . $service->getImageDetail4()) ){ 
                        unlink($directory . $service->getImageDetail4());
                    }
                    $service->setImageDetail4($namefile);
                }

                $service->gestionActive();

                $this->em->persist($service);
            }
        }
         
        $this->em->flush();
        return new Response();
    }


    #[Route('/uploder_image_delete', name: 'backend_admin_service_uploder_delete')]
    public function uploder_delete(Request $request)
    {
        die();
    }

}