<?php

namespace RxConnexionBundle\Form\Type;

use Doctrine\Common\Persistence\ObjectManager;
use RxConnexionBundle\Entity\PatientLetterEntity;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;

class PatientLetterCcForm extends AbstractType
{
    private $manager;

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', HiddenType::class)
            ->add('patient_referring_provider', HiddenType::class, array(
                'required' => false
            ))
            /*->add('patient_referring_health_facility', HiddenType::class, array(
                'required' => false
            ))*/
            ->add('patient_insurance', HiddenType::class, array(
                'required' => false
            ))
            ->add('affiliate_type', ChoiceType::class, array(
                'label' => 'Affiliates',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => true,
                'choices' => array(
                    PatientLetterEntity::AFFILIATE_TYPE_INSURANCE => 'Insurance',
                    PatientLetterEntity::AFFILIATE_TYPE_REFERRING_PROVIDER => 'Referring Provider',
                )
            ))
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();
                /**@var PatientLetterEntity $patientLetter*/
                $patientLetter = $data ? $data->getPatientLetter() : null;
                $patientReferringProvider = $patientLetter ? $patientLetter->getPatientReferringProvider() : null;
                //$patientReferringHealthFacility = $patientLetter ? $patientLetter->getPatientReferringHealthFacility() : null;
                $patientInsurance = $patientLetter ? $patientLetter->getPatientInsurance() : $patientLetter;
                
                $form
                    ->add('patient_referring_provider_desc', TextType::class, array(
                        'label' => 'Referring Provider',
                        'label_attr' => array(
                            'class' => 'required'
                        ),
                        'required' => true,
                        'attr'  => array(
                            'class' => 'hidden-affiliate-cc'
                        ),
                        'mapped' => false,
                        'data' => $patientReferringProvider ? $patientReferringProvider->getReferringProvider()->getFullName() : ''
                    ))
                    /*->add('patient_referring_health_facility_desc', TextType::class, array(
                        'label' => 'Referring Health Facility',
                        'label_attr' => array(
                            'class' => 'required'
                        ),
                        'attr'  => array(
                            'class' => 'hidden-affiliate-cc'
                        ),
                        'required' => true,
                        '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-cc'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $patientInsurance ? $patientInsurance->getInsurance()->getDescription() : ''
                    ));
            });
        $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 'patientLetterCc';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' 	  => 'RxConnexionBundle\Entity\PatientLetterCcEntity'
        ));
    }
}