<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\FaxingOptionEntity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Validator\Constraints\Choice;

class FaxingOptionForm 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('faxNo', TextType::class, array(
                'label' => 'Fax No.',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'attr'  => array(
                    'class' => 'fax'
                ),
                'required' => true,
            ))
            ->add('page', TextType::class, array(
                'label' => 'Page(s)',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'attr'  => array(
                    'class' => ''
                ),
                'required' => true,
            ))
            ->add('template', ChoiceType::class, array(
                'label' => 'Template',
                'label_attr' => array(
                    'class' => ''
                ),
                'mapped'    => true,
                'required' => true,
                'choices' => array(
                    FaxingOptionEntity::FAXING_OPTION_TEMPLATE_DSRC => 'Dynamic Sleep and Respiratory Care',
                    FaxingOptionEntity::FAXING_OPTION_TEMPLATE_FRSC => 'Freedom Respiratory & Sleep Centre',
                    FaxingOptionEntity::FAXING_OPTION_TEMPLATE_PSS => 'Perfect Sleep Solutions WC Inc.',
                )
            ))
            ->add('customBody', TextareaType::class, array(
                'label' => 'Custom Body',
                'required' => false
            ))
            ->add('patient', HiddenType::class)
            ->add('referringHealthFacility', HiddenType::class)
            ->add('company', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $patient = $data->getPatient();
                $faxCoverSelection = explode(',', $data->getFaxCoverSelection());
                $referringAffiliate = $data->getReferringHealthFacility();
                $company = $data->getCompany();

                $form
                    ->add('patientName', TextType::class, array(
                        'label' => 'Patient',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $patient ? $patient->getFullName() : ''
                    ))
                    ->add('referringHealthFacility_desc', TextType::class, array(
                        'label' => 'Referring Affiliate',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $referringAffiliate ? $referringAffiliate->getDescription() : ''
                    ))
                    ->add('company_desc', TextType::class, array(
                        'label' => 'Company',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $company ? $company->getDescription() : ''
                    ))
                    ->add('birth_date', TextType::class, array(
                        'label' => 'Birth Date',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getBirthDateAsString() : ''
                    ))
                    ->add('faxCoverSelection', ChoiceType::class, array(
                        'required' => false,
                        'choices' => array(
                            'Urgent' => FaxingOptionEntity::FAX_COVER_SELECTION_1,
                            'For Review' => FaxingOptionEntity::FAX_COVER_SELECTION_2,
                            'Please Comment' => FaxingOptionEntity::FAX_COVER_SELECTION_3,
                            'Please Reply' => FaxingOptionEntity::FAX_COVER_SELECTION_4,
                        ),
                        'expanded' => true,
                        'multiple' => true,
                        'mapped' => false,
                        'data' => $faxCoverSelection
                    ));
            });

        $builder->get('referringHealthFacility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ReferringHealthFacilityEntity', false));
        $builder->get('patient')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientEntity', false));
        $builder->get('company')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:CompanyEntity', false));
    }

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

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