<?php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Form\Actors\ActorAddForm;
use App\Repository\ActorRepository;
use App\Entity\ActivityEntity;
use App\Entity\ActorEntity;
use App\Form\Actors\ActorDeleteForm;
use Symfony\Component\Routing\Generator\UrlGenerator;
use App\Form\Actors\ActorEditForm;
use App\Repository\ActivityRepository;
use App\Enum\StatusEnum;

class ActorController extends AbstractExtendedController
{

    /**
     * Actors management page
     *
     * @param Request $request
     * @return Response
     */
    public function actors(Request $request): Response
    {
        // Get repositories
        $entityManager = $this->getDoctrine()->getManager();
        /** @var ActorRepository $actorRepository */
        $actorRepository = $entityManager->getRepository(ActorEntity::class);

        // Actor add form
        /** @var ActorAddForm $actorAddFom */
        $actorAddFom = $this->createNamedCustomForm('actorAdd', ActorAddForm::class);
        $actorAddFom->handleRequest($request);
        if ($actorAddFom->isSubmitted() && $actorAddFom->isValid()) {
            $errors = $actorAddFom->validate();
            if (empty($errors)) {
                $actor = $actorAddFom->getActor();
                $entityManager->persist($actor);
                $entityManager->flush();

                return $this->ajaxFormAnswer([
                    'refresh' => true,
                    'reset' => true,
                ]);
            } else {
                return $this->ajaxFormAnswer([
                    'error' => join('<br/>', $errors),
                    'code' => Response::HTTP_BAD_REQUEST,
                ]);
            }
        }

        // Actor delete form
        /** @var ActorDeleteForm $actorDeleteForm */
        $actorDeleteForm = $this->createNamedCustomForm('actorDelete', ActorDeleteForm::class);
        $actorDeleteForm->handleRequest($request);
        if ($actorDeleteForm->isSubmitted() && $actorDeleteForm->isValid()) {
            $errors = $actorDeleteForm->validate();
            if (empty($errors)) {
                $actor = $actorDeleteForm->getActor($actorRepository);
                if (!is_null($actor)) {
                    $entityManager->remove($actor);
                    $entityManager->flush();
                    return $this->ajaxFormAnswer([
                        'refresh' => true,
                    ]);
                } else {
                    return $this->ajaxFormAnswer([
                        'error' => 'Utilisateur non trouvé',
                        'code' => Response::HTTP_BAD_REQUEST,
                    ]);
                }
            } else {
                return $this->ajaxFormAnswer([
                    'error' => join('<br/>', $errors),
                    'code' => Response::HTTP_BAD_REQUEST,
                ]);
            }
        }

        // Get actors
        $actors = $actorRepository->findAll();

        return $this->render('actor/actors.html.twig', [
            'actorAddForm' => $actorAddFom,
            'actorDeleteForm' => $actorDeleteForm,
            'actors' => $actors,
        ]);
    }

    /**
     * Actor edition page
     *
     * @param string $id
     * @param Request $request
     * @return Response
     */
    public function actor(string $id, Request $request): Response
    {
        // Get repositories
        $entityManager = $this->getDoctrine()->getManager();
        /** @var ActorRepository $actorRepository */
        $actorRepository = $entityManager->getRepository(ActorEntity::class);
        /** @var ActivityRepository $activityRepository */
        $activityRepository = $entityManager->getRepository(ActivityEntity::class);

        // Get actor
        $actor = $actorRepository->find($id);

        // Actor edit form
        /** @var ActorEditForm $actorEditForm */
        $actorEditForm = $this->createNamedCustomForm('edit', ActorEditForm::class);
        $actorEditForm->handleRequest($request);
        if ($actorEditForm->isSubmitted() && $actorEditForm->isValid()) {
            $errors = $actorEditForm->validate();
            if (empty($errors)) {
                $actorEditForm->updateActor($actor);
                $entityManager->persist($actor);
                $entityManager->flush();
                return $this->ajaxFormAnswer([
                    'message' => 'Modification enregistrées',
                    'refresh' => true,
                ]);
            } else {
                return $this->ajaxFormAnswer([
                    'error' => join('<br/>', $errors),
                    'code' => Response::HTTP_BAD_REQUEST,
                ]);
            }
        }

        // Actor delete form
        /** @var ActorDeleteForm $actorDeleteForm */
        $actorDeleteForm = $this->createNamedCustomForm('delete', ActorDeleteForm::class);
        $actorDeleteForm->handleRequest($request);
        if ($actorDeleteForm->isSubmitted() && $actorDeleteForm->isValid()) {
            $errors = $actorDeleteForm->validate();
            if (empty($errors)) {
                $entityManager->remove($actor);
                $entityManager->flush();
                return $this->ajaxFormAnswer([
                    'redirect' => $this->generateUrl('actors_actors', [], UrlGenerator::ABSOLUTE_PATH),
                ]);
            } else {
                return $this->ajaxFormAnswer([
                    'error' => join('<br/>', $errors),
                    'code' => Response::HTTP_BAD_REQUEST,
                ]);
            }
        }

        // Update fields
        $actorEditForm->updateFields($actor);
        
        // Get activities
        $activities = $activityRepository->findBy([
            'actor' => $actor,
            'status' => StatusEnum::ACTIVE_STATUS
        ]);

        return $this->render('actor/actor.html.twig', [
            'actor' => $actor,
            'activities' => $activities,
            'editForm' => $actorEditForm,
            'deleteForm' => $actorDeleteForm,
        ]);
    }
}

