<?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\SeoPage;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;

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

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

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

        /** @var Page $homepage */
        $homepage = $this->em->getRepository('App\\Entity\\Page')->contentFindOneBy(['_.homepage' => true,]);
        $homeSeo = $homepage->getTranslationSeoMetadata();

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

        return $seo;
    }

    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;
                    }
                }
            }
        }
    }
}
