<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\NoteTemplateEntity;
use RxConnexionBundle\Entity\PatientNoteEntity;
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\NumberType;
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\FormEvents;
use Symfony\Component\Form\FormEvent;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;


class NoteTemplateForm 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('id', HiddenType::class)
            ->add('description', TextType::class, array(
                'label' => 'Description',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true,
            ))
            ->add('noteType', ChoiceType::class, array(
                'label' => 'Note Type',
                'required' => false,
                'choices' => array(
                    PatientNoteEntity::TYPE_CLINICAL => 'Clinical',
                    PatientNoteEntity::TYPE_BILLING => 'Billing',
                    PatientNoteEntity::TYPE_APPOINTMENT => 'Appointment',
                    PatientNoteEntity::TYPE_LETTER => 'Letter',
                    PatientNoteEntity::TYPE_TEMPLATE => 'Template',
                ),
                'mapped' => true
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Note',
                'required' => false
            ))
            ->add('company', HiddenType::class)
            ->add('healthFacility', HiddenType::class)
            ->add('chartType', HiddenType::class)
            ->add('chartStatus', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $chartType = $data->getChartType();
                $chartStatus = $data->getChartStatus();
                $company = $data->getCompany();
                $healthFacility = $data->getHealthFacility();

                $form
                    ->add('chartType_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Encounter Type',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'data' => $chartType ? $chartType->getDescription() : ''
                    ))
                    ->add('chartStatus_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Encounter Status',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'data' => $chartStatus ? $chartStatus->getDescription() : ''
                    ))
                    ->add('company_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Company',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'data' => $company ? $company->getDescription() : ''
                    ))
                    ->add('healthFacility_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Branch',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'data' => $healthFacility ? $healthFacility->getDescription() : ''
                    ));
            });

        $builder->get('chartType')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ChartTypeEntity', false));
        $builder->get('chartStatus')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ChartStatusEntity', false));
        $builder->get('healthFacility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('company')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:CompanyEntity', false));
    }

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

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