<?php


namespace AutoAdmin\Bundle\Admin\DependencyInjection\Compiler;


use AutoAdmin\Bundle\Admin\Configuration\ConfigManager;
use AutoAdmin\Bundle\Admin\Factory\BuilderFactory;
use AutoAdmin\Bundle\Admin\Factory\ContentFactory;
use AutoAdmin\Bundle\Admin\Finder\EntityFinder;
use AutoAdmin\Bundle\Admin\Helper\AccessHelper;
use AutoAdmin\Bundle\Admin\Helper\EntityHelper;
use AutoAdmin\Bundle\Admin\Router\AdminRouter;
use AutoAdmin\Bundle\Content\Routing\ContentRouter;
use AutoAdmin\Bundle\Content\Service\DuplicatorService;
use AutoAdmin\Bundle\Content\Site\SiteContextHelper;
use AutoAdmin\Bundle\Seo\Generator\GeneratorManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class ControllerCompilerPass implements CompilerPassInterface
{
    /**
     * @inheritDoc
     */
    public function process(ContainerBuilder $container)
    {
        $deps = [
            'setEntityHelper' => new Reference(EntityHelper::class),
            'setBuilderFactory' => new Reference(BuilderFactory::class),
            'setAdminRouter' => new Reference(AdminRouter::class),
            'setContentFactory' => new Reference(ContentFactory::class),
            'setAccessHelper' => new Reference(AccessHelper::class),
            'setConfigManager' => new Reference(ConfigManager::class),
            'setContentRouter' => new Reference(ContentRouter::class),
            'setSiteContextHelper' => new Reference(SiteContextHelper::class),
            'setEntityFinder' => new Reference(EntityFinder::class),
            'setSeoGenerator' => new Reference(GeneratorManager::class),
            'setEm' => new Reference(EntityManagerInterface::class),
            'setDuplicatorService' => new Reference(DuplicatorService::class),
        ];

        $extraConfigServices = $container->findTaggedServiceIds('aa_admin.controller');
        foreach ($extraConfigServices as $id => $value) {
            $def = $container->getDefinition($id);
            if (!$def->hasTag('controller.service_arguments')) {
                $def->addTag('controller.service_arguments');
            }
            foreach ($deps as $k => $arg) {
                $def->addMethodCall($k, [$arg]);
            }
            $def->setAutowired(true);
        }
    }
}
