<?php

namespace AutoAdmin\Bundle\Seo\Extractor;

use AutoAdmin\Bundle\Content\Entity\Page;
use AutoAdmin\Bundle\Media\Library\MediaResolver;
use AutoAdmin\Bundle\Seo\Entity\SeoMetadata;
use AutoAdmin\Bundle\Seo\Model\Meta;
use AutoAdmin\Bundle\Seo\Model\OpenGraphContentInterface;
use AutoAdmin\Bundle\Seo\Model\SeoContentInterface;
use AutoAdmin\Bundle\Seo\Model\SeoPage;
use Symfony\Component\HttpFoundation\Request;

class TwitterExtractor extends AbstractExtractor
{
    public function __construct(
        protected MediaResolver $mediaResolver,
    ) {
    }

    public function supports($subject): bool
    {
        return $this->isTranslatable($subject)
            || $this->isTranslation($subject)
            || $this->isUntranslatedContent($subject);
    }

    public function extract(Request $request, object $subject, SeoPage $seoPage): SeoPage
    {
        [$translatable, $translation] = $this->getTranslatableTranslationObjects($subject);

        $homepage = $this->getHomepage();
        $homeSeo = $homepage->getTranslationSeoMetadata();

        $this->addTwitterTitle($homeSeo, $seoPage);
        $this->addTwitterDescription($homeSeo, $seoPage);
        $seoPage->addTwitterMeta(new Meta('twitter:card', 'summary_large_image'));
        if ($seoPage->getCanonical()) {
            $seoPage->addTwitterMeta(new Meta('twitter:url', $seoPage->getCanonical()));
        }
        $this->addTwitterImage($seoPage, $translatable, $homepage);

        return $seoPage;
    }

    private function addTwitterTitle(SeoMetadata $homepageSeo, SeoPage $seo): void
    {
        if ($title = $seo->getTitle()) {
            $seo->addTwitterMeta(new Meta('twitter:title', $title));
        } else {
            $seo->addTwitterMeta(new Meta('twitter:title', $homepageSeo->getTitle()));
        }
    }

    private function addTwitterDescription(?SeoMetadata $homeSeo, SeoPage $seo): void
    {
        $meta = new Meta('twitter:description', '');
        /** @var Meta $item */
        foreach ($seo->getMetas() as $item) {
            if ($item->getName() === 'description') {
                $meta->setValue($item->getValue());
            }
        }

        if ($meta->getValue() === '' && $desc = $homeSeo->getDescription()) {
            $meta->setValue($desc);
        }

        if ($meta->getValue()) {
            $seo->addOpenGraphMeta($meta);
        }
    }

    // TODO: use Twitter image instead of OG image
    private function addTwitterImage(SeoPage $seo, $subject, Page $homepage): void
    {
        $fallback = [$subject, $homepage];
        foreach ($fallback as $content) {
            if ($content instanceof OpenGraphContentInterface) {
                $og = $content->getTranslationOpenGraph();
                if ($stringMedia = $og->getImage()) {
                    $url = $this->mediaResolver->resolveUrl($stringMedia);
                    if ($url) {
                        $parse = parse_url($seo->getCanonical());
                        $host = $parse['host'];

                        $seo->addOpenGraphMeta(new Meta('twitter:image', 'https://'.$host.$url));
                        break;
                    }
                }
            }
        }
    }
}
