<?php

namespace OfficeBrain\Bundle\ServiceGroupBundle\Form;

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

class ServiceRuleMappingActionType extends AbstractType
{
    /**
     * @param ContainerInterface $container
     */
    private $allowValidation; // Check whether variable is set or not.
    private $fieldAllowed; // Check whether variable is set or not.
    private $ControllerObj = null;
    private $cultureList = null;
    private $optionList = null;

    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';

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

        $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))
        ;
        $builder
        ->add('ruleMappingRuleId', 'hidden', array('required' => false))
        ;
        $builder->add('type', 'choice',
                array('required' => true,
                        'choices' => (isset($this->optionList['action_type']) ? $this->optionList['action_type'] : null),
                        'constraints' => array(new NotBlank(array('message' => 'admin.service_rule_mapping.select_type'))),
                )
        )
        ->add('limitBy', 'choice',
                array('required' => false,
                        'choices' => (isset($this->optionList['action_limit_by']) ? $this->optionList['action_limit_by'] : null),
                )
        )
        ->add('limitAllowed', 'text',
                array('required' => false,
                        'constraints' => array(new Assert\Range(array(
                                'min' => 0,
                                'max' => 100000,
                                'minMessage' => 'You cannot enter less than {{ limit }} limit.',
                                'maxMessage' => 'You cannot enter more than {{ limit }} limit.',
                        ))),
                )
        )
        ->add('ruleType', 'choice',
                array('required' => true,
                        'choices' => (isset($this->optionList['action_rule_type']) ? $this->optionList['action_rule_type'] : null),
                        'constraints' => array(new NotBlank(array('message' => 'admin.service_rule_mapping.rule_type'))),
                )
        )
        ->add('chargeType', 'choice',
                array('required' => true,
                        'choices' => (isset($this->optionList['action_charge_type']) ? $this->optionList['action_charge_type'] : null),
                        'constraints' => array(new Assert\Callback(array('methods' => array(array($this, 'checkRuleTypeIsCharges')))),
                        ),
                )
        )
        ->add('amountType', 'choice',
                array('required' => true,
                        'choices' => (isset($this->optionList['action_amount_type']) ? $this->optionList['action_amount_type'] : null),
                        'constraints' => array(new NotBlank(array('message' => 'admin.service_rule_mapping.amount_type'))),
                )
        )
        ->add('amount', 'text',
                array('required' => true,
                        'constraints' => array(
                                new NotBlank(array('message' => 'admin.service_rule_mapping.amount')),
                                new Assert\Callback(array('methods' => array(array($this, 'checkValidDecimal')))
                                ),
                        ), )
        )
        /*->add ( 'maxQty', 'text',
         array ('required' => false,
                 'constraints' => array(
                         new Assert\Callback(array('methods'=>array(array($this,'checkValidNumeric')))
                         ),
                 ))
        )*/
        ->add('isShippingFree', 'choice',
                array('required' => true,
                        'choices' => array('false' => 'No', 'true' => 'Yes'),
                        'constraints' => array(new NotBlank(array('message' => 'admin.service_rule_mapping.shipping_free'))),
                )
        )
        ->add('isTaxFree', 'choice',
                array('required' => true,
                        'choices' => array('false' => 'No', 'true' => 'Yes'),
                        'constraints' => array(new NotBlank(array('message' => 'admin.service_rule_mapping.tax_free'))),
                )
        )
        ->add('isActive', 'choice',
                array('required' => true,
                        'choices' => $this->groupStatus,
                        'constraints' => array(new NotBlank(array('message' => 'admin.service_rule_mapping.status'))),
                )
        )
        ;
        if ($this->formMode == 'add') {
        }
    }

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

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

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

    public function setControllerObj($obj)
    {
        $this->ControllerObj = $obj;
    }

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

    public function checkRuleTypeIsCharges($data, ExecutionContextInterface $context)
    {
        if ($this->form->has('ruleType') && $this->form->get('ruleType')->getData() == 1
                && $data == '') {
            $context->addViolation('admin.service_rule_mapping.charge_type');
        }
    }

    public function checkValidDecimal($data, ExecutionContextInterface $context)
    {
        if ($data != '' && !is_float($data) && !is_numeric($data)) {
            $context->addViolation('admin.service_rule_mapping.proper_amount');
        }
    }

    public function checkValidNumeric($data, ExecutionContextInterface $context)
    {
        if ($data != '' && !is_numeric($data)) {
            $context->addViolation('admin.service_rule_mapping.max_qty');
        }
    }
}
