<?php


namespace AutoAdmin\Bundle\Menu\DependencyInjection;

use InvalidArgumentException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AutoAdminMenuExtension extends Extension implements PrependExtensionInterface
{
    public function prepend(ContainerBuilder $container)
    {
        $bundles = $container->getParameter('kernel.bundles');
        // $exts = $container->getExtensions();

        if (in_array('Knp\\Bundle\\MenuBundle\\KnpMenuBundle', $bundles, true)) {
            $config = [
                'twig' => [
                    // 'template' => 'KnpMenuBundle::menu.html.twig',
                    'template' => '@AutoAdminMenu/menu.html.twig',
                ],
            ];
            $container->prependExtensionConfig('knp_menu', $config);
        }
    }

    /**
     * Loads a specific configuration.
     *
     * @throws InvalidArgumentException When provided tag is not defined in this extension
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
        $loader->load('services.yaml');

        # todo: permit to override this template using autoadmin config
        //        $container->set
        //        $container->setParameter('knp_menu.renderer.twig.template', '@AutoAdminMenu/menu.html.twig');
        //        $adminCOnfig = $container->getExtension('auto_admin_admin');
        //        dump($adminCOnfig);
        //        die;
    }
}
