<?php

namespace App\Controller\Admin;

use App\Entity\TypeEvent;
use App\Form\TypeEventType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;

#[Route("/admin/type-event", name: "admin_typeevent_")]
class TypeEventController extends AbstractController
{
    #[Route('/{id}/down', name: 'down', priority: 2)]
    public function down(TypeEvent $type_event, EntityManagerInterface $manager) {
        if (!$this->getUser()) { return $this->redirectToRoute('auth_login');}
        if (!in_array("ROLE_EDITOR", $this->getUser()->getRoles())) throw new AccessDeniedException();

        $type_event->setOrdreDown();

        $manager->flush();

        return new Response("OK", 200);
    }

    #[Route('/{id}/up', name: 'up', priority: 2)]
    public function up(TypeEvent $type_event, EntityManagerInterface $manager) {
        if (!$this->getUser()) { return $this->redirectToRoute('auth_login');}
        if (!in_array("ROLE_EDITOR", $this->getUser()->getRoles())) throw new AccessDeniedException();

        $type_event->setOrdreUp();

        $manager->flush();

        return new Response("OK", 200);
    }

    #[Route('/new', name: 'new', priority: 99)]
    public function new(Request $request, EntityManagerInterface $manager, TranslatorInterface $t)
    {
        $type_event = new TypeEvent();
        $type_event->setActive(true);

        return $this->_edit($type_event, $request, $manager, $t);
    }

    #[Route('/{id}', name: 'edit', priority: 1)]
    public function edit(TypeEvent $type_event, Request $request, EntityManagerInterface $manager, TranslatorInterface $t)
    {
        return $this->_edit($type_event, $request, $manager, $t);
    }

    private function _edit(TypeEvent $type_event, Request $request, EntityManagerInterface $manager, TranslatorInterface $t)
    {
        if (!$this->getUser()) { return $this->redirectToRoute('auth_login');}
        if (!in_array("ROLE_ADMIN", $this->getUser()->getRoles())) throw new AccessDeniedException();

        $new = ($type_event->getId() <= 0);

        $form = $this->createForm(TypeEventType::class, $type_event);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $manager->persist($type_event);
            $manager->flush();

            $this->addFlash('success', $t->trans('admin.event-type.flash.'.($new?"created":"updated").'-ok', ["%name%" => $type_event->getName()], domain: 'admin'));

            // on redirige vers la liste
            return $this->redirectToRoute("admin_typeevents_list");
        }

        return $this->render('admin/type-event/edit.html.twig',
            [ "form" => $form->createView(),
              "new" => $new,
              "current" => $type_event,
              "id" => $type_event->getId() ]
        );
    }

}
