<?php

namespace OfficeBrain\Bundle\ServiceGroupBundle\Form;

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

class ServiceRuleChargesEquationType extends AbstractType
{
    public function __construct(array $masterOptions = array())
    {
        $this->options = $masterOptions;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('baseCharge1', 'hidden')
            ->add('conditionOperator1', 'choice',
                    array('required' => true,
                            'choices' => $this->options['charge_operators'],
                            'constraints' => array(
                                    new NotBlank(array('message' => 'admin.charges_rule_mapping.amount_type.not_blank')),
                            )
                    ))
            ->add('baseCharge2', 'hidden')
            ->add('conditionOperator2', 'choice',
                    array('required' => true,
                            'choices' => $this->options['charge_operators'],
                            'constraints' => array(
                                    new NotBlank(array('message' => 'admin.charges_rule_mapping.amount_type.not_blank')),
                            )
                    ))
            ->add('isPerColor')
            ->add('isPerLocation')
            //->add('serviceRuleMappingId', 'hidden')



            ->add('isColumnQty', 'checkbox', array(
                    'required' => false,
            ))
            ->add('amountType', 'choice',
                    array('required' => true,
                            'choices' => $this->options['amount_type'],
                            'constraints' => array(
                                    new NotBlank(array('message' => 'admin.charges_rule_mapping.amount_type.not_blank')),
                                    new Assert\Callback(array('methods' => array(array($this, 'checkamountType')))),
                                    new Assert\Callback(array('methods' => array(array($this, 'checkValidamountType')))),
                            )
                    )
            )
            ->add('amount', 'text',
                    array('required' => false,
                            'constraints' => array(
                                    //new NotBlank(array('message' => 'Please enter Amount.')),
                                    new Assert\Callback(array('methods' => array(array($this, 'checkValidDecimal')))),
                            ), )
            )
        ;
    }

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

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

    public function checkamountType($data, ExecutionContextInterface $context)
    {
        $this->amountType = $data;
    }
    public function checkValidamountType($data, ExecutionContextInterface $context)
    {
        /* if ($this->amountType == '1' && $this->chargeType == '1' ) {
            $context->addViolation('admin.catalogue_rule_mapping.amount_type.valid');
        } */
    }


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