<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use RxConnexionBundle\Entity\AppointmentNoticeFaxEntity;
use RxConnexionBundle\Entity\PatientLetterEntity;
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\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 AppointmentNoticeFaxForm 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('patient', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['patient_id']
                )
            ))
            ->add('schedule', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['schedule_id']
                )
            ))
            ->add('id', HiddenType::class)
            ->add('patient_referring_provider', HiddenType::class)
            ->add('patient_referring_health_facility', HiddenType::class)
            ->add('patient_insurance', HiddenType::class)
            ->add('affiliate_type', ChoiceType::class, array(
                'label' => 'Affiliates',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false,
                'choices' => AppointmentNoticeFaxEntity::AFFILIATE_TYPE
            ))
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();
                $patientReferringProvider = $data->getPatientReferringProvider();
                $patientReferringHealthFacility = $data->getPatientReferringHealthFacility();
                $patientInsurance = $data->getPatientInsurance();
                $form
                    ->add('patient_referring_provider_desc', TextType::class, array(
                        'label' => 'Referring Physician',
                        'label_attr' => array(
                            'class' => 'required'
                        ),
                        'required' => false,
                        'attr'  => array(
                            'class' => 'hidden-affiliate'
                        ),
                        'mapped' => false,
                        'data' => $patientReferringProvider ? $patientReferringProvider->getReferringProvider()->getFullName() : ''
                    ))
                    ->add('patient_referring_health_facility_desc', TextType::class, array(
                        'label' => 'Referring Affiliate',
                        'label_attr' => array(
                            'class' => 'required'
                        ),
                        'attr'  => array(
                            'class' => 'hidden-affiliate'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $patientReferringHealthFacility ? $patientReferringHealthFacility->getReferringHealthFacility()->getDescription() : ''
                    ))
                    ->add('patient_insurance_desc', TextType::class, array(
                        'label' => 'Insurance',
                        'label_attr' => array(
                            'class' => 'required'
                        ),
                        'attr'  => array(
                            'class' => 'hidden-affiliate'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $patientInsurance ? $patientInsurance->getInsurance()->getDescription() : ''
                    ));
            });
        $builder->get('patient')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientEntity', true));
        $builder->get('schedule')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ScheduleEntity', true));
        $builder->get('patient_referring_provider')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientReferringProviderEntity', false));
        $builder->get('patient_referring_health_facility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientReferringHealthFacilityEntity', false));
        $builder->get('patient_insurance')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientInsuranceEntity', false));
    }

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

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