<?php


namespace AutoAdmin\Bundle\Seo\Sitemap;


use AutoAdmin\Bundle\Seo\Sitemap\Model\SitemapUrl;
use Exception;
use Thepixeldeveloper\Sitemap\Extensions\Image;
use Thepixeldeveloper\Sitemap\Url;
use Thepixeldeveloper\Sitemap\Urlset;

class SitemapManager
{
    /**
     * @var array<SitemapBuilderInterface>
     */
    private array $builders;

    public function __construct(array $builders = [])
    {
        $this->builders = $builders;
    }

    public function getSections(): array
    {
        $sections = [];
        foreach ($this->builders as $builder) {
            foreach ($builder->getSections() as $section) {
                $sections[] = $section;
            }
        }

        return $sections;
    }

    /**
     * @throws Exception
     */
    public function build($host, $section = null): array
    {
        $urlSets = [];

        /** @var SitemapBuilderInterface $builder */
        foreach ($this->builders as $builder) {
            if ($section !== null && !in_array($section, $builder->getSections())) {
                continue;
            }
            $urls = $builder->getUrls();

            /** @var SitemapUrl $url */
            foreach ($urls as $sitemapUrl) {
                if ($section !== null && $sitemapUrl->getSection() !== $section) {
                    continue;
                }

                if (!$sitemapUrl instanceof SitemapUrl) {
                    throw new Exception(
                        'Sitemap urls must be represented by ' . SitemapUrl::class . '. "' . gettype($url) . '" given.'
                    );
                }

                $url = new Url($host . $sitemapUrl->getUrl());
                if ($sitemapUrl->getLastModification()) {
                    $url->setLastMod($sitemapUrl->getLastModification());
                }
                if ($sitemapUrl->getChangeFrequency()) {
                    $url->setChangeFreq($sitemapUrl->getChangeFrequency());
                }
                if ($sitemapUrl->getPriority()) {
                    $url->setPriority($sitemapUrl->getPriority());
                }

                if (!empty($images = $sitemapUrl->getImages())) {
                    foreach ($images as $image) {
                        if (is_string($image)) {
                            $url->addExtension(new Image($image));
                        } else {
                            // permit to use image object (\Thepixeldeveloper\Sitemap\Extensions\Image)
                            $url->addExtension($image);
                        }
                    }
                }

                if (!isset($urlSets[$section = $sitemapUrl->getSection()])) {
                    $urlSets[$section] = new Urlset();
                }
                $urlSets[$section]->add($url);
            }
        }

        return $urlSets;
    }
}
