<?php

namespace App\Form;

use App\Entity\Event;
use App\Entity\Sponsor;
use App\Entity\TypeEvent;
use App\Entity\EventStatus;
use App\Form\EventDateType;
use App\Repository\SponsorRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use App\Doctrine\DBAL\Type\EventStatusEnumType;
use Symfony\Component\Form\FormBuilderInterface;
use Vich\UploaderBundle\Form\Type\VichImageType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class EventType extends AbstractType
{
    public function __construct(private EntityManagerInterface $manager) {
        
    }
    
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $type_events = $this->manager->getRepository(TypeEvent::class)->findAllActive();

        $eventStatus = EventStatus::cases();

        $builder
            ->add('title', TextType::class, [ 
                'required' => true, 
                'attr' => ['maxlength' => 150],
                'translation_domain' => 'admin'
            ])

            ->add('imgPresentationFile', VichImageType::class, [
                'data_class' => null, 
                'allow_delete' => false,
                'download_uri' => false,
                'required' => false,
                'translation_domain' => 'admin'
                ])

            ->add('videoPresentation', TextType::class, [ 
                'required' => false, 
                'attr' => ['maxlength' => 255],
                'translation_domain' => 'admin' 
            ])

            ->add('subject', TextType::class, [ 
                'required' => true, 
                'attr' => ['maxlength' => 255],
                'translation_domain' => 'admin'
            ])

            ->add('type', ChoiceType::class, [
                'choices'  => $type_events,
                'choice_value' => 'id',
                'choice_label' => 'name',
                'choice_translation_domain' => false,
                'required' => true,
                'translation_domain' => 'admin'
            ])

            // ->add('status', ChoiceType::class, [
            //     'choices'  => $eventStatus,
            //     'choice_value' => 'value',
            //     'choice_label' => function(?EventStatus $e): string { return 'event-status.'.$e->value; },
            //     'choice_translation_domain' => 'fields',
            //     'required' => true,
            //     'translation_domain' => 'admin'
            // ])

            ->add('active', CheckboxType::class, [ 
                'required' => false,
                'translation_domain' => 'admin' 
            ]);
            
            $builder->add('performers', CollectionType::class, [
                'entry_type' => EventPerformerType::class,
                'entry_options' => ['label' => false],
                'allow_add' => true,
                'allow_delete' => true,
            ]);


            $builder->add('sponsors', CollectionType::class, [
                'entry_type' => EventSponsorType::class,
                'entry_options' => ['label' => false],
                'allow_add' => true,
                'allow_delete' => true,
            ]);

            
            $builder->add('dates', CollectionType::class, [
                'entry_type' => EventDateType::class,
                'entry_options' => ['label' => false],
                'allow_add' => true,
                'allow_delete' => true,
            ]);
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Event::class,
            // "allow_extra_fields" => true
        ]);
    }
}
