<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\ChartTypeEntity;
use RxConnexionBundle\Entity\MarketingStaffScheduleEntity;
use RxConnexionBundle\Entity\MarketingStaffScheduleStatusEntity;
use RxConnexionBundle\Entity\ReferringHealthFacilityNoteEntity;
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\TextareaType;
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;

class ReferringHealthFacilityScheduleForm 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('referringHealthFacility', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['referring_health_facility_id']
                )
            ))
            ->add('id', HiddenType::class)
            ->add('start_time_hour', ChoiceType::class, array(
                'required' => true,
                'choices' => $this->generateHoursArray()
            ))
            ->add('start_time_minute', ChoiceType::class, array(
                'required' => true,
                'choices' => $this->generateMinutesArray()
            ))
            ->add('start_time_period', ChoiceType::class, array(
                'required' => true,
                'choices' => array(
                    MarketingStaffScheduleEntity::START_TIME_PERIOD_AM => 'AM',
                    MarketingStaffScheduleEntity::START_TIME_PERIOD_PM => 'PM'
                )
            ))
            ->add('end_time_hour', ChoiceType::class, array(
                'required' => true,
                'choices' => $this->generateHoursArray()
            ))
            ->add('end_time_minute', ChoiceType::class, array(
                'required' => true,
                'choices' => $this->generateMinutesArray()
            ))
            ->add('end_time_period', ChoiceType::class, array(
                'required' => true,
                'choices' => array(
                    MarketingStaffScheduleEntity::END_TIME_PERIOD_AM => 'AM',
                    MarketingStaffScheduleEntity::END_TIME_PERIOD_PM => 'PM'
                )
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true,
                'attr' => array(
                    'rows' => 5
                ),
                'data' => null
            ))
            ->add('marketingStaffScheduleStatus', EntityType::class, array(
                'class' => MarketingStaffScheduleStatusEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('ss')
                        ->where('ss.isActive = 1')
                        ->orderBy('ss.description', 'ASC');
                },
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true
            ))
            ->add('note_type', ChoiceType::class, array(
                'label' => 'Note Type',
                'required' => false,
                'choices' => array(
                    ReferringHealthFacilityNoteEntity::TYPE_CLINICAL => 'Clinical',
                    ReferringHealthFacilityNoteEntity::TYPE_BILLING => 'Billing',
                    ReferringHealthFacilityNoteEntity::TYPE_APPOINTMENT => 'Appointment',
                ),
                'mapped' => false
            ))
            /*            ->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')
                        ->orderBy('ct.description', 'ASC');
                },
                'label' => 'Encounter Type',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true
            ))*/
            ->add('health_facility', HiddenType::class)
            ->add('marketingStaff', HiddenType::class)
            ->add('chartType', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $healthFacility = $data->getHealthFacility();
                $marketingStaff = $data->getMarketingStaff();
//                $faxCoverSelection = explode(',', $data->getFaxCoverSelection());
//                $faxBodySelection = explode(',', $data->getFaxBodySelection());
                $chartType = $data->getChartType();

                $form
                    ->add('chartType_desc', TextType::class, array(
                        'label' => 'Type',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $chartType ? $chartType->getDescription() : ''
                    ))
                    ->add('health_facility_desc', TextType::class, array(
                        'label' => 'Branch',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $healthFacility ? $healthFacility->getDescription() : ''
                    ))
                    ->add('marketingStaff_fullName', TextType::class, array(
                        'label' => 'Marketing Staff',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $marketingStaff ? $marketingStaff->getFullName() : ''
                    ))
                    ->add('schedule_date', TextType::class, array(
                        'label' => 'Schedule Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getScheduleDateAsString() : ''
                    ));
//                    ->add('faxCoverSelection', ChoiceType::class, array(
//                        'required' => false,
//                        'choices' => array(
//                            'Urgent' => ScheduleEntity::FAX_COVER_SELECTION_1,
//                            'For Review' => ScheduleEntity::FAX_COVER_SELECTION_2,
//                            'Please Comment' => ScheduleEntity::FAX_COVER_SELECTION_3,
//                            'Please Reply' => ScheduleEntity::FAX_COVER_SELECTION_4,
//                        ),
//                        'expanded' => true,
//                        'multiple' => true,
//                        'mapped' => false,
//                        'data' => $faxCoverSelection
//                    ));
//                    ->add('faxBodySelection', ChoiceType::class, array(
//                        'required' => false,
//                        'choices' => array(
//                            'Referring HealthFacility is aware of the appointment' => ScheduleEntity::FAX_BODY_SELECTION_1,
//                            'Message left on the voicemail' => ScheduleEntity::FAX_BODY_SELECTION_2,
//                            'Referring HealthFacility is not aware of the appointment. Please inform the patient. We are also still trying to contact them' => ScheduleEntity::FAX_BODY_SELECTION_3,
//                        ),
//                        'expanded' => true,
//                        'multiple' => true,
//                        'mapped' => false,
//                        'data' => $faxBodySelection
//                    ));
            });

        $builder->get('health_facility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('referringHealthFacility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ReferringHealthFacilityEntity', true));
        $builder->get('marketingStaff')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:MarketingStaffEntity', false));
        $builder->get('chartType')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ChartTypeEntity', false));
    }

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

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

    private function generateHoursArray() {

        $hoursArray = array();

        for($i=1; $i<=12; $i++) {
            $hoursArray[sprintf('%002d', $i)] = $i;
        }
        return $hoursArray;

    }

    private function generateMinutesArray() {

        $minutesArray = array();

        for($i=0; $i<=55; $i+=5) {
            $minutesArray[sprintf('%002d', $i)] = $i;
        }

        return $minutesArray;
    }
}