<?php
/**
 * FormField Type for Form
 * 
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\FormBundle\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\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use VisualM\FormBundle\Doctrine\FieldType\FormFieldType as FieldType;
use VisualM\FormBundle\Model\FormManager;

/**
 * FormField Type for Form
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class FormFieldType extends AbstractType
{
    /**
     * Allow Subform include
     * @var bool
     */
    protected $allowSubformInclude;

    /**
     * Form Manager
     * @var FormManager
     */
    protected $formManager;

    /**
     * Constructor
     *
     * @param bool        $allowSubformInclude Allow Subform include
     * @param FormManager $formManager         Form Manager
     */
    public function __construct($allowSubformInclude, FormManager $formManager)
    {
        $this->allowSubformInclude = $allowSubformInclude;
        $this->formManager = $formManager;
    }

    /**
     * {@inhertDoc@}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('volgorde', 'hidden');

        $builder->add('titel', 'text', array('label' => 'form_field.titel', 'trim' => true));
        $builder->add('referentie', 'text', array('label' => 'form_field.referentie', 'trim' => true));


        $field_types = [
            FieldType::TEXT => 'form_field.type.text',
            FieldType::NUMBER => 'form_field.type.number',
            FieldType::PHONE => 'form_field.type.phone',
            FieldType::EMAIL => 'form_field.type.email',
            FieldType::DATE => 'form_field.type.date',
            FieldType::TEXTAREA => 'form_field.type.textarea',
            FieldType::SELECT => 'form_field.type.select',
            FieldType::SELECTLIST => 'form_field.type.selectlist',
            FieldType::CHECKBOXLIST => 'form_field.type.checkboxlist',
            FieldType::OPTIONLIST => 'form_field.type.optionlist',
            FieldType::HIDDEN => 'form_field.type.hidden'
        ];

        if ($this->allowSubformInclude) {
            $formOptions = $this->formManager->getIndex([ 'n_id' => $options['form_id'] ]);
            foreach($formOptions as $formOption) {
                $field_types[sprintf('form#%d[]', $formOption->getId())] = sprintf('Subform "%s" (%s)', $formOption->getTitel(), $formOption->getReferentie());
            }
        }

        $builder->add('type', 'choice', array(
            'label' => 'form_field.type.label',
            'choices' => $field_types,
            'empty_value' => false,
            'expanded' => false,
            'required' => true
        ));

        $builder->add('verplicht', 'choice', array(
            'label' => 'form_field.verplicht',
            'choices' => array(false => 'boolean.no', true => 'boolean.yes'),
            'empty_value' => false,
            'expanded' => false,
            'required' => true
        ));

        $builder->add('rijen', 'choice', array(
            'label' => 'form_field.rijen',
            'choices' => array(
                '2' => '2',
                '4' => '4',
                '6' => '6',
                '8' => '8'
            ),
            'empty_value' => false,
            'expanded' => false,
            'required' => true
        ));

        $builder->add('regex', 'text', array('label' => 'form_field.regex', 'trim' => true, 'required' => false));
        $builder->add('waarden', 'textarea', array('label' => 'form_field.waarden', 'required' => false, 'attr' => [ 'rows' => 4 ]));

    }


    /**
     * {@inhertDoc@}
     */
    public function getName()
    {
        return 'visualm_form_field';
    }
    
    /**
     * {@inhertDoc@}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VisualM\FormBundle\Entity\FormField',
            'translation_domain' => 'forms',
            'form_id' => null,
            'validation_groups' => function(FormInterface $form) {
                $data = $form->getData();
                if ($data->getId() === null) {
                    return array('form_field', 'Default', 'add', 'form_field_add');
                } else {
                    return array('form_field', 'Default', 'edit', 'form_field_edit');
                }
            }
        ));
    }

}