<?php
namespace OfficeBrain\Bundle\PaymentBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
//use OfficeBrain\Bundle\PaymentBundle\Form\Extension\PaymentCreditCardExpirationDateTypeExtension;
use OfficeBrain\Bundle\PaymentBundle\Form\PaymentCreditCardExpirationDateType;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Luhn;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\ExecutionContextInterface;

class PaymentCardType extends AbstractType
{
	private $flag;
	
	public function __construct($flag=true){
		$this->flag = $flag;
	}
  /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    { 
    	if($this->flag==true)
    	{
     	 $builder
            ->add('holder', 'text', array('constraints'=>
            		array(
            				new NotBlank(array('message'=>'payment.holder.not_blank')),
            				new Assert\Length(array('min'=>'4', 'minMessage' => 'payment.holder.min_message' ))
            			 )
            ))
            ->add('number', 'text', array('constraints'=>
            		array(
            				new NotBlank(array('message'=>'payment.number.not_blank')),
            				new Luhn(array('message' => 'payment.number.invalid_card_number' ))
            			)
            ))
            ->add('securityCode', 'password', array('constraints'=>
            		array(
            				new NotBlank(array('message'=>'payment.password.not_blank')),
            				new Assert\Length(array('min'=>'3', 'minMessage' => 'payment.password.min_charecter' )),
            				new Assert\Length(array('max'=>'4', 'maxMessage' => 'payment.password.max_character' )),
            				new Assert\Regex(array("pattern"=>"/^\S+$/","match"=>true, "message"=>"payment.password.invalid_code"))
            			)
            ))
            ->add('expireAt', new PaymentCreditCardExpirationDateType(),
                array(
                    'input' => 'datetime',
                    'widget' => 'choice',
                	'constraints'=>
                		array(
                				new NotBlank(array('message'=>'payment.expire.not_blank')),
                				new DateTime(array('message'=>'payment.expire.valid_date')),
						new Assert\Callback(array('methods' => array( array($this, 'checkMonthYear') )))
            				)
                )
            )
             ->add('term', 'checkbox',array('constraints'=> new NotBlank(array('message'=>'terms_conditions'))));
    	}
    	else
    	{
    		$builder
    		->add('holder', 'text')
    		->add('number', 'text')
    		->add('securityCode', 'password')
    		->add('expireAt', new PaymentCreditCardExpirationDateType())
    		->add('term', 'checkbox');
    		;
    		
    	} 
    }

     /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
         $resolver->setDefaults(array(
            'data_class' => null
        ));
    }

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

    /**
     * Checks month not a past month
     *
     * @param string $dateTime
     * @param \Symfony\Component\Validator\ExecutionContextInterface $context
     */
    public function checkMonthYear($dateTime, ExecutionContextInterface $context)
    {
    	if (new \DateTime() > new \DateTime($dateTime->format('Y-m-d H:i:s'))) {
    		$context->addViolation('payment.expire.valid_date', array(), null);
    	}
    }
}
