<?php

namespace RxConnexionBundle\Form\Type;

use RxConnexionBundle\Entity\InventorySerialNoEntity;
use RxConnexionBundle\Entity\PatientBillingBreakdownEntity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
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 Doctrine\Common\Persistence\ObjectManager;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;

class PatientBillingBreakdownForm extends AbstractType
{
    private $manager;

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', HiddenType::class)
            ->add('serialNumber', TextType::class, array(
                'required' => false
            ))
            ->add('inventory_serial_no', TextType::class, array(
                'required' => false
            ))
            ->add('patientChart', HiddenType::class, array(
                'required' => false
            ))
            ->add('chartProcedure', HiddenType::class, array(
                'required' => false
            ))
            ->add('description', TextType::class, array(
                'required' => false
            ))
            ->add('item', TextType::class, array(
                'required' => false
            ))
            ->add('term', ChoiceType::class, array(
                'choices' => array(
                    PatientBillingBreakdownEntity::TERM_RENT_TO_OWN => 'Rent to Own',
                    PatientBillingBreakdownEntity::TERM_LONG_TERM_RENTAL => 'Long Term Rental',
                    PatientBillingBreakdownEntity::TERM_WARRANTY => 'Warranty',
                ),
            ))
            ->add('item_type', ChoiceType::class, array(
                'choices' => array(
                    PatientBillingBreakdownEntity::ITEM_TYPE_ACCESSORY => 'Accessory',
                    PatientBillingBreakdownEntity::ITEM_TYPE_CPAP_PACKAGE => 'CPAP Package',
                    PatientBillingBreakdownEntity::ITEM_TYPE_MEDICAL_SUPPLIES => 'Medical Supplies',
                    PatientBillingBreakdownEntity::ITEM_TYPE_PART_OF_CPAP_PACKAGE => 'Part of CPAP Package',
                    PatientBillingBreakdownEntity::ITEM_TYPE_CPAP_TRIAL => PatientBillingBreakdownEntity::ITEM_TYPE_CPAP_TRIAL,
                ),
            ))
            /*->add('tranDateForOther', TextType::class, array(
                'required' => false
            ))*/
            ->add('quantity', TextType::class)
            ->add('price', TextType::class)
            ->add('amount', TextType::class)
            ->add('amount_covered', TextType::class, array(
                'required' => false,
            ))
            ->add('price_covered', TextType::class, array(
                'required' => false,
            ))
            ->add('frequency', NumberType::class, array(
                'required' => false
            ))
            ->add('benefitPeriod', ChoiceType::class, array(
                'choices' => array(
                    PatientBillingBreakdownEntity::BENEFIT_PERIOD_CALENDAR_YEAR => 'Calendar Year',
                    PatientBillingBreakdownEntity::BENEFIT_PERIOD_BENEFIT_YEAR => 'Benefit Year',
                ),
            ))
            ->add('type', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();

                $patientChart = $data ? $data->getPatientChart() : null;
                $chartProcedure = $data ? $data->getChartProcedure() : null;
                $item = $data ? $data->getItem() : null;
                $itemSubcategory = null;
                if($item && $item->getItemSubCategory()){
                    $itemSubcategory = $item->getItemSubCategory();
                }

                $form
                    ->add('patientChart_chartNo', TextType::class, array(
                        'mapped' => false,
                        'data' => $patientChart ? $patientChart->getId() : ''
                    ))
                    ->add('chartProcedure_description', TextType::class, array(
                        'mapped' => false,
                        'data' => $chartProcedure ? $chartProcedure->getDescription() : ''
                    ))
                    ->add('item_description', TextType::class, array(
                        'mapped' => false,
                        'data' => $item ? $item->getDescription() : ''
                    ))
                    ->add('itemCategory', HiddenType::class, array(
                        'data' => $itemSubcategory ? $itemSubcategory->getId() : '',
                        'mapped' => false
                    ))
                    ->add('itemCategory_desc', TextType::class, array(
                        'mapped' => false,
                        'data' => $itemSubcategory ? $itemSubcategory->getDescription() : ''
                    ));
            });

        $builder->get('patientChart')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientChartEntity', false));
        $builder->get('chartProcedure')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ChartProcedureEntity', false));
        $builder->get('item')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ItemEntity', false));
        $builder->get('inventory_serial_no')->addModelTransformer(new DataTransformer($this->manager, InventorySerialNoEntity::class, false));
    }

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

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