<?php

namespace AutoAdmin\Bundle\Content\Translation;

use AutoAdmin\Bundle\Content\Context\ContentContext;
use AutoAdmin\Bundle\Content\Model\TranslatableInterface;
use AutoAdmin\Bundle\Content\Routing\ContentRouter;
use AutoAdmin\Bundle\Content\Site\SiteContextHelper;
use AutoAdmin\Bundle\Content\Translation\Model\LocaleSwitch;

class LocaleSwitchHelper
{
    public const PUBLIC_PATH_PATTERN = "/assets/images/locales/%s.png";

    public function __construct(
        protected SiteContextHelper $contextHelper,
        protected ContentContext $contentContext,
        protected ContentRouter $contentRouter,
        protected EntityTranslator $entityTranslator,
    ) {
    }

    /**
     * @return LocaleSwitch[]
     */
    protected function siteConfigsToLocaleSwitch(): array
    {
        $localeSwitchers = [];

        foreach ($this->contextHelper->getCurrentContextSiteConfigs() as $siteConfig) {
            $localeSwitchers[] = new LocaleSwitch(
                $siteConfig,
                null,
                $siteConfig->getDomain()->getDomain(),
                sprintf(self::PUBLIC_PATH_PATTERN, $siteConfig->getLocale()),
            );
        }

        return $localeSwitchers;
    }

    protected function prepareDataCurrentContentPass(?object $content = null): array
    {
        return [
            "main_content" => $content,
        ];
    }

    /**
     * @param LocaleSwitch[] $localeSwitches
     * @return LocaleSwitch[]
     */
    protected function localeSwitchCurrentContentPass(array $localeSwitches, array $data = []): array
    {
        if ($dataContent = $data['main_content']) {
            $content = $dataContent;
        } else {
            $content = $this->contentContext->getMainContent();
        }

        foreach ($localeSwitches as $k => $localeSwitch) {
            $locale = $localeSwitch->getLocale();
            $localeCode = $locale->getCode();
            $url = null;
            if ($content) {
                if (
                    $content instanceof TranslatableInterface
                    && $content->getCurrentTranslation()
                    && $content->getCurrentTranslation()->getLocale() !== $locale
                ) {
                    $content = $this->entityTranslator->initializeTranslationInClone($content, $locale);
                }

                $generated = $this->contentRouter->generateSafe(
                    $content,
                    'chain',
                    [
                        '_locale' => $localeCode,
                    ],
                    [
                        'locale' => $localeCode,
                        'host' => $localeSwitch->getHost(),
                    ]
                );

                if ($generated) {
                    $url = $generated;
                }
            }
            $localeSwitch->setUrl($url);
            $localeSwitches[$k] = $localeSwitch;
        }

        return $localeSwitches;
    }

    public function buildCurrentSwitch(?object $content = null): ?LocaleSwitch
    {
        $localeSwitches = array_values(
            array_filter(
                $this->siteConfigsToLocaleSwitch(),
                fn(LocaleSwitch $ls) => $ls->getLocale() === $this->contextHelper->getLocale()
            )
        );
        $localeSwitches = $this->localeSwitchCurrentContentPass(
            $localeSwitches,
            $this->prepareDataCurrentContentPass($content),
        );

        return $localeSwitches[0] ?? null;
    }

    /**
     * @return LocaleSwitch[]
     */
    public function buildAlternateSwitches(?object $content = null): array
    {
        $localeSwitches = array_values(
            array_filter(
                $this->siteConfigsToLocaleSwitch(),
                fn(LocaleSwitch $ls) => $ls->getLocale() !== $this->contextHelper->getLocale()
            )
        );
        $localeSwitches = $this->localeSwitchCurrentContentPass(
            $localeSwitches,
            $this->prepareDataCurrentContentPass($content),
        );

        return $localeSwitches;
    }
}
