<?php

namespace Lc\SovBundle\EventSubscriber\Ticket;

use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
use Lc\SovBundle\Event\Ticket\TicketEvent;
use Lc\SovBundle\Model\Ticket\TicketInterface;
use Lc\SovBundle\Model\Ticket\TicketMessageInterface;
use Lc\SovBundle\Notification\MailMailjetNotification;
use Lc\SovBundle\Repository\User\UserStore;
use Lc\SovBundle\Solver\Ticket\TicketSolver;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class SendNotificationTicketEventSubscriber implements EventSubscriberInterface
{
    protected MailMailjetNotification $mailMailjetNotification;
    protected UserStore $userStore;
    protected AuthorizationCheckerInterface $authorizationChecker;
    protected TicketSolver $ticketSolver;

    public function __construct(
            MailMailjetNotification $mailMailjetNotification,
            UserStore $userStore,
            AuthorizationCheckerInterface $authorizationChecker,
            TicketSolver $ticketSolver
    ) {
        $this->mailMailjetNotification = $mailMailjetNotification;
        $this->userStore = $userStore;
        $this->authorizationChecker = $authorizationChecker;
        $this->ticketSolver = $ticketSolver;
    }

    public static function getSubscribedEvents()
    {
        return [
                TicketEvent::NEW_MESSAGE_EVENT => ['sendNotificationAfterNewMessage'],
                TicketEvent::NEW_TICKET_EVENT => ['sendNotificationAfterNewTicket'],
        ];
    }


    public function sendNotificationAfterNewTicket(TicketEvent $ticketEvent)
    {
        $ticket = $ticketEvent->getTicket();
        $lastMessage = $this->ticketSolver->getLastMessage($ticket);
        if ($ticket->getUser()) {
            $firstname = $ticket->getUser()->getFirstname();
            $email = $ticket->getUser()->getEmail();
        } else {
            $firstname = $ticket->getVisitorFirstname();
            $email = $ticket->getVisitorEmail();
        }

        if ($lastMessage->getAnswerByAdmin()) {
            // envoi email au client
            $this->mailMailjetNotification->send(
                    [
                            MailMailjetNotification::SUBJECT => 'Vous avez reçu un nouveau message',
                            MailMailjetNotification::TO_EMAIL => $email,
                            MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-new-by-admin',
                            MailMailjetNotification::CONTENT_DATA => [
                                    'firstname' => $firstname,
                                    'ticket' => $ticket,
                            ],
                    ]
            );
        } else {
            $this->mailMailjetNotification->send(
                    [
                            MailMailjetNotification::SUBJECT => 'Nouvelle demande',
                            MailMailjetNotification::TO_EMAIL => $email,
                            MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-new',
                            MailMailjetNotification::CONTENT_DATA => [
                                    'firstname' => $firstname,
                                    'ticket' => $ticket,
                            ],
                    ]
            );
        }

        $this->notifyUser($ticket);
    }

    public function sendNotificationAfterNewMessage(TicketEvent $ticketEvent)
    {
        $ticket = $ticketEvent->getTicket();
        $lastMessage = $this->ticketSolver->getLastMessage($ticket);
        if ($ticket->getUser()) {
            $firstname = $ticket->getUser()->getFirstname();
            $email = $ticket->getUser()->getEmail();
        } else {
            $firstname = $ticket->getVisitorFirstname();
            $email = $ticket->getVisitorEmail();
        }

        if ($lastMessage->getAnswerByAdmin()) {
            // envoi email au client
            $this->mailMailjetNotification->send(
                    [
                            MailMailjetNotification::SUBJECT => 'Réponse à votre demande',
                            MailMailjetNotification::TO_EMAIL => $email,
                            MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-response',
                            MailMailjetNotification::CONTENT_DATA => [
                                    'firstname' => $firstname,
                                    'ticket' => $ticket,
                            ],
                    ]
            );
        }
        $this->notifyUser($ticket);
    }

    protected function notifyUser(TicketInterface $ticket): void
    {
        // notifyAdmin
        $usersToNotify = $this->userStore->getByTicketTypesNotification($ticket->getType());

        foreach ($usersToNotify as $userToNotify) {
            if ($this->authorizationChecker->isGranted('ROLE_ADMIN', $userToNotify)) {
                $this->mailMailjetNotification->send(
                        [
                                MailMailjetNotification::SUBJECT => 'Nouveau ticket sur Place du Local',
                                MailMailjetNotification::TO_EMAIL => $userToNotify->getEmail(),
                                MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-notification',
                                MailMailjetNotification::CONTENT_DATA => [
                                        'firstname' => $userToNotify->getFirstname(),
                                        'ticket' => $ticket,
                                        'ticketMessage' => $ticket->getTicketMessages()[0],
                                ],
                        ]
                );
            }
        }
    }


}