<?php

declare(strict_types=1);

/*
 * This file is part of the VitiCore 2.
 *
 * (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\Routing;

use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RouteCollection;

class VitiCoreLoader extends Loader
{
    private string $environment;

    public function __construct(KernelInterface $kernel)
    {
        $this->environment = $kernel->getEnvironment();
        parent::__construct($this->environment);
    }

    public function load(mixed $resource, ?string $type = null): RouteCollection
    {
        $routes = new RouteCollection();

        $this->importYaml($routes, '@CpCreationVitiCoreBundle/Resources/config/routing.yaml');
        $this->importYaml($routes, '@CpCreationVitiCoreBundle/Resources/config/routes/site_media.yaml');
        $this->importYaml($routes, '@CpCreationVitiCoreBundle/Resources/config/routes/presta_sitemap.yaml');

        if ('dev' === $this->environment) {
            $this->importYaml($routes, '@CpCreationVitiCoreBundle/Resources/config/routes/dev/twig.yaml');
            $this->importYaml($routes, '@CpCreationVitiCoreBundle/Resources/config/routes/dev/web_profiler.yaml');
        }

        $this->importYaml($routes, '@CpCreationVitiCoreBundle/Resources/config/routes/api_platform.yaml');
        $this->importYaml($routes, '@CpCreationVitiCoreBundle/Resources/config/routes/fos_js_routing.yaml');
        $this->importYaml($routes, '@CpCreationVitiCoreBundle/Resources/config/routes/imagine.yaml');
        $this->importYaml($routes, '@CpCreationVitiCoreBundle/Resources/config/routes/ux_live_component.yaml');

        return $routes;
    }

    public function supports(mixed $resource, ?string $type = null): bool
    {
        return 'viti-core' === $type;
    }

    private function importYaml(RouteCollection $routes, string $resource): void
    {
        $importedRoutes = $this->import($resource, 'yaml');
        $routes->addCollection($importedRoutes);
    }
}
