<?php

namespace RxConnexionBundle\Form;

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

class AccountUserReminderForm extends AbstractType
{
    private $manager;
    private $accountUser;

    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'
                ),
                'data' => $options['action']
            ))
            ->add('id', HiddenType::class)
            ->add('accountUserReminderRefPatients', CollectionType::class, array(
                'entry_type' => AccountUserReminderRefPatientForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Note',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                  'rows' => 3
                ),
            ))
            ->add('reminder_type', EntityType::class, array(
                'class' => ReminderTypeEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('rt')
                        ->where('rt.isDeleted != :isDeleted')
                        ->orderBy('rt.description', 'ASC')
                        ->setParameters(array(
                            'isDeleted' => true,
                        ));
                },
                'label' => 'Category',
                'label_attr' => array(
                    'class' => 'middle required'
                ),
                'required' => true
            ))
            ->add('status', ChoiceType::class, array(
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'choices' => array(
                    AccountUserReminderEntity::REMINDER_STATUS_1 => AccountUserReminderEntity::REMINDER_STATUS_1,
                    AccountUserReminderEntity::REMINDER_STATUS_2 => AccountUserReminderEntity::REMINDER_STATUS_2,
                )
            ))
            ->add('account_user', HiddenType::class)
            ->add('assigned_to_user_group', HiddenType::class)
            ->add('assigned_to_account_user', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
                $form = $event->getForm();
                $data = $event->getData();
                $assignedToAccountUser = $data->getAssignedToAccountUser();
                $assignedToUserGroup = $data->getAssignedToUserGroup();

                $form
                    ->add('assigned_to_account_user_desc', TextType::class, array(
                        'label' => 'Assign To',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $assignedToAccountUser ? $assignedToAccountUser->getFullName() : '',
                        'attr' => array(
                            'placeholder' => 'User'
                        )
                    ))
                    ->add('assigned_to_user_group_desc', TextType::class, array(
                        'label' => 'Assign To User Group',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $assignedToUserGroup ? $assignedToUserGroup->getDescription() : '',
                        'attr' => array(
                            'placeholder' => 'User Group'
                        )
                    ))
                    ->add('start_date', TextType::class, array(
                       'label' => 'Start Date',
                       'label_attr' => array(
                          'class' => 'required middle',
                       ),
                       '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('assigned_to_account_user')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserEntity', false));
        $builder->get('account_user')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserEntity', false));
        $builder->get('assigned_to_user_group')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:UserGroupEntity', false));
    }

    public function getName()
    {
        return 'accountUserReminder';
    }

    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'         => 'accountUserReminderEntity_intention',
            'action'            => 'n',
        ));
    }
}