<?php

namespace App\Controller\Admin\EventConfig;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Routing\Attribute\Route;
use Doctrine\ORM\EntityManagerInterface;

use App\Entity\Event;
use App\Entity\Game;
use App\Repository\GameRepository;

final class GameController extends AbstractController
{
    #[Route('/admin/event/{id}/configure/game', name: 'app_admin_event_config_game', requirements: ['id' => Requirement::UUID_V7], methods: ['GET', 'POST'])]
    public function index(?Event $event, GameRepository $repository): Response
    {
        $gamesPlayed = $event->getGameAssigned();
        $gamesNotPlayed = array();

        // lister tous les jeux de tous les MJ pour trouver ceux qui ne sont pas assignés
        foreach($event->getGamemastersAssigned() as $gamemaster) {
            foreach($gamemaster->getGamesCanMaster() as $game) {
                // Si le jeu n'est pas dans les jeux joués
                if (!in_array($game, $gamesPlayed->toArray())) {
                    // et s'il n'est pas déjà dans les jeux pas joués
                    if (!in_array($game, $gamesNotPlayed)) {
                        $gamesNotPlayed[] = $game;
                    }
                }
            }
        }

        $allGames = array_merge($gamesPlayed->toArray(), $gamesNotPlayed);

        return $this->render('admin/event/config/game.html.twig', [
            'gamesPlayed' => $gamesPlayed,
            'gamesNotPlayed' => $gamesNotPlayed,
            'allGames' => $allGames,
            'event' => $event,
        ]);
    }

    #[Route('/admin/event/{id}/configure/game/toggle/', name: 'app_admin_event_config_game_toggle', requirements: ['id' => Requirement::UUID_V7], methods: ['POST'])]
    public function toggle(?Event $event, Request $request, GameRepository $repository, EntityManagerInterface $entityManager): JsonResponse
    {
        // Récupération du jeu
        $data = json_decode($request->getContent(), true);
        $gameID = $data['gameId'] ?? null;
        if (!$gameID) {
            return new JsonResponse(['success' => false]);
        }
        $game = $repository->findByStrID($gameID);
        
        if (in_array($game, $event->getGameAssigned()->toArray()))
        {
            // Enlever le jeu de la liste
            $event->removeGameAssigned($game);
            // marquer la réponse
            $state='DISABLE';

        } else {
            // Ajouter le jeu à la liste
            $event->addGameAssigned($game);
            // Ajouter les jeux spécifiques à ce MJ dans les jeux de l'événement
            // marquer la réponse
            $state='ENABLE';

        }
        $entityManager->persist($event);
        $entityManager->flush();

        return new JsonResponse(['success' => true, 'status' => $state]);

    }

}
