<?php

namespace Lc\ShopBundle\Form\Backend\ProductFamily;

use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\ProductCategoryInterface;
use Lc\ShopBundle\Context\ProductInterface;
use Lc\ShopBundle\Repository\ProductCategoryRepository;
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 ProductFamilyCategoriesType extends AbstractType
{
        protected $em;
        protected $productCategoryRepository ;

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

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
                $categories = $this->productCategoryRepository->findAllParents(true);

                $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($categories) {
                        $builder = $event->getForm();
                        $currentProductCategories = $event->getData();

                        foreach ($categories as $category) {
                                $builder->add('category_' . $category->getId(), CheckboxType::class, [
                                        'label' => $category->getStatus() == 0 ? $category->getTitle() .' (hors ligne)': $category->getTitle() ,
                                        'data' => $currentProductCategories->contains($category),
                                        'required' => false,
                                        'disabled'=>true,
                                        'attr'=>[
                                                'class'=>'none'
                                        ]
                                ]);
                                $childrenCategories = $this->productCategoryRepository->findAllByParent($category, true);
                                foreach ($childrenCategories as $children) {
                                        $builder->add('category_children_' . $children->getId(), CheckboxType::class, [
                                                'label' =>  $children->getStatus()  == 0 ? $children->getTitle() .' (hors ligne)': $children->getTitle() ,
                                                'data' => $currentProductCategories->contains($children),
                                                'required' => false
                                        ]);
                                }
                        }
                });
        }

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