<?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\Model\MetaTagSetManager;
use VisualM\TagManagerBundle\Util\TaggableManagerRegistry;

/**
 * MetaTagSet FormType
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class MetaTagSetType extends AbstractType
{
    protected $taggableManagerRegistry;
    protected $metaTagSetManager;

    /**
     * Constructor
     * 
     * @param TaggableManagerRegistry $taggableManagerRegistry
     * @param MetaTagSetManager $metaTagSetManager
     */
    public function __construct(TaggableManagerRegistry $taggableManagerRegistry, MetaTagSetManager $metaTagSetManager)
    {
        $this->taggableManagerRegistry = $taggableManagerRegistry;
        $this->metaTagSetManager = $metaTagSetManager;
    }

    /**
     * {@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'
        ));

        $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
        ));

        $factory = $this->metaTagSetManager->getFactory($meta_tag_set->getFactoryService());
        $builder->add('factoryConfiguration', $factory->getFormType(), array(
            'data' => $meta_tag_set->getFactoryConfiguration(),
            'label' => false,
            'attr' => array(
                'tab_title' => $factory->getTitle(),
                'view' => $factory->getFormView(),
            ),
        ));
        
        $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');
                }
            }
        ));
    }
}
