<?php
/**
 * MetaTagSet FormType
 *
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\TagManagerBundle\Form\Type;

use Exception;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use VisualM\TagManagerBundle\Model\MetaTagSetModel;
use VisualM\TagManagerBundle\Util\TaggableManagerRegistry;

/**
 * MetaTagSet FormType
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class MetaTagSetType extends AbstractType
{

    /**
     * Taggable Manager Registry
     * @var TaggableManagerRegistry
     */
    protected $taggableManagerRegistry;

    /**
     * Constructor
     * 
     * @param TaggableManagerRegistry $taggableManagerRegistry Taggable Manager Registry
     */
    public function __construct(TaggableManagerRegistry $taggableManagerRegistry)
    {
        $this->taggableManagerRegistry = $taggableManagerRegistry;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $meta_tag_set = $options['data'];
        if (!($meta_tag_set instanceof MetaTagSetModel)) {
            throw new Exception("Data should be instance of MetaTagSetModel");
        }

        $builder->add('naam', TextType::class, array('label' => 'meta_tag_set.naam', 'trim' => true));

        $builder->add('kiesbaar', ChoiceType::class, array(
            'choices' => array(
                false => 'meta_tag_set.kiesbaar.false',
                true => 'meta_tag_set.kiesbaar.true'
            ),
            'placeholder' => 'meta_tag_set.kiesbaar.empty',
            'label' => 'meta_tag_set.kiesbaar.label'
        ));

        // Load available content types from the taggableManagerRegistry
        $content_types_choices = [];
        foreach($this->taggableManagerRegistry->getRegistratedNames() as $name) {
            $content_types_choices[$name] = 'meta_tag_set.content_type.' . $name;
        }

        $builder->add('contentTypes', ChoiceType::class, array(
            'choices' => $content_types_choices,
            'multiple' => true,
            'expanded' => true,
            'required' => false,
            'label' => 'meta_tag_set.content_types.label'
        ));

        $builder->add('metaTagPresets', CollectionType::class, array(
            'entry_type' => MetaTagPresetType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'by_reference' => true,
            'label' => false
        ));
        
        $builder->add('save', SubmitType::class, array('label' => 'gebruiker.save'));
        $builder->add('saveAndClose', SubmitType::class, array('label' => 'gebruiker.save_and_close'));


    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'meta_tag_set';
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VisualM\TagManagerBundle\Entity\MetaTagSet',
            'translation_domain' => 'forms',
            'validation_groups' => function(FormInterface $form) {
                $data = $form->getData();
                if ($data->getId() === null) {
                    return array('meta_tag_set', 'Default', 'add', 'meta_tag_set_add');
                } else {
                    return array('meta_tag_set', 'Default', 'edit', 'meta_tag_set_edit');
                }
            }
        ));

    }

}
