<?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\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
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', 'text', array('label' => 'meta_tag_set.naam', 'trim' => true));

        $builder->add('kiesbaar', 'choice', array(
            'choices' => array(
                'meta_tag_set.kiesbaar.false' => false,
                'meta_tag_set.kiesbaar.true' => true,
            ),
            'choices_as_values' => true,
            'required' => false,
            'empty_value' => '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', 'choice', array(
            'choices' => $content_types_choices,
            'multiple' => true,
            'expanded' => true,
            'required' => false,
            'label' => 'meta_tag_set.content_types.label'
        ));

        $builder->add('metaTagPresets', 'collection', array(
            'type' => 'meta_tag_preset',
            'allow_add' => true,
            'allow_delete' => true,
            'by_reference' => true,
            'label' => false
        ));
        
        $builder->add('save', 'submit', array('label' => 'gebruiker.save'));
        $builder->add('saveAndClose', 'submit', array('label' => 'gebruiker.save_and_close'));


    }

    /**
     * {@inheritDoc}
     */
    public function getName()
    {
        return 'meta_tag_set';
    }

    /**
     * {@inheritDoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $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');
                }
            }
        ));

    }

}
