<?php

namespace AutoAdmin\Bundle\Content\Twig\Extension;

use AutoAdmin\Bundle\Content\Model\TranslatableInterface;
use AutoAdmin\Bundle\Content\Provider\TranslationProvider;
use AutoAdmin\Bundle\Content\Site\SiteContextHelper;
use AutoAdmin\Bundle\Content\Translation\EntityTranslator;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

class TranslationExtension extends AbstractExtension
{
    public function __construct(
        protected EntityTranslator $entityTranslator,
        protected TranslationProvider $translationProvider,
        protected SiteContextHelper $contextHelper,
    ) {
    }

    /**
     * @return TwigFilter[]
     */
    public function getFilters(): array
    {
        return [
            // return entity translation for field and locale: aa_content_translate(content, 'title', 'en')
            new TwigFilter('aa_content_translate', [$this, 'translate']),
            // return static trans: aa_trans('key')
            // fallback on symfony |trans system if nothing is found
            new TwigFilter('aa_trans', [$this, 'staticTranslate'], ['is_safe' => ['html']]),

        ];
    }

    public function getFunctions()
    {
        return [
            // lock/unlock static text locale from template
            new TwigFunction('aa_trans_lock', [$this, 'staticTranslateLock']),
            new TwigFunction('aa_trans_unlock', [$this, 'staticTranslateUnlock']),
            new TwigFunction('aa_site_locales', [$this, 'getSiteLocales']),
        ];
    }


    public function translate(TranslatableInterface $translatable, $field, $locale = null): ?string
    {
        return $this->entityTranslator->translate($translatable, $field, $locale);
    }

    public function staticTranslate(string $key, array $options = [], string $domain = 'messages', $locale = null)
    {
        return $this->translationProvider->translate($key, $domain, $options, $locale);
    }

    public function staticTranslateLock(mixed $locale): void
    {
        $this->translationProvider->lockLocale($locale);
    }

    public function staticTranslateUnlock(): void
    {
        $this->translationProvider->unlockLocale();
    }

    public function getSiteLocales(bool $checkDomain = true): array
    {
        $site = $this->contextHelper->getCurrentContext()->getSite();
        $domain = $this->contextHelper->getCurrentContext()->getDomain();
        $currentLocale = $this->contextHelper->getCurrentContext()->getLocale();

        $siteConfigs = $this->contextHelper->getSiteLocalizedDomains($site);
        $locales = [];
        foreach ($siteConfigs as $siteConfig) {
            $sameDomain = $siteConfig->getDomain() === $domain;
            if (!$checkDomain) {
                $sameDomain = true;// ignore domain
            }
            if ($sameDomain && $siteConfig->getLocale() !== $currentLocale) {
                $locales[] = $siteConfig->getLocale();
            }
        }

        return [
            'current' => $currentLocale,
            'alternates' => $locales,
        ];
    }
}
