<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\AdminBundle\Form\Offer;

use Ferienpass\AdminBundle\Form\AsOfferFormType;
use Ferienpass\AdminBundle\Form\Type\OfferImagePickerType;
use Ferienpass\CoreBundle\Entity\DbafsMedia;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\OfferMedia;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

#[AsOfferFormType]
class Image extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->addEventListener(FormEvents::PRE_SET_DATA, static function (FormEvent $event): void {
            $offer = $event->getForm()->getParent()?->getData();
            if (!$offer instanceof OfferInterface) {
                return;
            }

            $firstMedia = $offer->getMedia()->first();
            if ($firstMedia instanceof OfferMedia) {
                $event->setData($firstMedia->getMedia());
            }
        });

        $builder->addEventListener(FormEvents::POST_SUBMIT, static function (FormEvent $event): void {
            $offer = $event->getForm()->getParent()?->getData();
            $selectedMedia = $event->getData();

            if (!$offer instanceof OfferInterface || !$selectedMedia instanceof DbafsMedia) {
                return;
            }

            $mediaCollection = $offer->getMedia();

            // Find if the selected media is already in the collection
            $existingOfferMedia = null;
            foreach ($mediaCollection as $om) {
                if ($om->getMedia()->getUuid()->equals($selectedMedia->getUuid())) {
                    $existingOfferMedia = $om;
                    break;
                }
            }

            if (null === $existingOfferMedia) {
                $offer->addMedia($selectedMedia);
                // Need to find it again after addMedia as it creates a new OfferMedia object
                foreach ($offer->getMedia() as $om) {
                    if ($om->getMedia()->getUuid()->equals($selectedMedia->getUuid())) {
                        $existingOfferMedia = $om;
                        break;
                    }
                }
            }

            if ($existingOfferMedia) {
                $existingOfferMedia->setSorting(0);
                $i = 1;
                foreach ($mediaCollection as $om) {
                    if ($om !== $existingOfferMedia) {
                        $om->setSorting($i++);
                    }
                }
            }
        });
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'section' => 'media',
            'label' => 'Vorschaubild',
            'help' => 'Wählen Sie ein Bild aus, das als Vorschaubild für das Angebot angezeigt werden soll.',
            'required' => false,
            'mapped' => false,
        ]);
    }

    public function getParent(): string
    {
        return OfferImagePickerType::class;
    }
}
