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

use Ferienpass\AdminBundle\Form\CompoundType\PostalAddressesType;
use Ferienpass\AdminBundle\Form\Type\MarkdownType;
use Ferienpass\CoreBundle\Entity\Host;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\Validator\Constraints\File;
use Symfony\UX\Dropzone\Form\DropzoneType;

class EditHostType extends AbstractType
{
    public function __construct(private readonly SluggerInterface $slugger)
    {
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Host::class,
            'label_format' => 'hosts.label.%name%',
            'translation_domain' => 'admin',
            'required' => false,
        ]);
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', null, ['section' => 'default', 'width' => '2/3'])
            ->add('alias', null, [
                'section' => 'default',
                'width' => '1/3',
                'help' => 'hosts.help.alias',
            ])
            ->add('phone', null, ['section' => 'contact', 'width' => '1/2'])
            ->add('fax', null, ['section' => 'contact', 'width' => '1/2'])
            ->add('mobile', null, ['section' => 'contact', 'width' => '1/2'])
            ->add('email', EmailType::class, ['section' => 'contact', 'width' => '1/2|clr'])
            ->add('website', UrlType::class, ['section' => 'contact', 'width' => '1/2'])
            ->add('postalAddresses', PostalAddressesType::class, ['section' => 'address', 'label' => false])
            ->add('text', MarkdownType::class, ['attr' => ['rows' => 4], 'section' => 'default'])
            ->add('logo', DropzoneType::class, [
                'section' => 'media',
                'attr' => ['placeholder' => 'offers.dropzonePlaceholder'],
                'mapped' => false,
                'constraints' => [
                    new File(maxSize: '1024k', mimeTypes: [
                        'image/jpeg',
                        'image/png',
                        'image/svg+xml',
                    ], mimeTypesMessage: 'Erlaubte Dateitypen sind: JPG, PNG, SVG'),
                ],
            ])
            ->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
                $host = $event->getData();
                if ($host instanceof Host && null === $host->getAlias()) {
                    $host->generateAlias($this->slugger);
                }
            })
        ;
    }
}
