<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Form;

use App\Entity\Agent;
use App\Entity\TypeAgent;
use App\Form\DataTransformer\RolesTransformer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Defines the form used to edit an user.
 *
 * @author Mathieu Dauphin
 */
class AgentType extends AbstractType
{
    private $transformer;
    private $em;

    public function __construct(RolesTransformer $transformer, EntityManagerInterface $em) {
        $this->transformer = $transformer;
        $this->em = $em;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {

        $builder
            ->add('id', TextType::class, [
                'label' => 'code_agent',
                'label_attr' => [
                    'class' => 'me-2 col-form-label',
                ],
                'row_attr' => [
                    'class' => 'input-group',
                ],
            ])
            ->add('nom_agent', TextType::class, [
                'label' => 'nom_agent',
                'label_attr' => [
                    'class' => 'me-2 col-form-label',
                ],
                'row_attr' => [
                    'class' => 'input-group',
                ],
            ])
            ->add('type_agent', EntityType::class, [
                'class' => TypeAgent::class,
                'label' => 'Type',
                'choice_label' => 'libelle',
                'required' => false,
                'label_attr' => [
                    'class' => 'me-2 col-form-label',
                ],
                'row_attr' => [
                    'class' => 'input-group',
                ],
            ])
            ->add('responsable', EntityType::class, [
                'class' => Agent::class,
                'multiple' => false,
                'label_attr' => [
                    'class' => 'me-4 col-form-label',
                ],
                'row_attr' => [
                    'class' => 'input-group',
                ],
                'choice_label' => 'nom_agent',
                'label' => 'Profil',
            ])
        ;

    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Agent::class,
        ]);
    }

    private function setupSpecificSiteField(Form $form, ?TypeSav $type)
    {

        $choices = $this->getSiteChoices($type);

        $form->add('site', EntityType::class, [
            'class' => Site::class,
            'choice_label' => 'libelle',
            'multiple' => true,
            'choices' => $choices,
            'expanded' => true,
            'label' => ' ',
            'label_attr' => [
                'class' => '`checkbox-inline form-check form-check-inline col-form-label',
            ],
            'row_attr' => [
                'class' => 'input-group',
            ],
        ]);
    }

    private function getSiteChoices(?TypeSav $type)
    {
        if ($type) {
            return $this->em->getRepository(Site::class)->findBy(['type_sav' => $type]);
        } else {
            return $this->em->getRepository(Site::class)->findAll();
        }
    }
}
