<?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 PatientLetterForm 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('id', HiddenType::class)
            ->add('patientLetterStatus', EntityType::class, array(
                'class' => PatientLetterStatusEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {
                        return $er->createQueryBuilder('ct')
                            ->where('ct.isActive = 1')
                            ->andWhere('ct.isDeleted = 0')
                            ->andWhere("ct.description != 'New'");
                },
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true
            ))
            ->add('letter_type', ChoiceType::class, array(
                'label' => 'Letter Type',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => false,
                'choices' => PatientLetterEntity::LETTERS
            ))
            ->add('affiliate_type', ChoiceType::class, array(
                'label' => 'Affiliates',
                'label_attr' => array(
                    'class' => ''
                ),
                'mapped'    => false,
                'required' => true,
                'choices' => array(
                    PatientLetterEntity::AFFILIATE_TYPE_INSURANCE => 'Insurance',
                    PatientLetterEntity::AFFILIATE_TYPE_REFERRING_PROVIDER => 'Referring Provider',
                )
            ))
            ->add('custom_body', TextareaType::class, array(
                'label' => 'Custom Body',
                'required' => false
            ))
            ->add('attention', TextType::class, array(
                'label' => 'Attention',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('recipient', TextType::class, array(
                'label' => 'Recipient',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('phone_no', TextType::class, array(
                'label' => 'Phone No.',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('fax_no', TextType::class, array(
                'label' => 'Fax No.',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('name', TextType::class, array(
                'label' => 'Name',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('page', TextType::class, array(
                'label' => 'Page(s)',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('carbon_copy', TextType::class, array(
                'label' => 'CC',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('patientLetterCcs', CollectionType::class, array(
                'entry_type' => PatientLetterCcForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))
            ->add('patient_referring_provider', HiddenType::class)
            ->add('patient_referring_health_facility', HiddenType::class)
            ->add('patient_insurance', HiddenType::class)
            ->add('accountUser', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();
                $patientReferringProvider = $data->getPatientReferringProvider();
                $patientReferringHealthFacility = $data->getPatientReferringHealthFacility();
                $accountUser = $data->getAccountUser();
                $patientInsurance = $data->getPatientInsurance();
                $faxCoverSelection = explode(',', $data->getFaxCoverSelection());
                $faxBranchSelection = explode(',', $data->getFaxBranchSelection());

                $form
                    ->add('patient_referring_provider_desc', TextType::class, array(
                        'label' => 'Referring Provider',
                        'label_attr' => array(
                            'class' => 'required'
                        ),
                        'required' => true,
                        'attr'  => array(
                            'class' => 'hidden-affiliate'
                        ),
                        'mapped' => false,
                        'data' => $patientReferringProvider ? $patientReferringProvider->getReferringProvider()->getFullName() : ''
                    ))
                    ->add('date', TextType::class, array(
                        'label' => 'Date',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date',
                        ),
                        'data' => $data ? $data->getDateAsString() : ''
                    ))
                    ->add('accountUser_fullName', TextType::class, array(
                        'label' => 'Assign To',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $accountUser ? $accountUser->getFullName() : ''
                    ))
                    ->add('patient_referring_health_facility_desc', TextType::class, array(
                        'label' => 'Referring Health Facility',
                        'label_attr' => array(
                            'class' => 'required'
                        ),
                        'attr'  => array(
                            'class' => 'hidden-affiliate'
                        ),
                        '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'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $patientInsurance ? $patientInsurance->getInsurance()->getDescription() : ''
                    ))
                    ->add('faxCoverSelection', ChoiceType::class, array(
                        'required' => false,
                        'choices' => array(
                            'Urgent' => PatientLetterEntity::FAX_COVER_SELECTION_1,
                            'For Review' => PatientLetterEntity::FAX_COVER_SELECTION_2,
                            'Please Comment' => PatientLetterEntity::FAX_COVER_SELECTION_3,
                            'Please Reply' => PatientLetterEntity::FAX_COVER_SELECTION_4,
                        ),
                        'expanded' => true,
                        'multiple' => true,
                        'mapped' => false,
                        'data' => $faxCoverSelection
                    ))
                    ->add('fax_cover_custom_body', TextareaType::class, array(
                        'label' => 'Fax Cover Body',
                        'required' => false
                    ))
                    ->add('faxBranchSelection', ChoiceType::class, array(
                        'required' => false,
                        'choices' => array(
                            PatientLetterEntity::FAX_BRANCH_SELECTION_1 => 1,
                            PatientLetterEntity::FAX_BRANCH_SELECTION_2 => 2,
                            //PatientLetterEntity::FAX_BRANCH_SELECTION_3 => 3,
//                            PatientLetterEntity::FAX_BRANCH_SELECTION_4 => 4,
//                            PatientLetterEntity::FAX_BRANCH_SELECTION_5 => 5,
                        ),
                        'expanded' => true,
                        'multiple' => true,
                        'mapped' => false,
                        'data' => $faxBranchSelection
                    ));
            });
        $builder->get('patient')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientEntity', 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));
        $builder->get('accountUser')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserEntity', false));
    }

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

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