<?php

namespace Lc\SovBundle\Form\Reminder;

use Lc\SovBundle\Model\Reminder\ReminderInterface;
use Lc\SovBundle\Model\User\UserInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
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\OptionsResolver\OptionsResolver;

class ReminderAdminFormType extends AbstractType
{
    protected $entityManager;

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $userClass = $this->entityManager->getEntityName(UserInterface::class);
        $userRepo = $this->entityManager->getRepository(UserInterface::class);
        $builder
                ->add('title', TextType::class, ['label' => 'Titre'])
                ->add(
                        'description',
                        TextareaType::class,
                        array(
                                'required' => false
                        )
                )
                ->add(
                        'crudAction',
                        HiddenType::class,
                        array(
                                'required' => false
                        )
                )
                ->add(
                        'crudControllerFqcn',
                        HiddenType::class,
                        array(
                                'required' => false
                        )
                )
                ->add(
                        'entityId',
                        HiddenType::class,
                        array(
                                'required' => false
                        )
                )
                ->add(
                        'users',
                        EntityType::class,
                        array(
                                'class' => $userClass,
                                'choices' => $userRepo->findByRole('ROLE_ADMIN'),
                                'required' => false,
                                'multiple' => true,
                                'expanded' => false
                        )
                )
                ->add(
                        'dateReminder',
                        DateType::class,
                        array(
                                'required' => false,
                                'widget' => 'single_text',
                        )
                );
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
                [
                        'data_class' => $this->entityManager->getEntityName(ReminderInterface::class)
                ]
        );
    }
}
