<?php

namespace Lc\SovBundle\Builder\Ticket;

use Doctrine\ORM\EntityManagerInterface;
use Lc\SovBundle\Event\Ticket\TicketEvent;
use Lc\SovBundle\Notification\MailMailjetNotification;
use Lc\SovBundle\Component\FormComponent;
use Lc\SovBundle\Factory\Ticket\TicketFactory;
use Lc\SovBundle\Model\Ticket\TicketInterface;
use Lc\SovBundle\Model\Ticket\TicketModel;
use Lc\SovBundle\Repository\User\UserStore;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;

class TicketBuilder
{
    protected Security $security;
    protected EntityManagerInterface $entityManager;
    protected MailMailjetNotification $mailMailjetNotification;
    protected FormComponent $formComponent;
    protected ParameterBagInterface $parameterBag;
    protected TicketFactory $ticketFactory;
    protected AuthorizationCheckerInterface $authorizationChecker;
    protected UserStore $userStore;
    protected EventDispatcherInterface $eventDispatcher;


    public function __construct(
            Security $security,
            EntityManagerInterface $entityManager,
            MailMailjetNotification $mailMailjetNotification,
            FormComponent $formComponent,
            ParameterBagInterface $parameterBag,
            AuthorizationCheckerInterface $authorizationChecker,
            TicketFactory $ticketFactory,
            UserStore $userStore,
            EventDispatcherInterface $eventDispatcher
    ) {
        $this->security = $security;
        $this->entityManager = $entityManager;
        $this->mailMailjetNotification = $mailMailjetNotification;
        $this->formComponent = $formComponent;
        $this->parameterBag = $parameterBag;
        $this->ticketFactory = $ticketFactory;
        $this->userStore = $userStore;
        $this->authorizationChecker = $authorizationChecker;
        $this->eventDispatcher = $eventDispatcher;
    }

    public function create(array $params = []): TicketInterface
    {
        $ticket = $this->ticketFactory->create();

        $this->init($ticket, $params);


        $this->entityManager->create($ticket);
        $this->entityManager->flush();

        $this->eventDispatcher->dispatch(new TicketEvent($ticket), TicketEvent::NEW_TICKET_EVENT);

        return $ticket;
    }

    public function init(TicketInterface $ticket, array $params = []): void
    {
        $user = $this->security->getUser();
        if ($user) {
            $ticket->setCreatedBy($user);
        }

        if (isset($params['section'])) {
            $ticket->setSection($params['section']);
        }
        $ticket->setMerchant($params['merchant']);

        if (isset($params['user'])) {
            $ticket->setUser($params['user']);
        } else {
            $ticket->setVisitorFirstname($params['visitorFirstname']);
            $ticket->setVisitorLastname($params['visitorLastname']);
            $ticket->setVisitorEmail($params['visitorEmail']);
            $ticket->setVisitorToken(uniqid());
        }

        $ticket
                ->setStatus(TicketModel::TICKET_STATUS_OPEN)
                ->setType($params['type'])
                ->setSubject($params['subject']);

        $ticketMessageArray = $ticket->getTicketMessages();
        $ticketMessage = $ticketMessageArray[0];
        $ticketMessage->setMessage($params['message']);

        if (isset($params['imageFilename']) && $params['imageFilename']) {
            $ticketMessage->setImageFilename($params['imageFilename']);
        }

        if (isset($params['createByAdmin']) && $params['createByAdmin']) {
            $ticketMessage->setAnswerByAdmin(true);
        }
    }

    // uploadImageTicketMessage
    public function uploadImageTicketMessage($formTicket): ?string
    {
        return $this->formComponent->uploadFile(
                $formTicket,
                'image',
                $this->parameterBag->get('app.ticket_images_directory'),
                $this->parameterBag->get('app.ticket_images_subdirectory')
        );
    }
}
