<?php

namespace Lc\SovBundle\Controller\Reminder;

use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\CaracoleBundle\Resolver\SectionResolver;
use Lc\SovBundle\Container\Reminder\ReminderContainer;
use Lc\SovBundle\Controller\ControllerTrait;
use Lc\SovBundle\Factory\Reminder\ReminderFactory;
use Lc\SovBundle\Form\Reminder\ReminderAdminFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Annotation\Route;

class ReminderAdminController extends AbstractController
{
    use ControllerTrait;

    protected EntityManagerInterface $entityManager;
    protected ReminderContainer $reminderContainer;
    protected FormFactoryInterface $formFactory;
    protected UrlGeneratorInterface $urlGenerator;
    protected MerchantResolver $merchantResolver;
    protected SectionResolver $sectionResolver;
    protected ParameterBagInterface $parameterBag;

    public function __construct(
            EntityManagerInterface $entityManager,
            ReminderContainer $reminderContainer,
            FormFactoryInterface $formFactory,
            UrlGeneratorInterface $urlGenerator,
            ParameterBagInterface $parameterBag
    ) {
        $this->entityManager = $entityManager;
        $this->reminderContainer = $reminderContainer;
        $this->formFactory = $formFactory;
        $this->urlGenerator = $urlGenerator;
        $this->parameterBag = $parameterBag;
    }

    /**
     * @Route("/admin/reminder/modal", name="sov_admin_reminder_render_modal")
     */
    public function renderModal(Request $request): Response
    {
        $id = $request->get('id');

        if($id) {
            $reminder = $this->reminderContainer->getRepositoryQuery()->getRepository()->find($id);
            $action = $this->urlGenerator->generate(
                    $this->parameterBag->get('app.reminder.route_edit'),
                    ['id' => $id]
            );
        }
        else {
            $crudAction = $request->get('crudAction');
            $crudControllerFqcn = $request->get('crudControllerFqcn');
            $entityId = $request->get('entityId') ? $request->get('entityId') : null ;
            $reminder = $this->createEntity($crudAction, $crudControllerFqcn, $entityId);
            $action = $this->urlGenerator->generate(
                    $this->parameterBag->get('app.reminder.route_new')
            );
        }

        $form = $this->formFactory->create(
                ReminderAdminFormType::class,
                $reminder,
                [
                        'action' => $action
                ]
        );

        return $this->render(
                '@LcSov/admin/reminder/form_modal.html.twig',
                [
                        'form_reminder' => $form->createView()
                ]
        );
    }

    /**
     * @Route("/admin/reminder/new", name="sov_admin_reminder_new")
     */
    public function new(Request $request)
    {
        $reminder = $this->createEntity();

        $form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $reminder = $form->getData();
            $this->entityManager->persist($reminder);
            $this->entityManager->flush();

            $this->addFlashTranslator('success', 'added');
        }

        return $this->redirect($request->headers->get('referer'));
    }

    /**
     * @Route("/admin/reminder/edit/{id}", name="sov_admin_reminder_edit")
     */
    public function edit(Request $request)
    {
        $id = $request->get('id');
        $reminder = $this->reminderContainer->getRepositoryQuery()->getRepository()->find($id);

        $form = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $reminder = $form->getData();
            $this->entityManager->update($reminder);
            $this->entityManager->flush();

            $this->addFlashTranslator('success', 'updated');
        }

        return $this->redirect($request->headers->get('referer'));
    }

    /**
     * @Route("/admin/reminder/done", name="sov_admin_reminder_done")
     */
    public function done(Request $request): JsonResponse
    {
        $id = $request->get('id');
        $reminder = $this->reminderContainer->getRepositoryQuery()->getRepository()->find($id);
        $done = $request->get('done');

        if($done == 'true') {
            $reminder->setDone(true);
        }
        else {
            $reminder->setDone(false);
        }

        $this->entityManager->update($reminder);
        $this->entityManager->flush();
        return new JsonResponse(['success' => true]);
    }

    public function createEntity(string $crudAction = null, string $crudControllerFqcn = null, int $entityId = null)
    {
        $factory = new ReminderFactory();
        return $factory->create($crudAction, $crudControllerFqcn, $entityId);
    }
}
