<?php

namespace RxConnexionBundle\Form;

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 ItemAveCostForm 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('ave_cost', TextType::class, array(
                'label' => 'Average Cost',
                '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_ave_cost';
    }

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