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

namespace VisualM\TagManagerBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use VisualM\TagManagerBundle\Doctrine\FieldType\WijzigbaarType;
use VisualM\TagManagerBundle\Entity\MetaTag;
use Symfony\Component\Security\Core\SecurityContextInterface;

/**
 * MetaTag FormType
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class MetaTagType extends AbstractType
{
    /**
     * SecurityContext
     * @var SecurityContextInterface
     */
    protected $securityContext;

    /**
     * Constructor
     * 
     * @param SecurityContextInterface $securityContext SecurityContext
     */
    public function __construct(SecurityContextInterface $securityContext)
    {
        $this->securityContext = $securityContext;
    }

    /**
     * {@inheritDoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Becouse we can dynamicly add metaTags we need the preset field as hidden
        // field. It contains the id of the corresponding set
        $builder->add('metaTagPreset', 'entity_hidden', [
            'class' => 'VisualM\TagManagerBundle\Entity\MetaTagPreset'
        ]);

        $securityContext = $this->securityContext;

        // Add Event listener
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($securityContext) {

            $form = $event->getForm();
            $data = $event->getData();
            /* @var $data MetaTag */

            // Check if we should allow editing of the inhoud field
            if (
                ($data === null) OR
                ($data->getMetaTagPreset()->getInhoudWijzigbaar() === WijzigbaarType::VOLLEDIG) OR
                ($securityContext->isGranted('ROLE_PERM_META_TAG_BEPERKT') AND $data->getMetaTagPreset()->getInhoudWijzigbaar() === WijzigbaarType::BEPERKT)
            ) {
                $form->add('inhoud', 'textarea', array('label' => 'meta_tag.inhoud', 'attr' => [ 'rows' => 6, 'cols' => 100 ]));
                $form->add('inhoudIsExpression', 'checkbox', array(
                    'label'     => 'meta_tag.inhoud_is_expression',
                    'required'  => false
                ));
            }

            // Check if we should allow editing of the zichtbaar field
            if (
                ($data === null) OR
                ($data->getMetaTagPreset()->getZichtbaarWijzigbaar() === WijzigbaarType::VOLLEDIG) OR
                ($securityContext->isGranted('ROLE_PERM_META_TAG_BEPERKT') AND $data->getMetaTagPreset()->getZichtbaarWijzigbaar() === WijzigbaarType::BEPERKT)
            ) {
                $form->add('zichtbaar', 'textarea', array('label' => 'meta_tag.zichtbaar', 'attr' => [ 'rows' => 6, 'cols' => 100 ]));
                $form->add('zichtbaarIsExpression', 'checkbox', array(
                    'label'     => 'meta_tag.zichtbaar_is_expression',
                    'required'  => false
                ));

                $zichtbaar_is_expression = $data === null ? false : $data->getZichtbaarIsExpression();
                $default = $zichtbaar_is_expression ? true : ($data === null ? true : ($data->getZichtbaar() == "0" ? false : true));
                $form->add('zichtbaarSelect', 'choice', array(
                    'label' => 'meta_tag.zichtbaar',
                    'choices' => [ true => 'meta_tag.zichtbaar_select.yes', false => 'meta_tag.zichtbaar_select.no' ],
                    'mapped' => false,
                    'data' => $default
                ));
            }
   
        });

        
        // Add event listener to load selectbox for zichtbaar
        $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($securityContext) {
            $form = $event->getForm();
            $data = $event->getData();

            $meta_tag_preset = $data->getMetaTagPreset();


            // Check if Zichtbaar was editable
            if (
                ($meta_tag_preset->getZichtbaarWijzigbaar() === WijzigbaarType::NIET) OR
                (!$securityContext->isGranted('ROLE_PERM_META_TAG_BEPERKT') AND $meta_tag_preset->getZichtbaarWijzigbaar() === WijzigbaarType::BEPERKT)
            ) {
                // If not get data from its preset
				if ($data->getId() === null) {
					$data->setZichtbaar($meta_tag_preset->getZichtbaar());
					$data->setZichtbaarIsExpression($meta_tag_preset->getZichtbaarIsExpression());
				}

            // Else check if data was an expression and handle select box
            } else if ($data->getZichtbaarIsExpression() == false) {
                $data->setZichtbaar($form->get('zichtbaarSelect')->getData() ? "1" : "0");
            }

            // Check if Inhoud was editable
            if (
                ($meta_tag_preset->getInhoudWijzigbaar() === WijzigbaarType::NIET) OR
                (!$securityContext->isGranted('ROLE_PERM_META_TAG_BEPERKT') AND $meta_tag_preset->getInhoudWijzigbaar() === WijzigbaarType::BEPERKT)
            ) {
                // If not get data from its preset
                $data->setInhoud($data->getMetaTagPreset()->getInhoud());
                $data->setInhoudIsExpression($data->getMetaTagPreset()->getInhoudIsExpression());
            }

            $event->setData($data);

        });

    }

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

    /**
     * {@inheritDoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VisualM\TagManagerBundle\Entity\MetaTag',
            'translation_domain' => 'forms'
        ));

    }

}
