<?php

namespace Lc\SovBundle\EventSubscriber;

use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Translation\TranslatableMessage;

class FlashMessageAdminEventSubscriber implements EventSubscriberInterface
{
    protected $session ;

    public function __construct(SessionInterface $session)
    {
        $this->session = $session ;
    }

    public static function getSubscribedEvents(): array
    {
        return [
                AfterEntityPersistedEvent::class => ['flashMessageAfterPersist'],
                AfterEntityUpdatedEvent::class => ['flashMessageAfterUpdate'],
                AfterEntityDeletedEvent::class => ['flashMessageAfterDelete'],
        ];
    }

    public function flashMessageAfterPersist(AfterEntityPersistedEvent $event): void
    {
        $this->session->getFlashBag()->add('success', new TranslatableMessage('flash_message.create', [
                '%name%' => (string) $event->getEntityInstance(),
        ]));
    }

    public function flashMessageAfterUpdate(AfterEntityUpdatedEvent $event): void
    {
        $this->session->getFlashBag()->add('success', new TranslatableMessage('flash_message.update', [
                '%name%' => (string) $event->getEntityInstance(),
        ]));
    }

    public function flashMessageAfterDelete(AfterEntityDeletedEvent $event): void
    {
        $this->session->getFlashBag()->add('success', new TranslatableMessage('flash_message.delete', [
                '%name%' => (string) $event->getEntityInstance(),
        ]));
    }
}