<?php
/**
 * Created by PhpStorm.
 * User: software
 * Date: 17/7/14
 * Time: 3:07 PM
 */

namespace OfficeBrain\Bundle\ObSolrOperationBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Regex;

class OperationType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('operation', 'choice', array('choices' => $this->getOperations(),    'required'    => true,
            'empty_value' => 'Please select operation',
            'empty_data'  => ''));
        $builder->add('culture', 'choice', array('choices' => array('en' => "English", "fr" => "French"), 'expanded' => true));
        $builder->add('supplierId', 'textarea', array('required' => false, 'constraints' => array(new Regex(array('pattern' => '/^((all)?|(((\d)+(,)?)+)?)$/', 'message' => "Please provide a valid supplier id")))));
        //$builder->add('imageFolderId', 'textarea', array('required' => false, 'constraints' => array(new Regex(array('pattern' => '/^((all)?|(((\d)+(,)?)+)?)$/', 'message' => "Please provide a valid Image folder id")))));
        $builder->add('productId', 'textarea', array('required' => false, 'constraints' => array(new Regex(array('pattern' => "/^((((\d)+(,)?)+)?)$/", 'message' => "Please provide a valid product id")))));
        $builder->add('sku', 'textarea', array('required' => false, 'constraints' => array(new Regex(array('pattern' => "/^(((([a-zA-Z0-9\-\.\_])+(,)?)+)?)$/", 'message' => "Please provide a valid product id")))));
        $builder->add('submit', 'submit');
        $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
            $data = $event->getData();
            switch ($data["operation"]) {
                case "sync_product":
                    if (trim($data["supplierId"]) == "" && trim($data["productId"]) == "") {
                        throw new \LogicException("Please provide either supplierId or productId");
                    }
                    break;
                case "delete_product":
                    if (trim($data["supplierId"]) == "" && trim($data["productId"]) == "" && trim($data["sku"]) == "") {
                        throw new \LogicException("Please provide either supplierId or productId or skus");
                    }
                    break;
                /*case "delete_thumbnail":
                    if (trim($data['imageFolderId'])=="") {
                        throw new \LogicException("Please provide valid image folder ids.");
                    }
                    break;*/
            }
        });
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array('data_class' => 'OfficeBrain\Bundle\ObSolrOperationBundle\Form\Data\OperationData',));
    }


    /**
     * Returns the name of this type.
     *
     * @return string The name of this type
     */
    public function getName()
    {
        return 'operation';
    }

    public function getOperations()
    {
        return array('sync_product' => 'Insert/Update Product Data',  'delete_product' => 'Delete Product');
    }

} 