<?php

namespace OfficeBrain\Bundle\CmsBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; // Added additionaly to get countries
use Symfony\Component\Validator\ExecutionContextInterface; // Added additionally to throw exception
use Symfony\Component\Validator\Constraints as Assert; //Added additionaly to set the callback

class CmsSearchCriteriaType extends AbstractType
{
    private $container;
    private $cmsSearchCriteriaManager;

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

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('attribute', 'text',
                array(
                    'required' => false,
                    'constraints' => array(
                        new Assert\Callback(array('methods' => array(array($this, 'checkQuery'))))
                    )
                ))
            ->add('countryId', 'choice', array(
                    'label' => 'Country',
                    'choices' => $this->getCountryOptions($this->container->getParameter('country')),
                    'multiple' => false,
                    'required' => true,
                    'expanded' => true,
                    'disabled' => isset($options['data']) && $options['data']->getCountryId() > 0 ? true : false,
            ))
            ->add('status', 'choice', array(
                    'required' => true,
                      'multiple' => false,
                      'choices' => array(1 => 'Active', 0 => 'In Active'),
              ))
            ->add('languageMapping', 'collection', array('type' => new CmsSearchCriteriaLanguageType(), 'cascade_validation' => true))
        ;
    }

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

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

    private function getCountryOptions($countries)
    {
        $countryOptions = array();
        foreach ($countries as $country) {
            $countryOptions[] = array($country['id'] => $country['name']);
        }

        return $countryOptions;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to check the query is set or not
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Not Blank
     */
    public function checkQuery($data, ExecutionContextInterface $context)
    {
        $attributes = json_decode($data, true);
        
        $attributeJson = $this->cmsSearchCriteriaManager->formatAttribute($attributes); // Array 
        
        if (empty($attributeJson) || $attributeJson == '[]') {
            $context->addViolation('cms.search_criteria.admin.form.attribute.not_blank');
        }
        
        return $data;
    }
}
