<?php

namespace Lc\ShopBundle\Form\Backend\Merchant;

use CKSource\Bundle\CKFinderBundle\Form\Type\CKFinderFileChooserType;
use Lc\ShopBundle\Context\MerchantConfigInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
use Vich\UploaderBundle\Form\Type\VichImageType;

class MerchantConfigType extends AbstractType
{
        protected $em;

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

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

                $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {

                        $form = $event->getForm();

                        $merchantConfig = $event->getData();

                        if ($merchantConfig) {
                                $form->add('name', HiddenType::class) ;

                                if($merchantConfig->getFieldType() == 'choice') {
                                        $form->add('value', ChoiceType::class, [
                                                'label' => $merchantConfig->getLabel(),
                                                'choices' => $merchantConfig->getChoices()
                                        ]);
                                }
                                elseif($merchantConfig->getFieldType() == 'date') {
                                        $form->add('value', DateType::class, [
                                                'label' => $merchantConfig->getLabel(),
                                                'widget' => 'single_text',
                                        ]);
                                }
                                elseif($merchantConfig->getFieldType() == 'image') {
                                        $form->add('imageFile', VichImageType::class, [
                                                'label' => $merchantConfig->getLabel(),
                                        ]);
                                }
                                else {
                                        if($merchantConfig->getOption()) {
                                                $form->add('value', TextType::class, [
                                                        'label' => $merchantConfig->getLabel(),
                                                ]);
                                        }
                                }
                        }
                });

        }

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

