<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\ChartStatusEntity;
use RxConnexionBundle\Entity\ChartTypeEntity;
use RxConnexionBundle\Entity\PatientChartEntity;
use RxConnexionBundle\Entity\PatientNoteEntity;
use RxConnexionBundle\Form\Type\PatientChartCcForm;
use RxConnexionBundle\Form\Type\PatientChartTypeMethodForm;
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\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Validator\Constraints\Choice;

class PatientChartForm 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('chart_type', EntityType::class, array(
                'class' => ChartTypeEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('ct')
                        ->where('ct.isActive = 1')
                        ->andWhere('ct.isDeleted = 0')
                        ->orderBy('ct.description', 'ASC');
                },
                'label' => 'Encounter Type',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true
            ))
            ->add('chart_status', EntityType::class, array(
                'class' => ChartStatusEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('cs')
                        ->where('cs.isActive = 1')
                        ->andWhere('cs.isDeleted = 0')
                        ->orderBy('cs.description', 'ASC');
                },
                'choice_attr' => function($chartStatus){
                    return array('class' => $chartStatus->getChartType()->getId());
                },
                'label' => 'Encounter Status',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => false
            ))
            ->add('patientChartCcs', CollectionType::class, array(
                'entry_type' => PatientChartCcForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'label_attr' => array(
                    'class' => 'middle required'
                ),
                'required' => true,
                'attr' => array(
                    'rows' => 5
                ),
                'data' => null
            ))
            ->add('height', TextType::class, array(
                'label' => 'Height (inch)',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                  'class' => 'compute-bmi amount',
                    'placeholder' => '0'
                ),
                'required' => false
            ))
            ->add('weight', TextType::class, array(
                'label' => 'Weight (Lbs)',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'compute-bmi amount',
                    'placeholder' => '0'
                ),
                'required' => false
            ))
            ->add('bmi', TextType::class, array(
                'label' => 'BMI',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'amount',
                    'readonly' => 'readonly',
                    'placeholder' => '0.00'
                ),
                'required' => false
            ))
            ->add('blood_pressure_first', TextType::class, array(
                'label' => 'Blood Pressure',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'vital-statistics amount',
                    'placeholder' => '0'
                ),
                'required' => false
            ))
            ->add('blood_pressure_second', TextType::class, array(
                'attr' => array(
                    'class' => 'vital-statistics amount',
                    'placeholder' => '0'
                ),
                'required' => false
            ))
            ->add('temperature', TextType::class, array(
                'label' => 'Temperature',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'vital-statistics amount',
                    'placeholder' => '0.00'
                ),
                'required' => false
            ))
            ->add('pulse', TextType::class, array(
                'label' => 'Pulse',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'vital-statistics amount',
                    'placeholder' => '0'
                ),
                'required' => false
            ))
            ->add('respiratory_rate', TextType::class, array(
                'label' => 'Respiratory Rate',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'vital-statistics amount',
                    'placeholder' => '0'
                ),
                'required' => false
            ))
            ->add('neck_size', TextType::class, array(
                'label' => 'Neck Size (Inches)',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'vital-statistics amount',
                    'placeholder' => '0'
                ),
                'required' => false
            ))
            ->add('otwo_saturation', TextType::class, array(
                'label' => 'O2 Saturation',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'vital-statistics amount',
                    'placeholder' => '0'
                ),
                'required' => false
            ))
            ->add('smoker', TextType::class, array(
                'label' => 'Smoker',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('drinker', TextType::class, array(
                'label' => 'Drinker',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('stop_bang', TextType::class, array(
                'label' => 'Stop Bang',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('nose', TextType::class, array(
                'label' => 'Nose',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('pre_ess', TextType::class, array(
                'label' => 'Pre-ESS',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('post_ess', TextType::class, array(
                'label' => 'Post-ESS',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('chartTypeMethods', CollectionType::class, array(
                'entry_type' => PatientChartTypeMethodForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))
            ->add('is_endorsed', CheckboxType::class, array(
                'label' => 'Endorsed',
                'required' => false
            ))
            ->add('account_user_endorsed_by', HiddenType::class, array(
                'mapped' => false,
            ))
            ->add('endorsed_by', TextType::class, array(
                'label' => 'Endorsed To',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('ticket_status_id', HiddenType::class, array(
                'mapped' => false,
            ))
            ->add('ticket_status', TextType::class, array(
                'label' => 'Ticket Status',
                'label_attr' => array(
                    'class' => 'middle',
                    'id' => 'label_status',
                    'style' => 'display:none'
                ),
                'required' => false,
                'mapped' => false
            ))
            ->add('ticket_severity_id', HiddenType::class, array(
                'mapped' => false,
            ))
            ->add('ticket_severity', TextType::class, array(
                'label' => 'Ticket Severity',
                'label_attr' => array(
                    'class' => 'middle',
                    'id' => 'label_severity',
                    'style' => 'display:none'
                ),
                'required' => false,
                'mapped' => false
            ))
            ->add('billing_status', TextType::class, array(
                'label' => 'Billing Status',
                'label_attr' => array(
                    'class' => 'middle required'
                ),
                'required' => false,
                'attr' => array(
                    'readonly' => 'readonly'
                )
            ))
            ->add('note_type', ChoiceType::class, array(
                'label' => 'Note Type',
                'required' => false,
                'choices' => array(
                    PatientNoteEntity::TYPE_CLINICAL => 'Clinical',
                    PatientNoteEntity::TYPE_BILLING => 'Billing',
                    PatientNoteEntity::TYPE_APPOINTMENT => 'Appointment',
                    PatientNoteEntity::TYPE_LETTER => 'Letter',
                    PatientNoteEntity::TYPE_TEMPLATE => 'Template',
                ),
                'mapped' => false
            ))
            ->add('isRepeat', CheckboxType::class, array(
                'label' => 'Repeat',
                'required' => false
            ))
            ->add('health_facility', HiddenType::class)
            ->add('provider', HiddenType::class)
            ->add('account_staff', HiddenType::class)
            ->add('account_user_billing', HiddenType::class)
            ->add('noteTemplate', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $healthFacility = $data->getHealthFacility();
                $provider = $data->getProvider();
                $accountStaff = $data->getAccountStaff();
                $accountUserBilling = $data->getAccountUserBilling();
                $noteTemplate = $data->getNoteTemplate();

                $form
                    ->add('health_facility_desc', TextType::class, array(
                        'label' => 'Branch',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $healthFacility ? $healthFacility->getDescription() : ''
                    ))
                    ->add('provider_desc', TextType::class, array(
                        'label' => 'Clinician',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $provider ? $provider->getFullName() : ''
                    ))
                    ->add('chart_date', TextType::class, array(
                        'label' => 'Encounter Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getChartDateAsString() : ''
                    ))
                    ->add('account_staff_full_name', TextType::class, array(
                        'label' => 'Staff',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $accountStaff ? $accountStaff->getFullName() : ''
                    ))
                    ->add('billing_full_name', TextType::class, array(
                        'label' => 'Billing',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $accountUserBilling ? $accountUserBilling->getFullName() : ''
                    ))
                    ->add('note_template_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Note Template',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'hide'
                        ),
                        'data' => $noteTemplate ? $noteTemplate->getDescription() : ''
                    ));
            });

        $builder->get('health_facility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('provider')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ProviderEntity', false));
        $builder->get('patient')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientEntity', true));
        $builder->get('account_staff')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountStaffEntity', false));
        $builder->get('account_user_billing')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserBillingEntity', false));
        $builder->get('noteTemplate')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:NoteTemplateEntity', false));
    }

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

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