<?php

namespace App\Dto\Ag;

use App\Entity\Ag\Main;
use App\Entity\Ag\MainMember;
use App\Entity\Ag\MainVote;
use App\Entity\Members;
use App\Repository\MembersRepository;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
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 AgVoteType extends AbstractType
{
    public function __construct(private MembersRepository $membersRepository)
    {
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /** @var Main $main */
        $main = $options['data']->getMain();
        $choices = [];

        foreach ($this->membersRepository->findAll() as $member) {
            if(!is_null($main)){
            $present = $main->getMainVote()->filter(function (MainVote $mainMember) use ($member) {
                return $member->getId() == $mainMember->getMember()->getId();
            })->first();
            if(!$present instanceof MainVote) {
                $choices[$member->getPseudo()] = $member->getId();
            }
            }
        }

        $builder
            ->add('member', ChoiceType::class, [
                'label' => 'Membre',
                'choices' => $choices,
                'mapped' => false,
            ])
            ->add('role', ChoiceType::class, [
                'label' => 'Rôle au sein de l\'association',
                'choices' => [
                    'Président(e)' => 'President',
                    'Trésorier(e)' => 'Tresorier',
                    'Secrétaire(e)' => 'Secretaire',
                    'Vice-Président(e)' => 'VicePresident',
                    'Trésorier(e) Adjoints' => 'TresorierAdjoint',
                    'Secrétaire(e) Adjoints' => 'SecretaireAdjoint',
                ],
            ])
            ->add('pour',NumberType::class,[
                'label' => 'Pour',
                'required' => true,
                'empty_data' => 0,
            ])
            ->add('contre',NumberType::class,[
                'label' => 'Contre',
                'required' => true,
                'empty_data' => 0,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefault('data_class',MainVote::class);
    }
}
