<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Entity\InventoryEntity;

class InventoryForm extends AbstractType
{
    private $manager;

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $inventoryStatuses = array();

        if(count($options['inventoryStatuses']))
            foreach($options['inventoryStatuses'] as $Status)
                $inventoryStatuses[$Status] = $Status;

        $builder
            ->add('action', HiddenType::class, array(
                'mapped' => false,
                'attr' => array(
                    'class' => 'form-action'
                )
            ))
            ->add('id', HiddenType::class)
            ->add('created_by', HiddenType::class)
            ->add('created_at', HiddenType::class)
            ->add('updated_by', HiddenType::class)
            ->add('updated_at', HiddenType::class)
            ->add('quantity', TextType::class, array(
                'label' => 'Quantity',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'amount'
                ),
                'required' => false
            ))
            ->add('status', ChoiceType::class, array(
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true,
                'choices' => $inventoryStatuses
            ))
            ->add('item', HiddenType::class)
            ->add('healthFacility', HiddenType::class)
            ->add('location', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $item = $data->getItem();
                $healthFacility = $data->getHealthFacility();
                $location = $data->getLocation();

                $form
                    ->add('item_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Item',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'data' => $item ? $item->getDescription() : ''
                    ))
                    ->add('branch_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Branch',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'data' => $healthFacility ? $healthFacility->getDescription() : ''
                    ))
                    ->add('location_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Location',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        //'required' => false,
                        'data' => $location ? $location->getDescription() : ''
                    ));
            });

        $builder->get('item')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ItemEntity', false));
        $builder->get('healthFacility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('location')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:LocationEntity', false));

    }

    public function getName()
    {
        return 'inventory';
    }

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