<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use RxConnexionBundle\Form\Type\PatientBillingInventoryForm;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

use RxConnexionBundle\Entity\PatientBillingEntity;
use RxConnexionBundle\Form\Type\PatientBillingBreakdownForm;

class PatientBillingForm 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'
                ),
                'data' => $options['action']
            ))
            ->add('patient', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['patient_id']
                )
            ))
            ->add('id', HiddenType::class)
            ->add('amount_due', HiddenType::class)
            ->add('patientInsurance', HiddenType::class)
            ->add('invoice_no', TextType::class, array(
                'label' => 'Invoice No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'disabled',
                    'disabled' => 'true',
                ),
                'required' => false,
            ))
            ->add('serial_no', TextType::class, array(
                'label' => 'Serial No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
            ))
            ->add('prev_sys_invoice_no', TextType::class, array(
                'label' => 'Invoice No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => 'disabled',
                    'disabled' => 'true',
                ),
                'required' => false,
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'required' => false,
                'attr' => array(
                    'rows' => 3,
                ),
                'data' => null
            ))
            ->add('comment', TextareaType::class, array(
                'label' => 'Comment',
                'required' => false,
                'attr' => array(
                    'rows' => 3
                )
            ))
            ->add('patientBillingBreakdowns', CollectionType::class, array(
                'entry_type' => PatientBillingBreakdownForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))
            /*->add('patient_billing_inventories', CollectionType::class, array(
                'entry_type' => PatientBillingInventoryForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))*/
            ->add('payer_type', ChoiceType::class, array(
                'label' => 'Payer Type',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'choices' => array(
                    PatientBillingEntity::PAYER_TYPE_SELF_PAY => 'Self Pay',
                    PatientBillingEntity::PAYER_TYPE_INSURANCE => 'Insurance',
                    PatientBillingEntity::PAYER_TYPE_FINANCING => 'Financing',
                ),
                'choice_attr' => function($val, $key, $index) {
                    return [
                        'class' => 'payer_type_choice_'.strtolower($key),
                    ];
                },
            ))
            ->add('healthFacility', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['defaultHealthFacility_id']
                )
            ))
            ->add('endorsed_to', TextType::class, array(
                'label' => 'Endorsed to',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
            ))
            ->add('healthFacilityForAddress', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use($options) {

                $form = $event->getForm();
                $data = $event->getData();
                $patientInsurance = $data ? $data->getPatientInsurance() : null;
                $patient = $data ? $data->getPatient() : null;
                $healthFacility = $data ? $data->getHealthFacility() : null;
                $healthFacilityForAddress = $data ? $data->getHealthFacilityForAddress() : null;

                $form
                    ->add('billing_date', TextType::class, array(
                        'label' => 'Billing Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date',
                            //'readonly' => $options['action'] == 'u' ? true : false
                        ),
                        'data' => $data && $data->getBillingDateAsString() != '' ? $data->getBillingDateAsString() : date('m/d/Y')
                    ))
                    ->add('due_date', TextType::class, array(
                        'label' => 'Due Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date',
                            //'readonly' => $options['action'] == 'u' ? true : false
                        ),
                        'data' => $data && $data->getDueDateAsString() != '' ? $data->getDueDateAsString() : date('m/d/Y')
                    ))
                    ->add('patientInsurance_insurance_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Insurance',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'attr' => array(
                            //'readonly' => $options['action'] == 'u' ? true : false
                        ),
                        'data' => $patientInsurance ? $patientInsurance->getInsurance()->getDescription() : ''
                    ))
                    ->add('patient_fullName', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Patient',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'attr' => array(
                            'class' => ($patient && $patient->getFullName() == 'Mismatched Invoice')  ? '' : 'hide'
                        ),
                        'data' => $patient ? $patient->getFullName() : ''
                    ))
                    ->add('healthFacility_desc', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Billing Branch',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        /*'attr' => array(
                            'readonly' => $options['action'] == 'u' ? true : false
                        ),*/
                        'data' => $healthFacility ? $healthFacility->getDescription() : $options['defaultHealthFacility']
                    ))
                    ->add('healthFacilityForAddress_desc', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Billing Address',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        /*'attr' => array(
                            'readonly' => $options['action'] == 'u' ? true : false
                        ),*/
                        'data' => $healthFacilityForAddress ? $healthFacilityForAddress->getDescription() : ''
                    ))
                    ->add('demand_letter_date', TextType::class, array(
                        'label' => 'Demand Letter Date',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'required' => false,
                        'data' => $data && $data->getDemandLetterDateAsString() != '' ? $data->getDemandLetterDateAsString() : ''
                    ))
                    ->add('insurance_claim_date', TextType::class, array(
                        'label' => 'Insurance Claim Date',
                        'label_attr' => array(
                            'class' => ''
                        ),
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'required' => false,
                        'data' => $data && $data->getInsuranceClaimDateAsString() != '' ? $data->getInsuranceClaimDateAsString() : ''
                    ));
            });

        $builder->get('patient')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientEntity', true));
        $builder->get('patientInsurance')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientInsuranceEntity', false));
        $builder->get('healthFacility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('healthFacilityForAddress')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
    }

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

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