<?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' => ''
                ),
                'attr'  => array(
                    'class' => ''
                ),
                'required' => false,
            ))
            ->add('faxTo', ChoiceType::class, array(
                'label' => 'Fax To',
                'label_attr' => array(
                    'class' => ''
                ),
                'mapped'    => true,
                'required' => false,
                'choices' => array(
                    FaxingOptionEntity::FAXING_OPTION_FAX_TO_INSURANCE => 'Insurance',
                    FaxingOptionEntity::FAXING_OPTION_FAX_TO_REFERRING_PROVIDER => 'Referring Physician',
                    FaxingOptionEntity::FAXING_OPTION_FAX_TO_REFERRING_HEALTH_FACILITY => 'Referring Affiliate',
                )
            ))
            ->add('customBody', TextareaType::class, array(
                'label' => 'Custom Body',
                'required' => false
            ))
            ->add('patient', HiddenType::class)
            ->add('insurance', HiddenType::class)
            ->add('referringProvider', 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());
                $insurance = $data->getInsurance();
                $referringPhysician = $data->getReferringProvider();
                $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('insurance_desc', TextType::class, array(
                        'label' => 'Insurance',
                        'label_attr' => array(
                            'class' => 'insurance'
                        ),
                        'attr'  => array(
                            'class' => 'insurance'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $insurance ? $insurance->getDescription() : ''
                    ))
                    ->add('referringProvider_fullName', TextType::class, array(
                        'label' => 'Referring Physician',
                        'label_attr' => array(
                            'class' => 'referring-provider hide'
                        ),
                        'attr'  => array(
                            'class' => 'referring-provider hide'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $referringPhysician ? $referringPhysician->getFullName() : ''
                    ))
                    ->add('referringHealthFacility_desc', TextType::class, array(
                        'label' => 'Referring Affiliate',
                        'label_attr' => array(
                            'class' => 'referring-hf hide'
                        ),
                        'attr'  => array(
                            'class' => 'referring-hf hide'
                        ),
                        '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('insurance')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:InsuranceEntity', false));
        $builder->get('referringProvider')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ReferringProviderEntity', false));
        $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'
        ));
    }
}