<?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\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
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\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use VisualM\FormBundle\Entity\FormField;
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', HiddenType::class);

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


        $field_types = [
            FormField::TEXT => 'form_field.type.text',
            FormField::NUMBER => 'form_field.type.number',
            FormField::PHONE => 'form_field.type.phone',
            FormField::EMAIL => 'form_field.type.email',
            FormField::DATE => 'form_field.type.date',
            FormField::TEXTAREA => 'form_field.type.textarea',
            FormField::SELECT => 'form_field.type.select',
            FormField::SELECTLIST => 'form_field.type.selectlist',
            FormField::CHECKBOXLIST => 'form_field.type.checkboxlist',
            FormField::OPTIONLIST => 'form_field.type.optionlist',
            FormField::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', ChoiceType::class, array(
            'label' => 'form_field.type.label',
            'choices' => $field_types,
            'placeholder' => false,
            'expanded' => false,
            'required' => true
        ));

        $builder->add('verplicht', ChoiceType::class, array(
            'label' => 'form_field.verplicht',
            'choices' => array(
                'boolean.no' => false,
                'boolean.yes' => true
            ),
            'choices_as_values' => true,
            'placeholder' => false,
            'expanded' => false,
            'required' => true
        ));

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

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

    }


    /**
     * {@inhertDoc@}
     */
    public function getBlockPrefix()
    {
        return 'visualm_form_field';
    }
    
    /**
     * {@inhertDoc@}
     */
    public function configureOptions(OptionsResolver $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');
                }
            }
        ));
    }

}