<?php

namespace Lc\SovBundle\Controller\User;

use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Lc\SovBundle\Container\User\UserContainer;
use Lc\SovBundle\Controller\AbstractAdminController;
use Lc\SovBundle\Definition\ActionDefinition;
use Lc\SovBundle\Definition\RolesDefinition;
use Lc\SovBundle\Definition\RolesDefinitionInterface;
use Lc\SovBundle\Doctrine\EntityManager;
use Lc\SovBundle\Factory\User\UserFactory;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

abstract class UserAdminController extends AbstractAdminController
{
    public function buildIndexActions(Actions $actions): void
    {
        parent::buildIndexActions($actions); // TODO: Change the autogenerated stub

        $actions->add(Crud::PAGE_INDEX, $this->getSwitchUserAction());
    }

    public function getSwitchUserAction(): Action
    {
        $switchAction = Action::new(
            ActionDefinition::SWITCH_USER,
            $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SWITCH_USER),
            'fa fa-fw fa-user-secret'
        )
            ->linkToCrudAction(ActionDefinition::SWITCH_USER)
            ->setLabel($this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SWITCH_USER))
            ->setCssClass('in-dropdown text-info action-confirm action_switch');

        return $switchAction;
    }

    public function overrideEntitiesActions(?EntityCollection $entities, string $pageName): void
    {
        parent::overrideEntitiesActions($entities, $pageName); // TODO: Change the autogenerated stub
        foreach ($entities as $entity) {
            foreach ($entity->getActions() as $action) {
                if ($action->getName() == ActionDefinition::SWITCH_USER) {
                    $url = $this->generateUrl(
                        $this->getParameter('lc_sov.homepage_route'),
                        array('_switch_user' => $entity->getInstance()->getEmail())
                    );
                    $action->setLinkUrl($url);
                }
            }
        }
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            EmailField::new('email'),
            TextField::new('lastname'),
            TextField::new('firstname'),
            ChoiceField::new('roles')
                ->allowMultipleChoices()
                ->autocomplete()
                ->setChoices($this->getUserContainer()->getRoleDefinition()->getRolesList())
        ];
    }

    public function createEntity(string $entityFqcn)
    {
        return $this->get(UserContainer::class)->getFactory()->create();
    }
}
