<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
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 RxConnexionBundle\Entity\ReferringHealthFacilityEntity;

class ReferringHealthFacilityForm 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 middle'
                ),
                'required' => true
            ))
            ->add('company_description', TextType::class, array(
                'label' => 'Company Description',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('branch_description', TextType::class, array(
                'label' => 'Branch Description',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('street', TextType::class, array(
                'label' => 'Street',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('mobile_no', TextType::class, array(
                'label' => 'Mobile No(s).',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('mobile_no_two', TextType::class, array(
                'label' => 'Mobile No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('phone_no', TextType::class, array(
                'label' => 'Phone No(s).',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('phone_no_two', TextType::class, array(
                'label' => 'Phone No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('fax_no', TextType::class, array(
                'label' => 'Fax No(s).',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr'  => array(
                    'class' => 'fax'
                ),
                'required' => false
            ))
            ->add('fax_no_two', TextType::class, array(
                'label' => 'Fax No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr'  => array(
                    'class' => 'fax'
                ),
                'required' => false
            ))
            ->add('email', TextType::class, array(
                'label' => 'Email(s)',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('is_active', CheckboxType::class, array(
                'label' => 'Active',
                'required' => false
            ))
            ->add('email_two', TextType::class, array(
                'label' => 'Email',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('geo_latitude', TextType::class, array(
                'label' => 'Latitude',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('geo_longitude', TextType::class, array(
                'label' => 'Longitude',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('city', HiddenType::class)
            ->add('postalCode', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

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

                $form
                    ->add('city_desc', TextType::class, array(
                        'label' => 'City',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $city ? $city->getDescription() : ''
                    ))
                    ->add('postalCode_desc', TextType::class, array(
                        'label' => 'Postal Code',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $postalCode ? $postalCode->getDescription() : ''
                    ));
            });
        $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',
            'csrf_protection' => true,
            'csrf_field_name' => '_token',
            // a unique key to help generate the secret token
            'intention'       => 'referring_health_facilityEntity_intention',
            'action'          => 'n'
        ));
    }
}