<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use RxConnexionBundle\Entity\FaxTranEntity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
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 FaxTranForm 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('remote_id', TextType::class, array(
                'label' => 'From Fax #',
                'attr'  => array(
                    'readonly'  => true,
                )
            ))
            ->add('receive_status', HiddenType::class)
            ->add('size', HiddenType::class)
            ->add('viewed_status', TextType::class, array(
                'label' => 'Viewed Status',
                'attr'  => array(
                    'readonly'  => true,
                )
            ))
            ->add('fax_id', HiddenType::class)
            ->add('fax_category', ChoiceType::class, array(
                'label' => 'Fax Category',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true,
                'choices' => FaxTranEntity::CATEGORY
            ))
            ->add('fax_status', ChoiceType::class, array(
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true,
                'choices' => FaxTranEntity::STATUS
            ))
            ->add('is_duplicate', CheckboxType::class, array(
                'label' => 'Duplicate',
                'required' => false
            ))
            ->add('is_existing', CheckboxType::class, array(
                'label' => 'Existing',
                'required' => false
            ))
            ->add('is_unable_to_process', CheckboxType::class, array(
                'label' => 'Unable to Process',
                'required' => false
            ))
            ->add('health_facility', HiddenType::class)
            ->add('account_user', HiddenType::class)
            ->add('patient', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();
                $healthFacility = $data->getHealthFacility();
                $accountUser = $data->getAccountUser();
                $patient = $data->getPatient();
                $form
                    ->add('fax_date', TextType::class, array(
                        'label' => 'Fax Date',
                        'attr' => array(
                            'readonly'  => true,
                        ),
                        'data' => $data ? $data->getFaxDateAsString() : ''
                    ))
                    ->add('pages', HiddenType::class, array(
                        'data' => $data ? $data->getPages() : ''
                    ))
                    ->add('file_name', HiddenType::class, array(
                        'data' => $data ? $data->getFileName() : ''
                    ))
                    ->add('health_facility_desc', TextType::class, array(
                        'label' => 'Branch',
                        'required' => false,
                        'mapped' => false,
                        'data' => $healthFacility ? $healthFacility->getDescription() : ''
                    ))
                    ->add('account_user_name', TextType::class, array(
                        'label' => 'User',
                        'required' => false,
                        'mapped' => false,
                        'data' => $accountUser ? $accountUser->getFullName() : ''
                    ))
                    ->add('patient_name', TextType::class, array(
                        'label' => 'Patient',
                        'required' => false,
                        'mapped' => false,
                        'data' => $patient ? $patient->getFullName() : ''
                    ));
            });
        $builder->get('health_facility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('account_user')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserEntity', false));
        $builder->get('patient')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientEntity', false));
    }

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

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