<?php

namespace App\EventSubscriber\Blog;

use App\Core\Entity\EntityInterface;
use App\Core\Event\EntityManager\EntityManagerEvent;
use App\Core\EventSubscriber\EntityManagerEventSubscriber;
use App\Core\Notification\MailNotifier;
use App\Core\Setting\SettingManager;
use App\Core\Site\SiteRequest;
use App\Entity\Blog\Comment;
use App\Repository\Blog\PostFollowRepositoryQuery;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * class CommentEventSubscriber.
 *
 * @author Simon Vieille <simon@deblan.fr>
 */
class CommentEventSubscriber extends EntityManagerEventSubscriber
{
    protected MailNotifier $notifier;
    protected UrlGeneratorInterface $urlGenerator;
    protected SettingManager $settingManager;
    protected PostFollowRepository $postFollowRepository;
    protected SiteRequest $siteRequest;

    public function __construct(
        MailNotifier $notifier,
        UrlGeneratorInterface $urlGenerator,
        SettingManager $settingManager,
        PostFollowRepositoryQuery $postFollowRepositoryQuery,
        SiteRequest $siteRequest
    ) {
        $this->notifier = $notifier;
        $this->urlGenerator = $urlGenerator;
        $this->settingManager = $settingManager;
        $this->postFollowRepositoryQuery = $postFollowRepositoryQuery;
        $this->siteRequest = $siteRequest;
    }

    public function supports(EntityInterface $entity)
    {
        return $entity instanceof Comment;
    }

    public function onCreate(EntityManagerEvent $event)
    {
        if (!$this->supports($event->getEntity())) {
            return;
        }

        $this->notifier
            ->init()
            ->setFrom($this->settingManager->get('email_sender')->getValue())
            ->addRecipient($this->settingManager->get('email_comment')->getValue())
            ->setSubject('[Deblan] Nouveau commentaire')
            ->notify('mail/comment.html.twig', [
                'post' => $event->getEntity()->getPost(),
                'links' => [
                    'post' => $this->urlGenerator->generate('blog_menu_post', [
                        'post' => $event->getEntity()->getPost()->getId(),
                        'slug' => $event->getEntity()->getPost()->getSlug(),
                        '_domain' => $this->siteRequest->getDomain(),
                    ], UrlGeneratorInterface::ABSOLUTE_URL).'#review-'.$event->getEntity()->getId(),
                ],
            ], 'text/plain')
        ;

        $postFollows = $this->postFollowRepositoryQuery->create()
            ->where('.post = :post')
            ->andWhere('.isEnabled = 1')
            ->setParameter(':post', $event->getEntity()->getPost()->getId())
            ->find()
        ;

        foreach ($postFollows as $postFollow) {
            if ($event->getEntity()->getEmail() !== $postFollow->getComment()->getEmail()) {
                $this->notifier
                    ->init()
                    ->setFrom($this->settingManager->get('email_sender')->getValue())
                    ->addRecipient($this->settingManager->get('email_comment')->getValue())
                    ->setSubject('[Deblan] Nouveau commentaire')
                    ->notify('mail/post_follow_comment.html.twig', [
                        'post' => $event->getEntity()->getPost(),
                        'links' => [
                            'post' => $this->urlGenerator->generate('blog_menu_post', [
                                'post' => $event->getEntity()->getPost()->getId(),
                                'slug' => $event->getEntity()->getPost()->getSlug(),
                                '_domain' => $this->siteRequest->getDomain(),
                            ], UrlGeneratorInterface::ABSOLUTE_URL).'#review-'.$event->getEntity()->getId(),
                            'disable' => $this->urlGenerator->generate('blog_tech_follow_disable', [
                                'hash' => $postFollow->getHash(),
                                '_domain' => $this->siteRequest->getDomain(),
                            ], UrlGeneratorInterface::ABSOLUTE_URL),
                        ],
                    ], 'text/plain')
                ;
            }
        }
    }
}
