<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\ChartStatusEntity;
use RxConnexionBundle\Entity\ChartTypeEntity;
use RxConnexionBundle\Entity\PatientChartEntity;
use RxConnexionBundle\Entity\PatientNoteEntity;
use RxConnexionBundle\Form\Type\PatientChartTypeMethodForm;
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\CheckboxType;
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;
use Symfony\Component\Validator\Constraints\Choice;

class ReferringProviderChartForm 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('referringProvider', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['referring_provider_id']
                )
            ))
            ->add('id', HiddenType::class)
            ->add('chart_type', EntityType::class, array(
                'class' => ChartTypeEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('ct')
                        ->where('ct.isActive = 1')
                        ->andWhere('ct.isDeleted = 0')
                        ->orderBy('ct.description', 'ASC');
                },
                'label' => 'Encounter Type',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true
            ))
            ->add('chart_status', EntityType::class, array(
                'class' => ChartStatusEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('cs')
                        ->where('cs.isActive = 1')
                        ->andWhere('cs.isDeleted = 0')
                        ->orderBy('cs.description', 'ASC');
                },
                'choice_attr' => function($chartStatus){
                    return array('class' => $chartStatus->getChartType()->getId());
                },
                'label' => 'Encounter Status',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => false
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'rows' => 5
                ),
                'data' => null
            ))
            ->add('is_endorsed', CheckboxType::class, array(
                'label' => 'Endorsed',
                'required' => false
            ))
            ->add('account_user_endorsed_by', HiddenType::class, array(
                'mapped' => false,
            ))
            ->add('endorsed_by', TextType::class, array(
                'label' => 'Endorsed To',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('ticket_status_id', HiddenType::class, array(
                'mapped' => false,
            ))
            ->add('ticket_status', TextType::class, array(
                'label' => 'Ticket Status',
                'label_attr' => array(
                    'class' => 'middle',
                    'id' => 'label_status',
                    'style' => 'display:none'
                ),
                'required' => false,
                'mapped' => false
            ))
            ->add('ticket_severity_id', HiddenType::class, array(
                'mapped' => false,
            ))
            ->add('ticket_severity', TextType::class, array(
                'label' => 'Ticket Severity',
                'label_attr' => array(
                    'class' => 'middle',
                    'id' => 'label_severity',
                    'style' => 'display:none'
                ),
                'required' => false,
                'mapped' => false
            ))
//            ->add('note_type', 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',
//                ),
//                'mapped' => false
//            ))
            ->add('health_facility', HiddenType::class)
            ->add('marketingStaff', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $healthFacility = $data->getHealthFacility();
                $marketingStaff = $data->getMarketingStaff();

                $form
                    ->add('health_facility_desc', TextType::class, array(
                        'label' => 'Branch',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $healthFacility ? $healthFacility->getDescription() : ''
                    ))
                    ->add('marketingStaff_desc', TextType::class, array(
                        'label' => 'Marketing Staff',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $marketingStaff ? $marketingStaff->getFullName() : ''
                    ))
                    ->add('chart_date', TextType::class, array(
                        'label' => 'Encounter Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getChartDateAsString() : ''
                    ));
            });

        $builder->get('health_facility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('marketingStaff')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:MarketingStaffEntity', false));
        $builder->get('referringProvider')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ReferringProviderEntity', true));
    }

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

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