<?php

namespace OfficeBrain\Bundle\ServiceGroupBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * @author OfficeBrain 4589 <info@officebrain.com>
 *
 * Description : Service Group Form Type
 */
class ServiceGroupType extends AbstractType
{
    private $allowValidation;

    private $fieldAllowed;

    private $data;

    /**
     * @author Employee Id: 4589
     * Description : initialize value
     *
     * @param 1 : ContainerInterface $container
     * @param 1 : array              $fromAdmin
     **/
    public function __construct(ContainerInterface $container, array $fromAdmin = array())
    {
        $this->container = $container;
        $this->allowValidation = (isset($fromAdmin ['allowValidation'])) ? $fromAdmin ['allowValidation'] : array(); // This value is set true when admin submitted registration form.
        $this->fieldAllowed = (isset($fromAdmin ['fieldAllowed'])) ? $fromAdmin ['fieldAllowed'] : array(); // This value is set true when admin submitted new registration form and
        // set false when admin submitted updated registration form.
        $this->formMode = (isset($fromAdmin ['mode'])) ? $fromAdmin ['mode'] : 'add';
        $this->data = (isset($fromAdmin ['data'])) ? $fromAdmin ['data'] : array();

        $this->serviceGroupRuleManager = $this->container->get('office_brain_service_group_rule_manager');
        $this->groupStatus = $this->serviceGroupRuleManager->getRuleStatus();
    }

    /**
     * @author Employee Id: 4589
     * Description :
     *
     * @param 1 : FormBuilderInterface $builder
     * @param 1 : array                $options
     **/
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', 'hidden', array('required' => false))
            ->add('countryId', 'choice', array(
                    'label' => 'Culture',
                    'choices' => $this->getCountryOptions($this->container->getParameter('country')),
                    'multiple' => false,
                    'required' => true,
                    'constraints' => array(new NotBlank(array('message' => 'service_group.culture.not_blank'))),
                    'expanded' => true,
            ));

        $builder->add('status', 'choice', array(
            'required' => false,
            'multiple' => false,
            'placeholder' => false,
            'choices' => $this->groupStatus,
        ));

        $builder->add('languageMapping', 'collection', array('type' => new ServiceGroupLanguageType(), 'cascade_validation' => true));

        if ($this->formMode == 'edit') {
            $builder->add('countryId', 'choice', array(
                'label' => 'Culture',
                'choices' => $this->getCountryOptions($this->container->getParameter('country')),
                'multiple' => false,
                'required' => true,
                'constraints' => array(new NotBlank(array('message' => 'service_group.culture.not_blank'))),
                'expanded' => true,
                'disabled' => true,
            ));
        }

        /*$builder->addEventListener(\Symfony\Component\Form\FormEvents::POST_BIND,
            function($event) {
                $form = $event->getForm();
                $data = $event->getData();

                if(!$data->languageMapping->isEmpty()) {

                }else{
                    $msg = 'Occorre specificare almeno un utente destinatario';
                    $form->get('users')->addError(new FormError($msg));
                }
        });*/
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OfficeBrain\Bundle\ServiceGroupBundle\Entity\ServiceGroup', 'cascade_validation' => true,
            'error_bubbling' => true,
        ));
    }

    /**
     * @author Employee Id: 4589
     * Description :
     *
     * @return : string
     **/
    public function getName()
    {
        return 'service_group_data';
    }

    /**
     * @author Employee Id: 4589
     * Description : bind country dropdown
     *
     * @param 1: array $countries
     *
     * @return array
     **/
    private function getCountryOptions($countries)
    {
        $countryOptions = array();
        foreach ($countries as $country) {
            $countryOptions[] = array($country['id'] => $country['name']);
        }

        return $countryOptions;
    }
}
