<?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 as Assert;
use Symfony\Component\Validator\ExecutionContextInterface;

class ServiceRuleMasterType extends AbstractType
{
    /**
     * @param ContainerInterface $container
     */
    /*
     * public function __construct(ContainerInterface $container) { $this->container = $container; }
     */
    private $serviceRuleManager = null;
    private $cultureList = null;

    public function __construct(ContainerInterface $container, array $fromAdmin = array())
    {
        $this->container = $container;

        // set false when admin submitted updated registration form.
        $this->formMode = (isset($fromAdmin ['mode'])) ? $fromAdmin ['mode'] : 'add';

        if (isset($fromAdmin ['cultureList'])) {
            $this->cultureList = $fromAdmin ['cultureList'];
        }

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

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('id', 'hidden', array('required' => false))
                ->add('ruleType', 'hidden', array('required' => false))
                ->add('ruleName', 'text', array('required' => true,
                    'constraints' => array(
                        new Assert\NotBlank(array(
                            'message' => 'service_group.rule_master.rule_name.not_blank',
                                )),
                        new Assert\Callback(array('methods' => array(array($this, 'checkRuleNameIsUnique')))),
                    ),
                        )
                )
                ->add('status', 'choice', array(
                    'required' => false,
                    'multiple' => false,
                    'choices' => $this->groupStatus,
        ));

        if ($this->formMode == 'add') {
            $builder->add('countryId', 'choice', array('required' => true,
                'choices' => $this->cultureList,
                'multiple' => false,
                'expanded' => true,
                    )
            );
        } else {
            $builder->add('countryId', 'choice', array('required' => true,
                'choices' => $this->cultureList,
                'multiple' => false,
                'expanded' => true,
                'disabled' => true,
                    )
            );
        }
    }

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

    public function setForm($form)
    {
        $this->form = $form;
    }

    public function setServiceRuleManager($serviceRuleManager)
    {
        $this->serviceRuleManager = $serviceRuleManager;
    }

    public function setRequest($request)
    {
        $this->request = $request;
    }

    public function checkRuleNameIsUnique($ruleName, ExecutionContextInterface $context)
    {
        if ($this->serviceRuleManager) {
            $countryId = $this->form->get('countryId')->getData();
            $id = $this->form->get('id')->getData();
            if ($this->serviceRuleManager->checkRuleNameExistServiceRuleMaster($ruleName, $countryId, $id)) {
                $context->addViolation('service_group.rule_master.rule_name.different');
            }
        }
    }

    public function checkRuleHasError($hasRuleCondError)
    {
        if ($hasRuleCondError) {
            $context->addViolation('service_group.rule_master.rule_name.condition');
        }
    }
}
