<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\AbstractType;
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;

class ReferringHealthFacilityForReferralForm extends AbstractType
{
    private $manager;

    public function __construct(ObjectManager $manager) {
        $this->manager = $manager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('description', TextType::class, array(
                'label' => 'Description',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true,
                'attr' => array(
                    'class' => 'remove-referringHealthFacility-id'
                )
            ))
            ->add('phone_no', TextType::class, array(
                'label' => 'Phone No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'class' => 'remove-referringHealthFacility-id'
                )
            ))
            ->add('fax_no', TextType::class, array(
                'label' => 'Fax No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'class' => 'remove-referringHealthFacility-id fax'
                )
            ))
            ->add('street', TextType::class, array(
                'label' => 'Street',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'class' => 'remove-referringHealthFacility-id'
                )
            ))

            ->add('city', HiddenType::class)
            ->add('postalCode', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();;
                $city = $data ? $data->getCity() : null;
                $postalCode = $data ? $data->getPostalCode() : null;

                $form
                    ->add('city_desc', TextType::class, array(
                        'label' => 'City',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $city ? $city->getDescription() : '',
                        'attr' => array(
                            'class' => 'remove-referringHealthFacility-id'
                        )
                    ))->add('postalCode_desc', TextType::class, array(
                        'label' => 'Postal Code',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $postalCode ? $postalCode->getDescription() : '',
                        'attr' => array(
                            'class' => 'remove-referringHealthFacility-id'
                        )
                    ));
            });

        $builder->get('city')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:CityEntity', false));
        $builder->get('postalCode')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PostalCodeEntity', false));
    }

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

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' 	  => 'RxConnexionBundle\Entity\ReferringHealthFacilityEntity'
        ));
    }
}