<?php

namespace App\EventSubscriber;

use App\Repository\EventRepository;
use App\Repository\ProductsRepository;
use Cocur\Slugify\Slugify;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Sitemap\Url\GoogleImage;
use Presta\SitemapBundle\Sitemap\Url\GoogleImageUrlDecorator;
use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

#[AsEventListener(event: SitemapPopulateEvent::class, method: 'onSitemapPopulate', priority: 10)]
class SitemapSubscriber
{
    public function __construct(private readonly EventRepository $eventRepository,private readonly ProductsRepository $productsRepository,private CacheManager $cacheManager)
    {
    }

    public function onSitemapPopulate(SitemapPopulateEvent $event) {
        $urlContainer = $event->getUrlContainer();
        $urlGenerator = $event->getUrlGenerator();

        $langs = ["fr","en","cn"];

        $urlHome = new UrlConcrete($urlGenerator->generate('app_about', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $decoratedUrlHome = new GoogleImageUrlDecorator($urlHome);
        $decoratedUrlHome->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $decoratedUrlHome->addImage(new GoogleImage($this->cacheManager->resolve('assets/partenair/couronnes-d-or.jpg','webp')));
        $decoratedUrlHome->addImage(new GoogleImage($this->cacheManager->resolve('assets/partenair/cosplay-familly.jpg','webp')));
        $decoratedUrlHome->addImage(new GoogleImage($this->cacheManager->resolve('assets/partenair/house-of-geek.jpg','webp')));
        $decoratedUrlHome->addImage(new GoogleImage($this->cacheManager->resolve('assets/partenair/siteconseil.png','webp')));
        $decoratedUrlHome = new GoogleMultilangUrlDecorator($decoratedUrlHome);
        foreach ($langs as $lang) {
            $decoratedUrlHome->addLink($urlGenerator->generate('app_home',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($decoratedUrlHome, 'default');

        $urlMembers = new UrlConcrete($urlGenerator->generate('app_members', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $decoratedUrlMembers = new GoogleImageUrlDecorator($urlMembers);
        $decoratedUrlMembers->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $decoratedUrlMembers->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/shoko.jpg','webp')));
        $decoratedUrlMembers->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/marta.jpg','webp')));
        $decoratedUrlMembers = new GoogleMultilangUrlDecorator($decoratedUrlMembers);
        foreach ($langs as $lang) {
            $decoratedUrlMembers->addLink($urlGenerator->generate('app_members',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($decoratedUrlMembers, 'default');

        $urlMembers = new UrlConcrete($urlGenerator->generate('app_recruit', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $decoratedUrlMembers = new GoogleImageUrlDecorator($urlMembers);
        $decoratedUrlMembers->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $decoratedUrlMembers = new GoogleMultilangUrlDecorator($decoratedUrlMembers);
        foreach ($langs as $lang) {
            $decoratedUrlMembers->addLink($urlGenerator->generate('app_recruit',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($decoratedUrlMembers, 'default');

        $urlMembers = new UrlConcrete($urlGenerator->generate('app_pages', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $decoratedUrlMembers = new GoogleImageUrlDecorator($urlMembers);
        $decoratedUrlMembers->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $decoratedUrlMembers = new GoogleMultilangUrlDecorator($decoratedUrlMembers);
        foreach ($langs as $lang) {
            $decoratedUrlMembers->addLink($urlGenerator->generate('app_pages',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($decoratedUrlMembers, 'pages');

        $urlMembers = new UrlConcrete($urlGenerator->generate('app_pages_discover', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $decoratedUrlMembers = new GoogleImageUrlDecorator($urlMembers);
        $decoratedUrlMembers->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $decoratedUrlMembers = new GoogleMultilangUrlDecorator($decoratedUrlMembers);
        foreach ($langs as $lang) {
            $decoratedUrlMembers->addLink($urlGenerator->generate('app_pages_discover',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($decoratedUrlMembers, 'pages');

        $urlEvents = new UrlConcrete($urlGenerator->generate('app_events', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $urlEvents = new GoogleImageUrlDecorator($urlEvents);
        $urlEvents->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $urlEvents = new GoogleMultilangUrlDecorator($urlEvents);
        foreach ($langs as $lang) {
            $urlEvents->addLink($urlGenerator->generate('app_events',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($urlEvents, 'default');

        $urlShop = new UrlConcrete($urlGenerator->generate('app_shop', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $urlShop = new GoogleImageUrlDecorator($urlShop);
        $urlShop->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $urlShop = new GoogleMultilangUrlDecorator($urlShop);
        foreach ($langs as $lang) {
            $urlShop->addLink($urlGenerator->generate('app_shop',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($urlShop, 'default');

        $urlEvents = new UrlConcrete($urlGenerator->generate('app_events', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $urlEvents = new GoogleImageUrlDecorator($urlEvents);
        $urlEvents->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $urlEvents = new GoogleMultilangUrlDecorator($urlEvents);
        foreach ($langs as $lang) {
            $urlEvents->addLink($urlGenerator->generate('app_events',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($urlEvents, 'default');

        $urlDons = new UrlConcrete($urlGenerator->generate('app_dons', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $urlDons = new GoogleImageUrlDecorator($urlDons);
        $urlDons->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $urlDons = new GoogleMultilangUrlDecorator($urlDons);
        foreach ($langs as $lang) {
            $urlDons->addLink($urlGenerator->generate('app_dons',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($urlDons, 'default');

        $urlContact = new UrlConcrete($urlGenerator->generate('app_contact', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $urlContact = new GoogleImageUrlDecorator($urlContact);
        $urlContact->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $urlContact = new GoogleMultilangUrlDecorator($urlContact);
        foreach ($langs as $lang) {
            $urlContact->addLink($urlGenerator->generate('app_contact',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($urlContact, 'default');

        $urlDoc = new UrlConcrete($urlGenerator->generate('app_doc', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $urlDoc = new GoogleImageUrlDecorator($urlDoc);
        $urlDoc->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $urlDoc = new GoogleMultilangUrlDecorator($urlDoc);
        foreach ($langs as $lang) {
            $urlContact->addLink($urlGenerator->generate('app_doc',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($urlDoc, 'default');

        $urlAbout = new UrlConcrete($urlGenerator->generate('app_about', [], UrlGeneratorInterface::ABSOLUTE_URL));
        $decoratedUrlAbout = new GoogleImageUrlDecorator($urlAbout);
        $decoratedUrlAbout->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
        $decoratedUrlAbout->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/shoko.jpg','webp')));
        $decoratedUrlAbout->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/marta.jpg','webp')));
        $decoratedUrlAbout = new GoogleMultilangUrlDecorator($decoratedUrlAbout);
        foreach ($langs as $lang) {
            $decoratedUrlAbout->addLink($urlGenerator->generate('app_about',['lang'=>$lang], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
        }
        $urlContainer->addUrl($decoratedUrlAbout, 'default');

        $s = new Slugify();
        foreach ($this->eventRepository->findAll() as $eventItem) {
            $urlAbout = new UrlConcrete($urlGenerator->generate('app_event_details', ['id'=>$eventItem->getid()], UrlGeneratorInterface::ABSOLUTE_URL));
            $decoratedUrlAbout = new GoogleImageUrlDecorator($urlAbout);
            $decoratedUrlAbout->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
            $decoratedUrlAbout->addImage(new GoogleImage($this->cacheManager->resolve($eventItem->getAffiche(),'webp')));
            $decoratedUrlAbout = new GoogleMultilangUrlDecorator($decoratedUrlAbout);
            foreach ($langs as $lang) {
                $decoratedUrlAbout->addLink($urlGenerator->generate('app_event_details',['id'=>$eventItem->getid()], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
            }
            $urlContainer->addUrl($decoratedUrlAbout, 'events');
        }
        foreach ($this->productsRepository->findAll() as $product) {
            $slug = $s->slugify($product->getName()."-".$product->getId());
            $urlAbout = new UrlConcrete($urlGenerator->generate('app_product_show', ['slug'=>$slug], UrlGeneratorInterface::ABSOLUTE_URL));
            $decoratedUrlAbout = new GoogleImageUrlDecorator($urlAbout);
            $decoratedUrlAbout->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
            $decoratedUrlAbout->addImage(new GoogleImage($this->cacheManager->resolve($product->getImage(),'webp')));
            $decoratedUrlAbout = new GoogleMultilangUrlDecorator($decoratedUrlAbout);
            foreach ($langs as $lang) {
                $decoratedUrlAbout->addLink($urlGenerator->generate('app_product_show',['lang'=>$lang,'slug'=>$slug], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
            }
            $urlContainer->addUrl($decoratedUrlAbout, 'products');
        }

        $cites =[
            "Tergnier",
            "Beautor",
            "La Fére",
            "Chauny",
            "Condren",
            "Saint-Quentin",
            "Laon",
            "Soissons",
            "Gauchy",
            "Fargnier",
            "Quessy",
            "Guny"
        ];
        foreach ($cites as $cite) {
            $slug = $s->slugify($cite);
            $urlAbout = new UrlConcrete($urlGenerator->generate('app_who', ['city'=>$slug], UrlGeneratorInterface::ABSOLUTE_URL));
            $decoratedUrlAbout = new GoogleImageUrlDecorator($urlAbout);
            $decoratedUrlAbout->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
            $decoratedUrlAbout = new GoogleMultilangUrlDecorator($decoratedUrlAbout);
            foreach ($langs as $lang) {
                $decoratedUrlAbout->addLink($urlGenerator->generate('app_who',['lang'=>$lang,'city'=>$slug], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
            }
            $urlContainer->addUrl($decoratedUrlAbout, 'cities');

            $slug = $s->slugify($cite);
            $urlAbout = new UrlConcrete($urlGenerator->generate('app_who_2', ['city'=>$slug], UrlGeneratorInterface::ABSOLUTE_URL));
            $decoratedUrlAbout = new GoogleImageUrlDecorator($urlAbout);
            $decoratedUrlAbout->addImage(new GoogleImage($this->cacheManager->resolve('assets/images/logo.jpg','webp')));
            $decoratedUrlAbout = new GoogleMultilangUrlDecorator($decoratedUrlAbout);
            foreach ($langs as $lang) {
                $decoratedUrlAbout->addLink($urlGenerator->generate('app_who_2',['lang'=>$lang,'city'=>$slug], UrlGeneratorInterface::ABSOLUTE_URL), $lang);
            }
            $urlContainer->addUrl($decoratedUrlAbout, 'cities');
        }

    }
}
