<?php

namespace Lc\ShopBundle\Form\Backend\ReductionCart;

use Doctrine\ORM\EntityManagerInterface;

use Doctrine\ORM\EntityRepository;
use Lc\ShopBundle\Context\ReductionCartInterface;
use Lc\ShopBundle\Context\ReductionCatalogInterface;
use Lc\ShopBundle\Context\UserInterface;
use Lc\ShopBundle\Repository\GroupUserRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UncombinablesType extends AbstractType
{
        protected $em;

        public function __construct(EntityManagerInterface $entityManager)
        {
                $this->em = $entityManager;
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
                $reductionCartClass = $this->em->getClassMetadata(ReductionCartInterface::class);

                $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($reductionCartClass) {
                        $builder = $event->getForm()->getParent();

                        $builder->add('uncombinables', EntityType::class, array(
                                'class' => $reductionCartClass->name,
                                'required' => false,
                                'multiple'=>true,
                                'choice_attr' => function ($choice, $key, $value) {
                                        return ['data-type' => $choice->getType()];
                                }
                        ));
                });
        }

        public function configureOptions(OptionsResolver $resolver)
        {
                /*$resolver->setDefaults([
                        'data_class' => $this->em->getClassMetadata(ReductionCatalogInterface::class)->getName(),
                ]);*/
        }
}
