<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\ExecutionContextInterface as context;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\EntityRepository;


class ContactUsType extends AbstractType
{

	/**
	 *
	 * @param ContainerInterface $container
	 * @param $mode
	 */
	public function __construct(ContainerInterface $container)
	{
		$this->container = $container;
		//$this->side = $side;
		$this->request = $this->container->get('request');
		$this->projectSetting = $this->request->get('project_user_setting')['user_data'];
		//$this->userId = $this->projectSetting->getId();
	}

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

    	$countryConfiguration=array();
    	$master = $this->container->get('office_brain_core_manager');
    	foreach ($this->container->getParameter('country') as $key=>$value) {
    		$countryConfiguration[]=$value['short_name'];
    	}


    	$countryConfiguration=array();
    	foreach ($this->container->getParameter('country') as $key=>$value) {
    		$countryConfiguration[]=$value['short_name'];
    	}
    	$country=$master->getCountryData($countryConfiguration);
    	if ($builder->getData()->getCountryId() != "") {
    		$state = $master->getStateData($builder->getData()->getCountryId());
    	} else {
    		$state = array();
    	}
    	if ($builder->getData()->getStateId() != "") {
    		$city = $master->getCityData($builder->getData()->getStateId(), $builder->getData()->getCountryId());
    	} else {
    		$city = array();
    	}


        $builder
        ->add('fullName','text', array(
        		'required' => false,
        		'max_length' => 255,
        		'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.first_name.not_blank')))
        ))
        ->add('lastName','text', array(
        		'required' => false,
        		'max_length' => 255,
        		'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.last_name.not_blank')))
        ))
        ->add('email','text', array(
        		'required' => false,
        		'max_length' => 255,
        		'constraints'=>array(
        				new Assert\NotBlank(array('message'=>'contact_us.email.notBlank')),
        				new Assert\Regex(array('pattern' => '/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-z]{2,4}$/','message'=>'order_catalog.senderEmailAddress.invalid'))),
        		'label' => 'contact_us.senderEmailAddress.label','translation_domain' => 'validators'

        ))
        ->add('companyName','text', array(
        		'required' => false,
        		'max_length' => 255,
        		//'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.company_name.not_blank')))
        ))
        ->add('countryId', 'choice', array(
        		'choices'=> $country,
        		'placeholder' => 'contact_us.country.label',
        		//'empty_value'=>  '',
        		'empty_data'  => null,
        ))
        ->add('stateId', 'choice', array(
        		'choices'=> $state,
        		'placeholder' => 'contact_us.state.label',
        		'empty_data'  => null,
        ))
        ->add('cityId', 'choice', array(
        		'choices'=> $city,
        		'placeholder' => 'contact_us.city.label',
        		'empty_data'  => null,
        ))
        ->add('phone','text', array(
        		'required' => false,
        		'constraints'=>array(
        				new Assert\NotBlank(array('message'=>'contact_us.phone.not_blank'))),

        ))
        ->add('cityName','text',array(
        		'required' => false,
        		'max_length' => 255,
        		//'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.cityName.not_blank')))
        ))
        ->add('stateName','text',array(
        		'required' => false,
        		'max_length' => 255,
        		//'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.stateName.not_blank')))
        ))
        ->add('countryName','text',array(
        		'required' => false,
        		'max_length' => 255,
        		//'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.countryName.not_blank')))
        ))
        ->add('street1','text', array(
        		'required' => false,
        		'max_length' => 255,
        		//'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.street1.not_blank')))
        ))
        ->add('street2','text', array(
        		'max_length' => 255
        ))
        ->add('zipcode','text', array(
        		'required' => false,
        		//'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.zipcode.not_blank')))
        ))
        ->add('distributorPurchasInfo','choice', array(
        		'choices' => array(
        				'1' => 'Yes',
        				'2' => 'No'
        		),
        		'multiple' => false,
        		'expanded' => true,
        		'required'    => false,
        		'placeholder' => false,

        ))

        ->add('message','textarea', array(
        		'required' => false,
        		'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.message.not_blank')))
        ))
        ->add('subject','text', array(
        		'required' => false,
        		'max_length' => 255,
        		'constraints'=>array( new Assert\NotBlank(array('message'=>'contact_us.subject.not_blank')))
        ))
        ;
            $formModifier1 = function (FormInterface $form, $state) {
            	$form->add('stateId', 'choice', array(
            			'placeholder' => 'contact_us.state.label',
            			'empty_data'  => null,
            			'choices' => $state,
            	));
            };

            $formModifier2 = function (FormInterface $form, $city) {
            	$form->add('cityId', 'choice', array(
            			'placeholder' => 'contact_us.city.label',
            			'empty_data'  => null,
            			'choices' => $city,
            	));
            };

            $builder->addEventListener(
            		FormEvents::PRE_SET_DATA,
            		function (FormEvent $event) use ($formModifier1, $formModifier2, $master) {
            			// this would be your entity, i.e. SportMeetup
            			$data = $event->getData();
            			$country = $data->getCountryId()? $data->getCountryId():'';
            			$state1 = $data->getStateId()? $data->getStateId():'';
            			$state=$master->getStateData($country);
            			$formModifier1($event->getForm(), $state);
            			$city=$master->getCityData($state1, $country);
            			$formModifier2($event->getForm(), $city);
            		}
            		);



            $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($formModifier1, $formModifier2, $master) {
            	$contactBook = $event->getData();
            	$form = $event->getForm();

            	if ($form->has('countryId')&& isset($contactBook['countryId'])) {
            		$stateDataArray=$master->getStateData($contactBook['countryId']);
            		$formModifier1($form, $stateDataArray);
            	}
            	if ($form->has('stateId') && isset($contactBook['stateId']) ) {
            		$cityDataArray=$master->getCityData($contactBook['stateId']);

            		$formModifier2($form, $cityDataArray);
            	}

            });
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OfficeBrain\CustomBundle\CmsBundle\Entity\ContactUs'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'officebrain_bundle_cmsbundle_contactus';
    }
}
