<?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 OfficeBrain\Bundle\CouponBundle\Form\EventListener\CouponEventListener;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
 * @author OfficeBrain 4835 <info@officebrain.com>
 */
class CouponType 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', array('required' => true))
                ->add('couponType', 'choice', array('empty_value' => 'Choose One',
                    'choices' => $discount_type,
                        )
                )
                ->add('couponAmount', 'text', array('required' => true))
                ->add('isAutogenerate')
                ->add('isFreeShipping')
                ->add('isBeforeTax', 'checkbox', array('data' => true))
                ->add('isSaleItem')
                ->add('usageLimit', 'text')
                ->add('usageCount', 'hidden')
                ->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')
                ->add('userGroups', 'choice', array('empty_value' => 'Choose One',
                    'choices' => $userRole,
                        )
                )
                ->add('status', 'hidden')
                ->add('couponPrifix');
        if ($this->mode == 'edit') {
            $builder->add('couponCode', 'text', array('read_only' => true))
                    ->add('isBeforeTax')
                    ->remove('isAutogenerate')
                    ->remove('couponPrifix')
                    ->remove('status')
                    ->add('status', 'choice', array('choices' => $status));
        } else {
            $builder->add('isMultiplecoupon', 'checkbox', array('mapped' => false))
                    ->add('isMakeActive', 'checkbox', array('mapped' => false))
                    ->add('isGenerateCsv', 'checkbox', array('mapped' => false))
                    ->add('couponTotal', 'text', array('mapped' => false));
        }
        if ($this->mode == 'smartadd') {
            $builder
                    ->add('couponCode', 'hidden', array('required' => false));
        }

        $builder->add('languageMapping', 'collection', array('type' => new CouponLanguageType(), 'cascade_validation' => true));

        $builder->addEventSubscriber(new CouponEventListener($manager));
    }

    /**
     * @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;
    }
}
