<?php

namespace OfficeBrain\Bundle\CouponBundle\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 as Assert;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
 * @author OfficeBrain 4835 <info@officebrain.com>
 */
class CouponimportType extends AbstractType
{
    /**
     * @param $mode
     */
    public function __construct(ContainerInterface $container, $mode = 'add')
    {
        $this->container = $container;
        $this->mode = $mode;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $discount_type = $this->container->getParameter('discount_type');
        $manager = $this->container->get('office_brain_coupon_manager');
        $userRole = $this->container->getParameter('user_role');
        $status = $this->container->getParameter('statusMode');

        if ($this->mode == 'edit') {
            $builder->add('countryId', 'hidden');
        } else {
            $builder->add('countryId', 'choice', array(
                'label' => 'Culture',
                'choices' => $this->getCountryOptions($this->container->getParameter('country')),
                'multiple' => false,
                'required' => true,
                'constraints' => array(new NotBlank(array('message' => 'admin.coupon.selculture'))),
                'expanded' => true,
            ));
        }

        $builder->add('couponCode', 'text')
                ->add('couponType', 'choice', array('empty_value' => 'Choose One',
                    'choices' => $discount_type,
                    'constraints' => array(new NotBlank(array('message' => 'admin.not_blank'))),
                        )
                )
                ->add('couponAmount', 'text', array('required' => true))
                ->add('isFreeShipping')
                ->add('isBeforeTax', 'checkbox', array('data' => true))
                ->add('isSaleItem')
                ->add('usageLimit', 'text')
                ->add('startDate', 'date', array('format' => 'MM-dd-yyyy', 'widget' => 'single_text', 'read_only' => true, 'constraints' => array(
                        new Assert\NotBlank(array('message' => 'admin.not_blank')),
                )))
                ->add('endDate', 'date', array('format' => 'MM-dd-yyyy', 'widget' => 'single_text', 'read_only' => true, 'constraints' => array(
                        new Assert\NotBlank(array('message' => 'admin.not_blank')),
                )))
                ->add('userEmail', 'text', array('constraints' => array(new Assert\Email((array('message' => 'admin.not_blank'))))))
                ->add('userGroups', 'choice', array('empty_value' => 'Choose One',
                    'choices' => $userRole,
                        )

                )

               ->add('status', 'choice', array('choices' => $status, 'constraints' => array(new NotBlank(array('message' => 'admin.not_blank')))));

        $builder->add('category', 'text', array('mapped' => false));
        $builder->add('product', 'text', array('mapped' => false));
        $builder->add('exproduct', 'text', array('mapped' => false));

        if ($this->mode == 'edit') {
            $builder->remove('languageMapping', 'collection', array('type' => new CouponLanguageType(), 'cascade_validation' => false));
        } else {
            $builder->add('languageMapping', 'collection', array('type' => new CouponLanguageType(), 'cascade_validation' => false));
        }

        $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
        $product = $event->getData();
        $form = $event->getForm();
        $couponType = $product->getCouponType();
        $couponamount = $product->getCouponAmount();

            if (($couponType == 'percent' || $couponType == 'percent_product')) {
                if ($couponamount > 100) {
                    $form->add('couponAmount', 'text', array('constraints' => array(
                            new Assert\LessThan(array('value' => 100, 'message' => 'admin.valid_percentage')), ),
                            )
                    );
                }
            }

    });
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OfficeBrain\Bundle\CouponBundle\Entity\Coupon', 'cascade_validation' => true, 'csrf_protection' => false,
        ));
    }

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

    /**
     * @author Employee Id: 4835
     * Description :
     *
     * @param 1: array $countries
     *
     * @return array
     * */
    private function getCountryOptions($countries)
    {
        $countryOptions = array();
        foreach ($countries as $country) {
            $countryOptions[] = array($country['id'] => $country['name']);
        }

        return $countryOptions;
    }
}
