<?php

namespace RxConnexionBundle\Form;

use Doctrine\ORM\EntityRepository;
use Doctrine\Common\Persistence\ObjectManager;
use RxConnexionBundle\Entity\AccountUserTypeEntity;
use RxConnexionBundle\Entity\SalesAgentEntity;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;

class AccountUserForm 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('account_user_type', EntityType::class, array(
                'class' => AccountUserTypeEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('aut')
                        ->orderBy('aut.description', 'ASC');
                },
                'label' => 'User Type',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true
            ))
            ->add('username', TextType::class, array(
                'label' => 'Username',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('password', RepeatedType::class, array(
                'type' => PasswordType::class,
                'required' => $options['action'] === 'n' ? true : false,
                'first_options' => array(
                    'label' => 'Password',
                    'label_attr' => array(
                        'class' => $options['action'] === 'n' ? 'required middle' : 'middle'
                    )
                ),
                'second_options' => array(
                    'label' => 'Confirm Password',
                    'label_attr' => array(
                        'class' => $options['action'] === 'n' ? 'required middle' : 'middle'
                    )
                )
            ))
            ->add('first_name', TextType::class, array(
                'label' => 'First Name',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true
            ))
            ->add('middle_name', TextType::class, array(
                'label' => 'Middle Name',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('last_name', TextType::class, array(
                'label' => 'Last Name',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true
            ))
            ->add('is_active', CheckboxType::class, array(
                'label' => 'Active',
                'required' => false
            ))
            ->add('account_staff', HiddenType::class)
            ->add('account_user_billing', HiddenType::class)
            ->add('sales_agent', HiddenType::class)
            ->add('provider', HiddenType::class)
            ->add('marketingStaff', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $accountStaff = $data->getAccountStaff();
                $accountUserBilling = $data->getAccountUserBilling();
                $salesAgent = $data->getSalesAgent();
                $provider = $data->getProvider();
                $marketingStaff = $data->getMarketingStaff();

                $form
                    ->add('sales_agent_fullname', TextType::class, array(
                        'label' => 'Sales Agent',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $salesAgent ? $salesAgent->getFullName() : ''
                    ))
                    ->add('account_staff_fullname', TextType::class, array(
                        'label' => 'Account Staff',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $accountStaff ? $accountStaff->getFullName() : ''
                    ))
                    ->add('account_user_billing_fullname', TextType::class, array(
                        'label' => 'Account User (Billing)',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $accountUserBilling ? $accountUserBilling->getFullName() : ''
                    ))
                    ->add('provider_fullname', TextType::class, array(
                        'label' => 'Clinician',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $provider ? $provider->getFullName() : ''
                    ))
                    ->add('marketingStaff_fullname', TextType::class, array(
                        'label' => 'Marketing Staff',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $marketingStaff ? $marketingStaff->getFullName() : ''
                    ));
            });

        $builder->get('account_staff')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountStaffEntity', false));
        $builder->get('account_user_billing')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserBillingEntity', false));
        $builder->get('sales_agent')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:SalesAgentEntity', false));
        $builder->get('provider')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:ProviderEntity', false));
        $builder->get('marketingStaff')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:MarketingStaffEntity', false));
    }

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

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