<?php

namespace Lc\ShopBundle\Controller\Backend;

use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use FOS\UserBundle\Model\UserManagerInterface;
use Lc\ShopBundle\Form\Backend\Ticket\TicketMessageType;
use Lc\ShopBundle\Form\Backend\Ticket\TicketStatusType;
use Lc\ShopBundle\Services\UtilsManager;
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;

class TicketController extends AdminController
{
        protected $ticketUtils;
        public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator)
        {
                $this->ticketUtils = $utilsManager->getTicketUtils();
                parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator);

        }


        public function showAction()
        {
                $this->dispatch(EasyAdminEvents::PRE_SHOW);

                $id = $this->request->query->get('id');
                $easyadmin = $this->request->attributes->get('easyadmin');
                $entity = $easyadmin['item'];

                $fields = $this->entity['show']['fields'];
                $deleteForm = $this->createDeleteForm($this->entity['name'], $id);

                $this->dispatch(EasyAdminEvents::POST_SHOW, [
                        'deleteForm' => $deleteForm,
                        'fields' => $fields,
                        'entity' => $entity,
                ]);

                $parameters = [
                        'entity' => $entity,
                        'fields' => $fields,
                        'delete_form' => $deleteForm->createView(),
                ];

                return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
        }

        /**
         * The method that is executed when the user performs a 'show' action on an entity.
         *
         * @return Response
         */
        public function renderTicketTemplate($actionName, $templatePath, array $parameters = [])
        {
                if ($actionName == 'show') {
                        $parameters['form_add_ticket_message'] = $this->createCustomForm(TicketMessageType::class, 'newMessageTicket', $parameters, false)->createView();
                        $parameters['form_ticket_status'] = $this->createCustomForm(TicketStatusType::class, 'ticketStatus', $parameters)->createView();
                }
                return parent::renderTemplate($actionName, $templatePath, $parameters);

        }


        public function ticketStatusAction(){
                $easyadmin = $this->request->attributes->get('easyadmin');
                $ticket = $easyadmin['item'];

                $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
                $formTicketStatusForm->handleRequest($this->request);

                if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
                                $this->em->persist($ticket);
                                $this->em->flush();

                                $this->utils->addFlash('success', 'success.ticket.statusChange');
                } else {
                        $this->utils->addFlash('error', $formTicketStatusForm->getErrors());
                }
                $response['flashMessages'] = $this->utils->getFlashMessages();

                return new Response(json_encode($response));
        }

        public function newMessageTicketAction(){
                $easyadmin = $this->request->attributes->get('easyadmin');
                $ticket = $easyadmin['item'];

                $formAddTicketMessage = $this->createForm(TicketMessageType::class);
                $formAddTicketMessage->handleRequest($this->request);

                if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
                        $this->ticketUtils->createTicketMessage(array(
                                'ticket'=>$ticket,
                                'message'=> $formAddTicketMessage->get('message')->getData(),
                                'answerByAdmin'=>true
                        ));

                        $this->utils->addFlash('success', 'success.ticket.addMessage');
                } else {
                        $this->utils->addFlash('error', $formAddTicketMessage->getErrors());
                }

                return $this->redirectToRoute('easyadmin', [
                        'action' => 'show',
                        'entity' => 'Ticket',
                        'id' => $ticket->getId()
                ]);
        }
}