<?php

namespace App\Form\Mod;

use App\Entity\Mod;
use App\Repository\ModRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;

class ModType extends AbstractType
{
    public function __construct(private readonly ModRepository $modRepository) {}

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $placeholderModNames = [
            'Create',
            'Ars Nouveau',
            'Oculus',
            'JEI',
        ];
        $isEdit = $options['data']?->getId() !== null;
        $mods = [];
        if ($isEdit) {
            $mods = $this->modRepository->findAvailableDependances($builder->getData());
        }

        $builder
            ->add('nom', TextType::class, [
                'disabled' => $isEdit,
                'label' => 'Nom du mod',
                'required' => true,
                'attr' => [
                    'placeholder' => 'Ex: '.$placeholderModNames[array_rand($placeholderModNames)],
                ],
            ])
            ->add('version', TextType::class, [
                'label' => 'Version du mod',
                'required' => true,
            ])
            ->add('uri', FileType::class, [
                'disabled' => $isEdit,
                'mapped' => false,
                'label' => 'Fichier',
                'constraints' => [
                    new Assert\File([
                        'maxSize' => '128M',
                        'maxSizeMessage' => 'Le fichier est trop volumineux ({{ size }} {{ suffix }}). La taille maximale autorisée est {{ limit }} {{ suffix }}.',
                        'mimeTypes' => [
                            'application/java-archive',
                            'application/x-java-archive',
                            'application/x-jar',
                            'application/zip',              // JAR = ZIP, souvent détecté ainsi
                            'application/x-zip-compressed',
                        ],
                        'mimeTypesMessage' => 'Uniquement des fichiers JAR sont autorisés.',
                   ]),
                ],
                'required' => !$isEdit, // Obligatoire uniquement lors de la création
                'attr' => [
                    'accept' => '.jar',
                ]
            ])
            ->add('dependances', EntityType::class, [
                'class' => Mod::class,
                'choice_label' => function (Mod $mod) {
                    return $mod->getNom() . ' - ' . $mod->getVersion();},
                'multiple' => true,
                'required' => false,
                'choices' => $mods,
                'disabled' => !$isEdit,
                'attr' => [
                    'id' => 'dependances',
                    'data-placeholder'=> $isEdit? 'Rechercher une dépendance...' : 'Ajouter une dependance après la création du mod',
                ],
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Mod::class,
        ]);
    }
}
