<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\MarketingStaffWorkScheduleEntity;;
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 MarketingStaffWorkScheduleForm 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('marketingStaff', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['marketingStaff_id']
                )
            ))
            ->add('type', ChoiceType::class, array(
                'label' => 'Type',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => false,
                'choices' => array(
                    MarketingStaffWorkScheduleEntity::TYPE_REGULAR => 'Regular',
                    MarketingStaffWorkScheduleEntity::TYPE_IRREGULAR => 'Irregular',
                    MarketingStaffWorkScheduleEntity::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(
                    MarketingStaffWorkScheduleEntity::DAYNAME_MONDAY => 'Monday',
                    MarketingStaffWorkScheduleEntity::DAYNAME_TUESDAY => 'Tuesday',
                    MarketingStaffWorkScheduleEntity::DAYNAME_WEDNESDAY => 'Wednesday',
                    MarketingStaffWorkScheduleEntity::DAYNAME_THURSDAY => 'Thursday',
                    MarketingStaffWorkScheduleEntity::DAYNAME_FRIDAY => 'Friday',
                    MarketingStaffWorkScheduleEntity::DAYNAME_SATURDAY => 'Saturday',
                    MarketingStaffWorkScheduleEntity::DAYNAME_SUNDAY => 'Sunday',
                ),
                '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(
                    MarketingStaffWorkScheduleEntity::FROMAM_AM => 'AM',
                    MarketingStaffWorkScheduleEntity::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(
                    MarketingStaffWorkScheduleEntity::TOAM_AM => 'AM',
                    MarketingStaffWorkScheduleEntity::TOPM_PM => 'PM'
                ),
                'attr' => array(
                    'class' => 'remove-val'
                )
            ))
            ->add('id', HiddenType::class)
            ->add('created_by', HiddenType::class)
            ->add('created_at', HiddenType::class)
            ->add('updated_by', HiddenType::class)
            ->add('updated_at', 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('marketingStaff')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:MarketingStaffEntity', true));
    }

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

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