<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\ProviderWorkScheduleEntity;
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 ProviderWorkScheduleForm 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('provider', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['provider_id']
                )
            ))
            ->add('type', ChoiceType::class, array(
                'label' => 'Type',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => false,
                'choices' => array(
                    ProviderWorkScheduleEntity::TYPE_REGULAR => 'Regular',
                    ProviderWorkScheduleEntity::TYPE_IRREGULAR => 'Irregular',
                    ProviderWorkScheduleEntity::TYPE_OUT => 'Out',
                )
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'rows' => 5,
                    'class' => 'remove-val'
                )
            ))
            ->add('day_name', ChoiceType::class, array(
                'label' => 'Day',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'choices' => array(
                    ProviderWorkScheduleEntity::DAYNAME_MONDAY => 'Monday',
                    ProviderWorkScheduleEntity::DAYNAME_TUESDAY => 'Tuesday',
                    ProviderWorkScheduleEntity::DAYNAME_WEDNESDAY => 'Wednesday',
                    ProviderWorkScheduleEntity::DAYNAME_THURSDAY => 'Thursday',
                    ProviderWorkScheduleEntity::DAYNAME_FRIDAY => 'Friday',
                    ProviderWorkScheduleEntity::DAYNAME_SATURDAY => 'Saturday',
                ),
                'attr' => array(
                    'class' => 'remove-val'
                )
            ))
            ->add('from_hr', ChoiceType::class, array(
                'required' => false,
                'choices' => $this->generateHoursArray(),
                'attr' => array(
                    'class' => 'remove-val'
                )
            ))
            ->add('from_min', ChoiceType::class, array(
                'required' => false,
                'choices' => $this->generateMinutesArray(),
                'attr' => array(
                    'class' => 'remove-val'
                )
            ))
            ->add('from_am', ChoiceType::class, array(
                'required' => false,
                'choices' => array(
                    ProviderWorkScheduleEntity::FROMAM_AM => 'AM',
                    ProviderWorkScheduleEntity::FROMAM_PM => 'PM'
                ),
                'attr' => array(
                    'class' => 'remove-val'
                )
            ))
            ->add('to_hr', ChoiceType::class, array(
                'required' => false,
                'choices' => $this->generateHoursArray(),
                'attr' => array(
                    'class' => 'remove-val'
                )
            ))
            ->add('to_min', ChoiceType::class, array(
                'required' => false,
                'choices' => $this->generateMinutesArray(),
                'attr' => array(
                    'class' => 'remove-val'
                )
            ))
            ->add('to_am', ChoiceType::class, array(
                'required' => false,
                'choices' => array(
                    ProviderWorkScheduleEntity::TOAM_AM => 'AM',
                    ProviderWorkScheduleEntity::TOPM_PM => 'PM'
                ),
                'attr' => array(
                    'class' => 'remove-val'
                )
            ))
            ->add('id', HiddenType::class)
            ->add('health_facility', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $healthFacility = $data->getHealthFacility();

                $form
                    ->add('health_facility_desc', TextType::class, array(
                        'label' => 'Branch',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $healthFacility ? $healthFacility->getDescription() : '',
                        'attr' => array(
                            'class' => 'remove-val'
                        ),
                    ))
                    ->add('from_date', TextType::class, array(
                        'label' => 'From',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date remove-val'
                        ),
                        'data' => $data ? $data->getFromDateAsString() : ''
                    ))
                    ->add('to_date', TextType::class, array(
                        'label' => 'To',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date remove-val'
                        ),
                        'data' => $data ? $data->getToDateAsString() : ''
                    ));
            });

        $builder->get('health_facility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('provider')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ProviderEntity', true));
    }

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

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' 	  => 'RxConnexionBundle\Entity\ProviderWorkScheduleEntity',
            'csrf_protection' => true,
            'csrf_field_name' => '_token',
            // a unique key to help generate the secret token
            'intention'       => 'providerWorkScheduleEntity_intention',
            'action'          => 'n',
            'provider_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;
    }
}