<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\PatientLetterEntity;
use RxConnexionBundle\Entity\PatientLetterStatusEntity;
use RxConnexionBundle\Form\Type\PatientLetterCcForm;
use RxConnexionBundle\Form\Type\PatientTicketForm;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
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\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class FrequentlyAskedQuestionForm extends AbstractType
{
    private $manager;

    public function __construct(ObjectManager $manager) {
        $this->manager = $manager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('action', HiddenType::class, array(
                'mapped' => false,
                'attr' => array(
                    'class' => 'form-action'
                )
            ))
            ->add('id', HiddenType::class)
            ->add('description', TextType::class, array(
                'label' => 'Description',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'rows' => 5
                ),
                'data' => null
            ))

            ->add('fileType', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();
                $fileType = $data->getFileType();

                $form
                    ->add('fileType_desc', TextType::class, array(
                        'label' => 'File Type',
                        'label_attr' => array(
                            'class' => 'required'
                        ),
                        'required' => true,
                        'attr'  => array(
                            'class' => ''
                        ),
                        'mapped' => false,
                        'data' => $fileType ? $fileType->getDescription() : ''
                    ));
            });
        $builder->get('fileType')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:FileTypeEntity', false));
    }

    public function getBlockPrefix()
    {
        return 'faq';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' 	  => 'RxConnexionBundle\Entity\FrequentlyAskedQuestionEntity',
            'csrf_protection' => true,
            'csrf_field_name' => '_token',
            // a unique key to help generate the secret token
            'intention'       => 'faqEntity_intention',
            'action'          => 'n',
        ));
    }
}