<?php

namespace OfficeBrain\CustomBundle\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use OfficeBrain\Bundle\UserBundle\Entity\User;
//use OfficeBrain\Bundle\UserBundle\Form\FindDistributorType as baseFindDistributorType;

class FindDistributorType extends AbstractType
{

	public function __construct(ContainerInterface $container)
    {
        $this->container = $container;    
        
    }

	/**
	 * @param FormBuilderInterface $builder
	 * @param array $options
	 */
	public function buildForm(FormBuilderInterface $builder, array $options)
	{
		$master = $this->container->get('office_brain_core_manager');		
		$countryConfiguration=array();
		foreach ($this->container->getParameter('country') as $key=>$value) {
			$countryConfiguration[]=$value['short_name'];
		}
		$country=$master->getCountryData($countryConfiguration);
		if ($builder->getData()['countryId'] != "") {
			$state = $master->getStateData($builder->getData()['countryId']);
		} else {
			$state = array();
		}
		if ($builder->getData()['stateId'] != "") {
			$city = $master->getCityData($builder->getData()['stateId'], $builder->getData()['countryId']);
		} else {
			$city = array();
		}
		
		$builder->add('countryId', 'choice', array(				
				'choices' => $country,
				'empty_value' => 'user.find_distributor.country.label',
				'data'=>'233'				
		))
		->add('stateId', 'choice', array(
				'choices'=> $state,
				'placeholder' => 'user.find_distributor.state.label',
				'empty_data'  => null,
		))
		->add('cityId', 'choice', array(
				'choices'=> $city,
				'placeholder' => 'user.find_distributor.city.label',
				'empty_data'  => null,
		))		
		->add('zipcode', 'text', array (
				'required'  => false,
		));

		$formModifier1 = function (FormInterface $form, $state) {			
			$form->add('stateId', 'choice', array(
				'placeholder' => 'user.find_distributor.state.label',
                'empty_data'  => null,
                'choices' => $state,
          	));
		};		
		
		$formModifier2 = function (FormInterface $form, $city) {			
			$form->add('cityId', 'choice', array(
				'placeholder' => 'user.find_distributor.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['countryId']? $data['countryId']:'';
					$state1 = $data['stateId']? $data['stateId']:''; 
					$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) {
			$eventData = $event->getData();
			$form = $event->getForm();
		
			if ($form->has('countryId')&& isset($eventData['countryId'])) {
				$stateDataArray=$master->getStateData($eventData['countryId']);
				$formModifier1($form, $stateDataArray);
			}			
			if ($form->has('stateId') && isset($eventData['stateId']) ) {
				$cityDataArray=$master->getCityData($eventData['stateId']);							
				$formModifier2($form, $cityDataArray);
			}
		
		});		
			
	}

	
	/**
	 * @param OptionsResolverInterface $resolver
	 */
	public function setDefaultOptions(OptionsResolverInterface $resolver)
	{
		$resolver->setDefaults(array(
				'data_class' => null
		));
	}
	
	
    /**
     * @return string
     */
    public function getName()
    {
        return 'find_distributor';
    }

}
