<?php

namespace Lc\SovBundle\Form\Setting;

use Lc\SovBundle\Model\Setting\SiteSettingInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SettingType extends BaseSettingType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $settingDefinition = $this->settingDefinition;

        $builder->addEventListener(
                FormEvents::PRE_SET_DATA,
                function (FormEvent $event) use ($settingDefinition) {
                    $form = $event->getForm();
                    $settingEntity = $event->getData();

                    if ($settingEntity) {
                        $form->add('name', HiddenType::class);
                        $settingName = $settingEntity->getName();
                        $label = 'setting_definition.global.settings.' . $settingName;
                        $this->buildFormSetting($label, $form, $settingDefinition, $settingEntity);
                    }
                }
        );
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
                [
                        'label' => false,
                        'data_class' => $this->em->getEntityName(SiteSettingInterface::class),
                ]
        );
    }
}

