<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\AccountUserReminderEntity;
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\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;

class PatientReminderForm 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('account_user', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['account_user_id']
                )
            ))
            ->add('patient', HiddenType::class, array(
                'attr' => array(
                    'value' => base64_decode($options['patient'])
                ),
                'mapped' => false
            ))
            ->add('id', HiddenType::class)
            ->add('status', ChoiceType::class, array(
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'choices' => array(
                    'Pending' => AccountUserReminderEntity::REMINDER_STATUS_1,
                    'Completed' => AccountUserReminderEntity::REMINDER_STATUS_2
                )
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Note',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'rows' => 5
                )
            ))
            ->add('reminderType', HiddenType::class)
            ->add('assignedToAccountUser', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $assignedToAccountUser = $data->getAssignedToAccountUser();
                $reminderType = $data->getReminderType();

                $form
                    ->add('reminderType_desc', TextType::class, array(
                        'label' => 'Reminder Type',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $reminderType ? $reminderType->getDescription() : ''
                    ))
                    ->add('assignedToAccountUser_fullName', TextType::class, array(
                        'label' => 'Assigned To',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $assignedToAccountUser ? $assignedToAccountUser->getFullName() : ''
                    ))
                    ->add('start_date', TextType::class, array(
                        'label' => 'Start Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getStartDateAsString() : ''
                    ))
                    ->add('end_date', TextType::class, array(
                        'label' => 'End Date',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getEndDateAsString() : ''
                    ));
            });
//        $builder->get('patient')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientEntity', true));
        $builder->get('account_user')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserEntity', true));
        $builder->get('assignedToAccountUser')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserEntity', false));
        $builder->get('reminderType')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ReminderTypeEntity', false));
    }

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

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