<?php
namespace App\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Http\SecurityEvents;

class SwitchUserSubscriber implements EventSubscriberInterface
{
    private $router;

    /**
     * SwitchUserSubscriber constructor.
     *
     * @param RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    public function onSwitchUser(SwitchUserEvent $event)
    {
        // $request = $event->getRequest();

        // if ($request->hasSession() && $request->getSession()->has('_groupId')) {
        //     $group = $request->getSession()->get('_groupId');
        //     if ($group->getRoles()->contains('ROLE_SUPER_ADMIN')) {
        //         return new RedirectResponse($this->router->generate('sonata_admin_dashboard'));
        //     }
        // }

        // return new RedirectResponse($this->router->generate('index'));

        // if ($request->hasSession() && ($session = $request->getSession)) {
        //     $session->set(
        //         '_locale',
        //         // assuming your User has some getLocale() method
        //         $event->getTargetUser()->getLocale()
        //     );
        // }
    }

    public static function getSubscribedEvents()
    {
        return [
            // constant for security.switch_user
            SecurityEvents::SWITCH_USER => 'onSwitchUser',
        ];
    }
}
