<?php

namespace AutoAdmin\Bundle\Content\Twig\Extension;

use AutoAdmin\Bundle\Content\ContentReference\ContentReferenceResolver;
use AutoAdmin\Bundle\Content\Entity\ContentReference;
use AutoAdmin\Bundle\Content\Model\NameInterface;
use AutoAdmin\Bundle\Content\Model\TitleInterface;
use AutoAdmin\Bundle\Content\Model\TranslatableInterface;
use AutoAdmin\Bundle\Content\Routing\ContentRouter;
use AutoAdmin\Bundle\Content\Service\GdaApiService;
use AutoAdmin\Bundle\Content\Service\ObfuscatorService;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

class ContentExtension extends AbstractExtension
{
    private ContentRouter $contentRouter;
    private ContentReferenceResolver $referenceResolver;
    private GdaApiService $gdaApiService;
    private EntityManagerInterface $em;

    public function __construct(
        ContentRouter $contentRouter,
        ContentReferenceResolver $referenceResolver,
        GdaApiService $gdaApiService,
        EntityManagerInterface $em
    ) {
        $this->contentRouter = $contentRouter;
        $this->referenceResolver = $referenceResolver;
        $this->gdaApiService = $gdaApiService;
        $this->em = $em;
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('aa_content_path', [$this, 'getContentPath']),
            new TwigFunction('aa_content_path_safe', [$this, 'getContentPathSafe']),
            new TwigFunction('aa_content_title', [$this, 'getContentTitle']),
            new TwigFunction('aa_content_ref_path', [$this, 'getContentReferencePath']),
            new TwigFunction('aa_get_last_actu', [$this, 'getGdalLastActu']),
        ];
    }

    public function getFilters()
    {
        return [
            new TwigFilter('aa_raw', [$this, 'handleRaw']),
        ];
    }

    public function getContentPath($content, array $parameters = [], ?string $action = null, array $options = []): ?string
    {
        if ($content === null) {
            return '#nullcontent';
        }

        return $this->contentRouter->generate($content, $action, $parameters, $options);
    }

    public function getContentPathSafe($content, array $parameters = [], ?string $action = null, array $options = []): ?string
    {
        return $this->contentRouter->generateSafe($content, $action, $parameters, $options);
    }

    public function getContentTitle(?object $content = null)
    {
        $title = null;

        if (empty($content)) {
            return $title;
        }

        if ($content instanceof TranslatableInterface) {
            $content = $content->getCurrentTranslation();
        }

        if ($content instanceof NameInterface) {
            $title = $content->getName();
        } elseif ($content instanceof TitleInterface) {
            $title = $content->getTitle();
        }

        return $title;
    }

    public function getContentReferencePath($var): ?string
    {
        if (!$var instanceof ContentReference) {
            return null;
        }

        $resolved = $this->referenceResolver->resolve($var);
        if (empty($resolved)) {
            return null;
        }

        return $this->getContentPathSafe($resolved);
    }

    public function getGdalLastActu($lang = 'fr'): array
    {
        return $this->gdaApiService->getLastActu($lang);
    }

    public function handleRaw($content)
    {
        return ObfuscatorService::obfuscate($content);
    }
}
