<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\AccountEntity;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
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\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;
use RxConnexionBundle\Entity\HealthFacilityEntity;

class HealthFacilityForm 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('description', TextType::class, array(
                'label' => 'Description',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true
            ))
            /*->add('branch_description', TextType::class, array(
                'label' => 'Branch Description',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))*/
            ->add('description', TextType::class, array(
                'label' => 'Description',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true
            ))
            ->add('street', TextType::class, array(
                'label' => 'Street',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('mobile_no', TextType::class, array(
                'label' => 'Fax No(s).',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('mobile_no_two', TextType::class, array(
                'label' => 'Mobile No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('mobile_no_three', TextType::class, array(
                'label' => 'Mobile No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('phone_no', TextType::class, array(
                'label' => 'Phone No(s).',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('phone_no_two', TextType::class, array(
                'label' => 'Phone No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('phone_no_three', TextType::class, array(
                'label' => 'Phone No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('email', TextType::class, array(
                'label' => 'Email(s)',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('email_two', TextType::class, array(
                'label' => 'Email',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('email_three', TextType::class, array(
                'label' => 'Email',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('geo_latitude', TextType::class, array(
                'label' => 'Latitude',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('geo_longitude', TextType::class, array(
                'label' => 'Longitude',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('website', TextType::class, array(
                'label' => 'Website',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('facebook', TextType::class, array(
                'label' => 'Facebook',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('twitter', TextType::class, array(
                'label' => 'Twitter',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('services', TextareaType::class, array(
                'label' => 'Services',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'rows' => 5
                )
            ))
            ->add('schedule_type', ChoiceType::class, array(
                'label' => 'Schedule Type',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true,
                'choices' => array(
                    HealthFacilityEntity::SCHEDULE_TYPE_TIMED => 'Timed',
                    HealthFacilityEntity::SCHEDULE_TYPE_QUEUED => 'Queued'
                )
            ))
            ->add('opening_time_hour', ChoiceType::class, array(
                'required' => true,
                'choices' => $this->generateHoursArray()
            ))
            ->add('opening_time_minute', ChoiceType::class, array(
                'required' => true,
                'choices' => $this->generateMinutesArray()
            ))
            ->add('opening_time_period', ChoiceType::class, array(
                'required' => true,
                'choices' => array(
                    HealthFacilityEntity::OPENING_TIME_PERIOD_AM => 'AM',
                    HealthFacilityEntity::OPENING_TIME_PERIOD_PM => 'PM'
                )
            ))
            ->add('closing_time_hour', ChoiceType::class, array(
                'required' => true,
                'choices' => $this->generateHoursArray()
            ))
            ->add('closing_time_minute', ChoiceType::class, array(
                'required' => true,
                'choices' => $this->generateMinutesArray()
            ))
            ->add('closing_time_period', ChoiceType::class, array(
                'required' => true,
                'choices' => array(
                    HealthFacilityEntity::CLOSING_TIME_PERIOD_AM => 'AM',
                    HealthFacilityEntity::CLOSING_TIME_PERIOD_PM => 'PM'
                )
            ))
            ->add('queueing_start_time_hour', ChoiceType::class, array(
                'label' => 'Queueing Start Time Hour',
                'required' => true,
                'choices' => $this->generateHoursArray()
            ))
            ->add('queueing_start_time_minute', ChoiceType::class, array(
                'label' => 'Queueing Start Time Minute',
                'required' => true,
                'choices' => $this->generateMinutesArray()
            ))
            ->add('queueing_start_time_period', ChoiceType::class, array(
                'label' => 'Queueing Start Time Period',
                'required' => true,
                'choices' => array(
                    'AM' => 'AM',
                    'PM' => 'PM'
                )
            ))
            ->add('queueing_end_time_hour', ChoiceType::class, array(
                'label' => 'Queueing End Time Hour',
                'required' => true,
                'choices' => $this->generateHoursArray()
            ))
            ->add('queueing_end_time_minute', ChoiceType::class, array(
                'label' => 'Queueing End Time Minute',
                'required' => true,
                'choices' => $this->generateMinutesArray()
            ))
            ->add('queueing_end_time_period', ChoiceType::class, array(
                'label' => 'Queueing End Time Period',
                'required' => true,
                'choices' => array(
                    'AM' => 'AM',
                    'PM' => 'PM'
                )
            ))
            ->add('no_of_clinician', TextType::class, array(
                'label' => 'No. of Clinicians',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
            ))
            ->add('account', EntityType::class, array(
                'class' => AccountEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('cs')
                        ->orderBy('cs.description', 'ASC');
                },
                'label' => 'Account',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true
            ))
            ->add('isMarketing', CheckboxType::class, array(
                'label' => 'Marketing',
                'required' => false
            ))
            ->add('is_active', CheckboxType::class, array(
                'label' => 'Active',
                'required' => false
            ))
            ->add('city', HiddenType::class)
            ->add('healthFacility_type', HiddenType::class)
            ->add('postalCode', HiddenType::class)
            ->add('company', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $city = $data->getCity();
                $healthFacilityType = $data->getHealthFacilityType();
                $account = $data->getAccount();
                $company = $data->getCompany();
                $postalCode = $data->getPostalCode();

                $form
                    ->add('company_desc', TextType::class, array(
                        'label' => 'Company',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $company ? $company->getDescription() : ''
                    ))
                    ->add('city_desc', TextType::class, array(
                        'label' => 'City',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $city ? $city->getDescription() : ''
                    ))
                    /*->add('account_desc', TextType::class, array(
                        'label' => 'Account',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $account ? $account->getDescription() : ''
                    ))*/
                    /*->add('healthFacility_type_desc', TextType::class, array(
                        'label' => 'Branch Type',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $healthFacilityType ? $healthFacilityType->getDescription() : ''
                    ))*/
                    ->add('postalCode_desc', TextType::class, array(
                        'label' => 'Postal Code',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $postalCode ? $postalCode->getDescription() : ''
                    ));
            });
        $builder->get('city')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:CityEntity', false));
        //$builder->get('account')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountEntity', false));
        //$builder->get('healthFacility_type')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityTypeEntity', false));
        $builder->get('postalCode')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PostalCodeEntity', false));
        $builder->get('company')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:CompanyEntity', false));
    }

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

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

    private function generateHoursArray() {

        $hoursArray = array();

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

        return $hoursArray;
    }

    private function generateMinutesArray() {

        $minutesArray = array();

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

        return $minutesArray;
    }
}