<?php

namespace App\Form\Domain;

use App\Entity\Domain;
use App\Entity\Domain\Stat;
use App\Entity\Stat as StatEntity;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class StatType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('domain', EntityType::class, [
                'class' => Domain::class,
                'choice_value' => 'id',
                'choice_label' => function ($domain) {
                    return $domain->getUrl();
                },
            ])
            ->add('stat', EntityType::class, [
                'class' => StatEntity::class,
                'choice_value' => 'id',
                'choice_label' => function ($stat) {
                    return $stat->getTitle();
                },
            ])
            ->add('value', IntegerType::class, [
                'label' => 'Value',
                'required' => false,
            ]);
    }

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