<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\PatientEmailCcEntity;
use RxConnexionBundle\Entity\PatientLetterEntity;
use RxConnexionBundle\Entity\PatientLetterStatusEntity;
use RxConnexionBundle\Form\Type\PatientEmailCcForm;
use RxConnexionBundle\Form\Type\PatientLetterCcForm;
use RxConnexionBundle\Form\Type\PatientTicketForm;
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\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class PatientEmailForm 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('patient', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['patient_id'],
                )
            ))
            ->add('id', HiddenType::class)
            ->add('customBody', TextareaType::class, array(
                'label' => 'Body',
                'required' => false
            ))
            ->add('email', TextType::class, array(
                'label' => 'Email',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('emailFrom', TextType::class, array(
                'label' => 'From',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => true
            ))
            ->add('patientEmailCcs', CollectionType::class, array(
                'entry_type' => PatientEmailCcForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))
            ->add('accountUser', HiddenType::class)
            ->add('emailTemplate', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();

                $emailTemplate = $data->getEmailTemplate();
                $accountUser = $data->getAccountUser();

                $form
                    ->add('date', TextType::class, array(
                        'label' => 'Date',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date',
                        ),
                        'data' => $data ? $data->getDateAsString() : ''
                    ))
                    ->add('email_template_description', TextType::class, array(
                        'label' => 'Email Template',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $emailTemplate ? $emailTemplate->getDescription() : ''
                    ))
                    ->add('accountUser_fullName', TextType::class, array(
                        'label' => 'Assign To',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $accountUser ? $accountUser->getFullName() : ''
                    ));
            });
        $builder->get('patient')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientEntity', true));
        $builder->get('accountUser')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserEntity', false));
        $builder->get('emailTemplate')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:EmailTemplateEntity', false));
    }

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

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