<?php
namespace App\EventListener;

use App\Events\MLCEvents;
use App\Events\FluxEvent;
use App\Entity\Flux;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\UserEvent;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Templating\EngineInterface;

/**
 * Listener responsable des évènements liés aux flux (transfert, transaction, retrait, vente...)
 */
class MLCEventListener implements EventSubscriberInterface
{
    protected $em;
    protected $mailer;
    protected $params;
    protected $templating;

    public function __construct(EntityManagerInterface $em, \Swift_Mailer $mailer, ParameterBagInterface $params, EngineInterface $templating)
    {
        $this->em = $em;
        $this->mailer = $mailer;
        $this->params = $params;
        $this->templating = $templating;
    }

    public static function getSubscribedEvents()
    {
        return array(
            MLCEvents::FLUX => 'onFlux',
            MLCEvents::COTISATION_COTISATION => 'onCotisation',
            MLCEvents::COTISATION_COTISATION_ADHERENT => 'onCotisation',
            MLCEvents::COTISATION_COTISATION_PRESTATAIRE => 'onCotisation',
            MLCEvents::TRANSACTION_ADHERENT_ADHERENT => 'onTransaction',
            MLCEvents::TRANSACTION_ADHERENT_PRESTATAIRE => 'onTransaction',
            MLCEvents::TRANSACTION_PRESTATAIRE_ADHERENT => 'onTransaction',
            MLCEvents::TRANSACTION_PRESTATAIRE_PRESTATAIRE => 'onTransaction',
            MLCEvents::TRANSFERT_COMPTOIR_GROUPE => 'onTransfert',
            MLCEvents::TRANSFERT_GROUPE_COMPTOIR => 'onTransfert',
            MLCEvents::TRANSFERT_GROUPE_SIEGE => 'onTransfert',
            MLCEvents::TRANSFERT_SIEGE_GROUPE => 'onTransfert',
            MLCEvents::TRANSFERT_PRESTATAIRE_COMPTOIR => 'onDepot',
            MLCEvents::TRANSFERT_PRESTATAIRE_SIEGE => 'onReconversion',
            MLCEvents::RETRAIT_COMPTOIR_ADHERENT => 'onRetrait',
            MLCEvents::RETRAIT_COMPTOIR_PRESTATAIRE => 'onRetrait',
            MLCEvents::VENTE_COMPTOIR_ADHERENT => 'onVente',
            MLCEvents::VENTE_COMPTOIR_PRESTATAIRE => 'onVente',
        );
    }

    public function onFlux(FluxEvent $event)
    {
        foreach ($event->getFlux()->getUsersToNotify() as $user) {
            if ($user instanceof User && $user->getAlertemailflux()) {
                $this->sendMail($user, $event->getFlux());
            } elseif ($user == 'siege') {
                $users = $this->container->get('doctrine')
                    ->getEntityManager()
                    ->createQuery('SELECT u FROM App:User u WHERE u.roles LIKE :role')
                    ->setParameter('role', '%"ROLE_ADMIN_SIEGE"%')
                    ->getResult();
                foreach ($users as $userAdminSiege) {
                    $this->sendMail($userAdminSiege, $event->getFlux());
                }
            }
        }
    }

    private function sendMail(User $user, Flux $flux)
    {
        $subject = $this->params->get('app.mlc_name_small').' : '.ucwords($flux->getParenttype()).' '.ucwords($flux->getType());
        $mail = (new \Swift_Message($subject))
            ->setFrom($this->params->get('app.mlc_notif_email'))
            ->setTo($user->getEmail())
            ->setBody(
                $this->templating->render(
                    'email/notification_flux.html.twig',
                    array(
                        'subject' => $subject,
                        'user' => $user,
                        'flux' => $flux
                    )
                ),
                'text/html'
            );
        $this->mailer->send($mail);
    }

    public function onCotisation(FluxEvent $event)
    {
    }

    public function onTransaction(FluxEvent $event)
    {
    }

    public function onTransfert(FluxEvent $event)
    {
        if ($event->getUsers()) {
        }
    }

    public function onDepot(FluxEvent $event)
    {
    }

    public function onReconversion(FluxEvent $event)
    {
    }

    public function onRetrait(FluxEvent $event)
    {
    }

    public function onVente(FluxEvent $event)
    {
    }
}
