<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\DependencyInjection;

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

class CpCreationVitiCoreExtension extends Extension implements PrependExtensionInterface
{
    public function load(array $configs, ContainerBuilder $container): void
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yaml');
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        $container->setParameter('cp_creation_viti_core.payment_method', $config['payment_method']);
        $container->setParameter('cp_creation_viti_core.social_network', $config['social_network']);
        $container->setParameter('cp_creation_viti_core.return_path', $config['return_path']);
        $container->setParameter('cp_creation_viti_core.carriers', $config['carriers']);
        $container->setParameter('cp_creation_viti_core.cb_directory_paths', $config['cb_directory_paths']);
        $container->setParameter('cp_creation_viti_core.maintenance_email', $config['maintenance_email']);
        $container->setParameter('knp.doctrine_behaviors.translatable_subscriber.translatable_trait', 'CpCreation\VitiCore\Behavior\Impl\Translatable');
        $container->setParameter('knp.doctrine_behaviors.translatable_subscriber.translation_trait', 'CpCreation\VitiCore\Behavior\Impl\Translation');

        // $container->registerForAutoconfiguration(PaymentProviderInterface::class)->addTag('viti-core.provider.payment');
        // $container->registerForAutoconfiguration(PaymentMethodInterface::class)->addTag('viti-core.form.payment_method');
        // $container->registerForAutoconfiguration(SocialNetworkProviderInterface::class)->addTag('viti-core.provider.socialnetwork');
        // $container->registerForAutoconfiguration(SocialNetworkNetworkInterface::class)->addTag('viti-core.form.social_network');
        // $container->registerForAutoconfiguration(CarrierProviderInterface::class)->addTag('viti-core.provider.carrier');
    }

    public function prepend(ContainerBuilder $container): void
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('parameters.yaml');

        $package = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/packages'));
        $bundles = $container->getParameter('kernel.bundles');
        $env = $container->getParameterBag()->get('kernel.environment');
        if (isset($bundles['OneupFlysystemBundle'])) {
            $package->load('oneup_flysystem.yaml');
        }

        if (isset($bundles['MercureBundle'])) {
            $package->load('mercure.yaml');
        }

        if (isset($bundles['LiipImagineBundle'])) {
            $package->load('imagine.yaml');
        }
        if (isset($bundles['ApiPlatformBundle'])) {
            $package->load('api_platform.yaml');
        }
        if (isset($bundles['StofDoctrineExtensionsBundle'])) {
            $package->load('stof_doctrine_extensions.yaml');
        }
        if (isset($bundles['TwigBundle'])) {
            $package->load('twig.yaml');
            $package->load('twig_extensions.yaml');
        }
        if (isset($bundles['CpCreationVitiCoreBundle'])) {
            $package->load('viticore.yaml');
        }
        if (isset($bundles['DoctrineBehaviorsBundle'])) {
            $package->load('knp_doctrine_behaviors.yaml');
        }
        if (isset($bundles['DoctrineBundle'])) {
            $package->load('doctrine.yaml');
            $package->load('doctrine_migrations.yaml');
        }
        if (isset($bundles['A2lixTranslationFormBundle'])) {
            $package->load('a2lix.yaml');
        }
        if (isset($bundles['EightPointsGuzzleBundle'])) {
            $package->load('eight_points_guzzle.yaml');
        }
        if (isset($bundles['BazingaJsTranslationBundle'])) {
            $package->load('translations.yaml');
        }
        if (isset($bundles['KnpPaginatorBundle'])) {
            $package->load('knp_paginator.yaml');
        }
        $package->load('framework.yaml');
        $package->load('notifier.yaml');
        $package->load('security.yaml');
        $package->load('assets.yaml');
        $package->load('webpack_encore.yaml');
        $package->load('doctrine_migrations.yaml');
        $package->load('routing.yaml');
        $package->load('mailer.yaml');
        $package->load('csrf.yaml');
        $package->load('http_discovery.yaml');
        $package->load('nyholm_psr7.yaml');
        $package->load('property_info.yaml');
        $package->load('ux_turbo.yaml');
        $package->load('messenger.yaml');
        if (isset($bundles['NelmioSecurityBundle'])) {
            if ('dev' === $env) {
                $package->load('dev/nelmio_security.yaml');
            } else {
                $package->load('prod/nelmio_security.yaml');
            }
        }
        if (isset($bundles['NelmioCorsBundle'])) {
            $package->load('nelmio_cors.yaml');
        }
        if (isset($bundles['EndroidQrCodeBundle'])) {
            $package->load('endroid_qr_code.yaml');
        }
        if ('dev' === $env) {
            if (isset($bundles['ApiPlatformBundle'])) {
                $package->load('dev/api_platform.yaml');
            }
            $package->load('dev/hautelook_alice.yaml');
            $package->load('dev/monolog.yaml');
            if (isset($bundles['TwigBundle'])) {
                $package->load('dev/twig.yaml');
            }
            $package->load('dev/web_profiler.yaml');
        }
        if ('prod' === $env) {
            $package->load('prod/doctrine.yaml');
            $package->load('prod/monolog.yaml');
        }
    }
}
