<?php

namespace App\Form\Campaign\Report;

use App\Entity\Campaign\Report\Element;
use App\Entity\Campaign\Report\Element\Type;
use App\Entity\Campaign\Report\Element\Type\Preset;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ElementType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('type', EntityType::class, [
                'class' => Type::class,
                'choice_value' => 'id',
                'choice_label' => function ($type) {
                    return $type->getTitle();
                },
            ])
            ->add('preset', EntityType::class, [
                'class' => Preset::class,
                'choice_value' => 'id',
                'choice_label' => function ($type) {
                    return $type->getTitle();
                },
                'required' => false,
            ])
            ->add('title', TextType::class, [
                'label' => 'Title',
            ])
            ->add('value', TextareaType::class, [
                'label' => 'Options',
                'required' => false,
            ])
            ->add('options', TextareaType::class, [
                'label' => 'Options',
                'required' => false,
            ])
            ->add('orderLvl', IntegerType::class, [
                'label' => 'Order',
                'required' => false,
            ])
            ->add('subjectType', TextType::class, [
                'label' => 'Subject Type',
            ])
            ->add('subjectId', IntegerType::class, [
                'label' => 'Subject ID',
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Element::class,
            'allow_extra_fields' => true,
            'csrf_protection' => false,
        ]);
    }
}
