<?php

namespace RxConnexionBundle\Form\Type;

use Doctrine\Common\Persistence\ObjectManager;
use RxConnexionBundle\Entity\HealthFacilityEntity;
use RxConnexionBundle\Entity\ItemEntity;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ItemTaxCodeForm 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('percent', TextType::class, array(
                'label' => 'Percent',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'amount'
                ),
            ))
            ->add('item', HiddenType::class)
            ->add('health_facility', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) use ($builder) {
                $form = $event->getForm();
                $data = $event->getData();

                $form
                    ->add('health_facility_description', TextType::class, array(
                        'label' => 'Branch',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'attr' => array(
                            'class' => ''
                        ),
                        'mapped' => false,
                        'required' => true,
                        'data' => $data && $data->getHealthFacility() ? $data->getHealthFacility()->getDescription() : ''
                    ));
            });
        $builder->get('item')->addModelTransformer(new DataTransformer($this->manager, ItemEntity::class));
        $builder->get('health_facility')->addModelTransformer(new DataTransformer($this->manager, HealthFacilityEntity::class, false));

    }

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

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