<?php

namespace Lc\SovBundle\Form\Common;

use Lc\SovBundle\DataTransformer\FileManagerTypeToDataTransformer;
use Lc\SovBundle\Entity\File\File;
use Lc\SovBundle\Model\File\FileInterface;
use Lc\SovBundle\Doctrine\EntityManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class FileManagerType extends AbstractType implements DataTransformerInterface
{
        protected $em;

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

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
                $builder->add('path', HiddenType::class, array(
                        'block_prefix' => 'file_manager_image',
                        'label' => false
                ));
                $builder->add('legend', TextType::class, array(
                        'block_prefix' => 'file_manager_legend',
                        'attr'=> array(
                                "placeholder"=> 'Légende'
                        ),
                        'label' => false
                ));
                $builder->add('position', HiddenType::class, array(
                        'block_prefix' => 'file_manager_position',
                        'empty_data'=> 0,
                        'required' => true,
                        'attr' => array(
                                'class' => 'field-position'
                        ),
                        'label' => false
                ));
        }

        /**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
                $resolver->setDefaults([
                        'data_class' => $this->em->getEntityName(FileInterface::class),
                        'compound' => true,
                ]);
        }

        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
                return 'file_manager';
        }

        /**
         * {@inheritdoc}
         */
        public function transform($data)
        {
                // Model data should not be transformed
                return $data;
        }

        /**
         * {@inheritdoc}
         */
        public function reverseTransform($data)
        {
                return null === $data ? '' : $data;
        }
}
