<?php

namespace App\Form\Modpack;

use App\Entity\Mod;
use App\Entity\Modpack;
use App\Repository\ModRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ModpackType extends AbstractType
{
    public function __construct(private readonly ModRepository $modRepository) {}
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $isEdit = $options['data']?->getId() !== null;

        $mods = $this->modRepository->findBy([], ['nom' => 'ASC', 'version' => 'ASC']);

        $builder
            ->add('version',TextType::class,[
                'label' => 'Version',
            ])
            ->add('downloadable', CheckboxType::class, [
                'label' => 'Téléchargeable',
                'required' => false,
                'disabled' => !$isEdit,
            ])
            ->add('mods', EntityType::class, [
                'mapped' => false,
                'class' => Mod::class,
                'choice_label' => function (Mod $mod) {
                    return $mod->getNom() . ' - ' . $mod->getVersion();},
                'choices' => $mods,
                'multiple' => true,
                'expanded' => true,
                'label' => 'Mods',
                'required' => false,
            ])
            ->add('mods_optionnels', EntityType::class, [
                'mapped'       => false,
                'class'        => Mod::class,
                'choice_label' => fn(Mod $mod) => $mod->getNom() . ' - ' . $mod->getVersion(),
                'choices'      => $mods,
                'multiple'     => true,
                'expanded'     => true,
                'label'        => false,
                'required'     => false,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {


        $resolver->setDefaults([
            'data_class' => Modpack::class,
        ]);
    }
}
