<?php

namespace Lc\ShopBundle\Form\Backend\Filters;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Lc\ShopBundle\Services\Utils;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;


class ListFilterType extends AbstractType
{
        protected $em;
        protected $translator;
        protected $utils;

        public function __construct(EntityManagerInterface $entityManager, TranslatorInterface $translator, Utils $utils)
        {
                $this->em = $entityManager;
                $this->translator = $translator;
                $this->utils = $utils;
        }


        public function buildForm(FormBuilderInterface $builder, array $options)
        {

                foreach ($options['fields'] as $field) {

                        switch ($field['dataType']) {
                                case 'option':

                                        $refl = new \ReflectionClass($options['entity_class']);

                                        $choiceOptions= array();
                                        foreach ($refl->getConstants() as $key => $value) {
                                                if (stripos($key, $this->utils->snakeCase($field['property'])) !== false) {
                                                        $choiceOptions[$this->translator->trans('field.'.$options['entity_name'].'.'.$field['property'].'Options.'.$value)] = $value;
                                                }
                                        }

                                        $builder->add($field['property'], ChoiceType::class, array(
                                                'required' => false,
                                                'choices'=> $choiceOptions,
                                                'attr'=>array(
                                                        'class'=> 'select2 input-sm',
                                                        'form'=> 'filters-form'
                                                )
                                        ));
                                        break;
                                case 'integer':
                                        $builder->add($field['property'], TextType::class, array(
                                                'required' => false,
                                                'attr'=>array(
                                                        'class'=> ' input-sm',
                                                        'form'=> 'filters-form'
                                                )
                                        ));
                                        break;
                                case 'string':
                                        $builder->add($field['property'], TextType::class, array(
                                                'required' => false,
                                                'attr'=>array(
                                                        'class'=> ' input-sm',
                                                        'form'=> 'filters-form'
                                                )
                                        ));
                                        break;
                                case 'toggle':
                                        $builder->add($field['property'], ChoiceType::class, array(
                                                'choices'=> array(
                                                        'Oui' => 1,
                                                        'Non' => 0,
                                                ),
                                                'placeholder'=> '--',
                                                'required'=>false,
                                                'attr'=>array(
                                                        'class'=> 'select2 input-sm',
                                                        'form'=> 'filters-form'
                                                )
                                        ));
                                        break;
                                case 'datetime':
                                case 'date':
                                        $builder->add(
                                                $builder->create($field['property'], FormType::class, array('inherit_data' => true))
                                                ->add('dateStart', DateType::class, array(
                                                        'widget' => 'single_text',
                                                        'required' => false,
                                                ))
                                                ->add('dateEnd', DateType::class, array(
                                                        'widget' => 'single_text',
                                                        'required' => false,

                                                ))
                                        );
                                        break;
                                case 'association':
                                        $classImplements = class_implements($field['targetEntity']);
                                        $builder->add($field['property'], EntityType::class, array(
                                                'class' => $field['targetEntity'],
                                                'placeholder'=> '--',
                                                'query_builder' => function (EntityRepository $repo) use ($classImplements) {
                                                        if (in_array('Lc\ShopBundle\Context\FilterMerchantInterface', $classImplements)) {
                                                                return $repo->findByMerchantQuery();
                                                        }else{
                                                                return $repo->createQueryBuilder('entity');
                                                        }

                                                },
                                                'required' => false,
                                                'attr'=>array(
                                                        'class'=> 'select2 input-sm',
                                                        'form'=> 'filters-form'
                                                )


                                        ));
                                        break;
                                case 'dateinterval':

                                        break;
                                case 'float':

                                        break;
                        }


                }
                /*                $builder
                                        ->add('title', TextType::class, [
                                                'label' => 'Titre',
                                                'required' => false,
                                        ])
                                        ->add('behaviorTaxRate', BehaviorTaxRateType::class)
                                        ->add('unit', UnitType::class)
                                        ->add('value', ValueType::class)
                                        ->add('permanent', PermanentType::class)
                                        ->add('dateStart', DateStartType::class)
                                        ->add('dateEnd', DateEndType::class)
                                        ->add('users', UsersFilterType::class)
                                        ->add('groupUsers', GroupUsersFilterType::class);*/
        }

        public function configureOptions(OptionsResolver $resolver)
        {
                $resolver->setDefaults([
                        'label' => false,
                        'csrf_protection'=> false,
                        'translation_domain' => 'lcshop',
                        'fields' => false,
                        'entity_name' => false,
                        'entity_class' => false,
                        //'entityClass' => false
                ]);
        }
}
