<?php

namespace App\Controller;

use App\Entity\Banner;
use App\Form\BannerType;
use App\Repository\BannerRepository;
use App\Repository\CityRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\String\Slugger\SluggerInterface;

/**
 * @IsGranted("ROLE_ADMIN")
 * @Route("/admin/banner")
 */
class BannerController extends AbstractController
{
    /**
     * @Route("/", name="admin_banner_index", methods={"GET"})
     */
    public function index(BannerRepository $bannerRepository): Response
    {
        return $this->render('banner/index.html.twig', [
            'banners' => $bannerRepository->findAll(),
        ]);
    }

    /**
     * @Route("/new", name="banner_new", methods={"GET","POST"})
     */
    public function new(Request $request, SluggerInterface $slugger): Response
    {
        $banner = new Banner();
        $form = $this->createForm(BannerType::class, $banner);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $srcFile = $form->get('file')->getData();

            if ($srcFile) {
                $originalFilename = pathinfo($srcFile->getClientOriginalName(), PATHINFO_FILENAME);
                
                $safeFilename = $slugger->slug($originalFilename);
                $newFilename = $safeFilename.'-'.uniqid().'.'.$srcFile->guessExtension();
                $srcFile->move(
                    $this->getParameter('banners_directory'),
                    $newFilename
                );
                
                $banner->setSrc($newFilename);
            }

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($banner);
            $entityManager->flush();

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

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

    /**
     * @Route("/{id}/edit", name="banner_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, Banner $banner, SluggerInterface $slugger): Response
    {
        $form = $this->createForm(BannerType::class, $banner);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $file = $this->getParameter('banners_directory') . DIRECTORY_SEPARATOR . $banner->getSrc();

            if (file_exists($file)) {
                unlink($file);    
            }

            $srcFile = $form->get('file')->getData();

            if ($srcFile) {
                $originalFilename = pathinfo($srcFile->getClientOriginalName(), PATHINFO_FILENAME);
                
                $safeFilename = $slugger->slug($originalFilename);
                $newFilename = $safeFilename.'-'.uniqid().'.'.$srcFile->guessExtension();
                $srcFile->move(
                    $this->getParameter('banners_directory'),
                    $newFilename
                );
                
                $banner->setSrc($newFilename);
            }

            $this->getDoctrine()->getManager()->flush();

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

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

    /**
     * @Route("/{id}", name="banner_delete", methods={"POST"})
     */
    public function delete(Request $request, Banner $banner): Response
    {
        if ($this->isCsrfTokenValid('delete'.$banner->getId(), $request->request->get('_token'))) {
            $file = $this->getParameter('banners_directory') . DIRECTORY_SEPARATOR . $banner->getSrc();

            if (file_exists($file)) {
                unlink($file);    
            }

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($banner);
            $entityManager->flush();
        }

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