<?php

namespace App\MessageHandler;

use App\Entity\FileAssociationNegotiationMessage;
use App\Message\NegotiationMessage;
use App\Service\Content\ContentFileExporter;
use App\Service\NegotiationMessageService;
use Swift_Attachment;
use Swift_Mailer;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class NegotiationMessageHandler implements MessageHandlerInterface
{
    /**
     * @var Swift_Mailer
     */
    private $mailer;
    /**
     * @var NegotiationMessageService
     */
    private $negotiationMessageService;
    /**
     * @var ContentFileExporter
     */
    private $contentFileExporter;
    /**
     * @var KernelInterface
     */
    private $kernel;

    public function __construct(Swift_Mailer $mailer, NegotiationMessageService $negotiationMessageService, ContentFileExporter $contentFileExporter, KernelInterface $kernel)
    {
        $this->mailer = $mailer;
        $this->negotiationMessageService = $negotiationMessageService;
        $this->contentFileExporter = $contentFileExporter;
        $this->kernel = $kernel;
    }

    public function __invoke(NegotiationMessage $message)
    {
        $email = (new \Swift_Message($message->getTitle()))
            ->setFrom('contact@i3xpress.net')
            ->setReplyTo($message->getFrom())
            ->setBody($message->getBody(), 'text/plain')
            ->setTo($message->getTo());

        if ($message->getAttachContent() && 'NONE' !== $message->getAttachContent()) {
            $extension = $message->getAttachContent();

            $negotiationMessage = $this->negotiationMessageService->get($message->getId());

            if (null !== $negotiationMessage->getNegotiation()->getContent()) {
                foreach ($negotiationMessage->getNegotiation()->getContent()->getFiles() as $file) {
                    /** @var FileAssociationNegotiationMessage $fileAssociation */
                    $fileAssociation = (new FileAssociationNegotiationMessage())
                        ->setNegotiationMessage($negotiationMessage)
                        ->setFile($file->getFile());

                    $negotiationMessage->addAttachment($fileAssociation);
                }

                $file = $this->contentFileExporter->export($negotiationMessage->getNegotiation()->getContent(), $extension);

                /** @var FileAssociationNegotiationMessage $fileAssociation */
                $fileAssociation = (new FileAssociationNegotiationMessage())
                    ->setNegotiationMessage($negotiationMessage)
                    ->setFile($file);
                $negotiationMessage->addAttachment($fileAssociation);
            }

            $negotiationMessage = $this->negotiationMessageService->save($negotiationMessage);

            foreach ($negotiationMessage->getAttachments() as $fileAssociation) {
                $path = sprintf('%s/public/uploads/%s/%s', $this->kernel->getProjectDir(), $fileAssociation->getFile()->getPath(), $fileAssociation->getFile()->getFilename());
                $email->attach(Swift_Attachment::fromPath($path));
            }
        }

        $email->getHeaders()->addTextHeader('X-Mailgun-Variables', "{\"message-id\": {$message->getId()}}");

        $this->mailer->send($email);
    }
}
