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

namespace VisualM\TagManagerBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use VisualM\TagManagerBundle\Doctrine\FieldType\MetaType;
use VisualM\TagManagerBundle\Doctrine\FieldType\WijzigbaarType;

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

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $wijzigbaar_choices = [
            WijzigbaarType::NIET => 'meta_tag_preset.wijzigbaar.niet',
            WijzigbaarType::BEPERKT => 'meta_tag_preset.wijzigbaar.beperkt',
            WijzigbaarType::VOLLEDIG => 'meta_tag_preset.wijzigbaar.volledig'
        ];

        $type_choices = [
            MetaType::META_TAG => 'meta_tag_base.meta_type.meta_tag',
            MetaType::HTML_HEADER_SNIPPET => 'meta_tag_base.meta_type.html_header_snippet',
            MetaType::HTML_BODY_SNIPPET => 'meta_tag_base.meta_type.html_body_snippet'
        ];

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

        $builder->add('type', ChoiceType::class, array(
            'choices' => $type_choices,
            'placeholder' => 'meta_tag_preset.type.empty',
            'label' => 'meta_tag_preset.type.label'
        ));

        $builder->add('zichtbaar', TextareaType::class, array('label' => 'meta_tag_preset.zichtbaar', 'attr' => [ 'rows' => 6, 'cols' => 100 ], 'required' => false));
        $builder->add('zichtbaarIsExpression', CheckboxType::class, array(
            'label'     => 'meta_tag_preset.zichtbaar_is_expression',
            'required'  => false
        ));
        $builder->add('zichtbaarWijzigbaar', ChoiceType::class, array(
            'choices' => $wijzigbaar_choices,
            'placeholder' => 'meta_tag_preset.zichtbaar_wijzigbaar.empty',
            'label' => 'meta_tag_preset.zichtbaar_wijzigbaar.label'
        ));
        
        $builder->add('inhoud', TextareaType::class, array('label' => 'meta_tag_preset.inhoud', 'attr' => [ 'rows' => 6, 'cols' => 100 ], 'required' => false));
        $builder->add('inhoudIsExpression', CheckboxType::class, array(
            'label'     => 'meta_tag_preset.inhoud_is_expression',
            'required'  => false
        ));
        $builder->add('inhoudWijzigbaar', ChoiceType::class, array(
            'choices' => $wijzigbaar_choices,
            'placeholder' => 'meta_tag_preset.inhoud_wijzigbaar.empty',
            'label' => 'meta_tag_preset.inhoud_wijzigbaar.label'
        ));

        // Add event listiner to set selectbox for zichtbaar
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
            $form = $event->getForm();
            $data = $event->getData();
            $zichtbaar_is_expression = $data === null ? false : $data->getZichtbaarIsExpression();
            $default = $zichtbaar_is_expression ? true : ($data === null ? true : ($data->getZichtbaar() == "0" ? false : true));
            $form->add('zichtbaarSelect', ChoiceType::class, array(
                'label' => 'meta_tag_preset.zichtbaar',
                'choices' => [
                    'meta_tag_preset.zichtbaar_select.yes' =>  true,
                    'meta_tag_preset.zichtbaar_select.no' => false
                ],
                'choices_as_values' => true,
                'choice_value' => function ($value) {
                    return $value ? 'true' : 'false';
                },
                'mapped' => false,
                'data' => $default
            ));
        });

        // Add event listener to load selectbox for zichtbaar
        $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
            $form = $event->getForm();
            $data = $event->getData();
            if ($data->getZichtbaarIsExpression() == false) {
                $data->setZichtbaar($form->get('zichtbaarSelect')->getData() ? "1" : "0");
            }           
        });

    }

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

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VisualM\TagManagerBundle\Entity\MetaTagPreset',
            'translation_domain' => 'forms'
        ));

    }

}
