<?php

namespace App\Framework\View;

use App\Framework\Cache\Cache;
use App\Framework\Core\PathProvider;
use App\Framework\DI\DefaultContainer;
use App\Framework\DI\Initializer;
use App\Framework\Discovery\FileScanner;
use App\Framework\Discovery\Results\DiscoveryResults;
use App\Framework\View\Loading\Resolvers\ControllerResolver;
use App\Framework\View\Loading\Resolvers\DefaultPathResolver;
use App\Framework\View\Loading\Resolvers\DiscoveryResolver;
use App\Framework\View\Loading\Resolvers\TemplateMapResolver;
use App\Framework\View\Loading\TemplateLoader;
use App\Framework\View\Loading\TemplatePathResolver;
use App\Framework\View\Processors\AssetInjector;
use App\Framework\View\Processors\CommentStripProcessor;
use App\Framework\View\Processors\ComponentProcessor;
use App\Framework\View\Processors\CsrfReplaceProcessor;
use App\Framework\View\Processors\FormProcessor;
use App\Framework\View\Processors\ForProcessor;
use App\Framework\View\Processors\HoneypotProcessor;
use App\Framework\View\Processors\LayoutTagProcessor;
use App\Framework\View\Processors\MetaManipulator;
use App\Framework\View\Processors\PlaceholderReplacer;
use App\Framework\View\Processors\RemoveEmptyLinesProcessor;
use App\Framework\View\Processors\SingleLineHtmlProcessor;
use App\Framework\View\Processors\VoidElementsSelfClosingProcessor;

final readonly class TemplateRendererInitializer
{
    public function __construct(
        private DefaultContainer $container,
        private FileScanner      $scanner,
        private PathProvider     $pathProvider,
        private DiscoveryResults $results,
    ) {}

    #[Initializer]
    public function __invoke(): TemplateRenderer
    {
        $doms = [
            ComponentProcessor::class,
            LayoutTagProcessor::class,
            MetaManipulator::class,
            ForProcessor::class,
            AssetInjector::class,
            CommentStripProcessor::class,
            RemoveEmptyLinesProcessor::class,
            FormProcessor::class,
            HoneypotProcessor::class,
        ];

        $strings = [
            PlaceholderReplacer::class,
            SingleLineHtmlProcessor::class,
            VoidElementsSelfClosingProcessor::class,
            CsrfReplaceProcessor::class,
        ];

        $templateProcessor = new TemplateProcessor(
            [ComponentProcessor::class, ...$this->results->getInterfaceImplementations(DomProcessor::class)],
            $this->results->getInterfaceImplementations(StringProcessor::class),
            $this->container,
        );

        $templateProcessor = new TemplateProcessor($doms, $strings, $this->container);

        $this->container->singleton(TemplateProcessor::class, $templateProcessor);

        #ViewCacheBootstrap::quickEnable($this->container);

        /*$files = $this->scanner->findFiles($this->pathProvider->getSourcePath(), '*.view.php');

        $templates = [];
        foreach ($files as $file) {
            $templates[$file->getFilename()] = $file->getPathname();
        }*/

        $pathProvider = $this->container->get(PathProvider::class);
        $cache = $this->container->get(Cache::class);

        $loader = new TemplateLoader(pathProvider: $pathProvider, cache: $cache, discoveryResults: $this->results/*, templates: $templates*/);

        $this->container->singleton(TemplateLoader::class, $loader);

        return new Engine(
            $loader,
            $pathProvider,
            processor: $templateProcessor,
            container: $this->container,
            cache: $cache,
        );
    }
}
