<?php

namespace App\Form;

use App\Entity\Game;
use App\Entity\Genre;
use App\Entity\User;
use App\Enum\AgeRange;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;

class GameType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', null, [
                'label' => 'Nom du jeu',
                'required' => true,
                'help' => 'Entrez le nom du jeu.',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('slug', null, [
                'label' => 'Slug',
                'help' => 'Laissez vide pour le générer automatiquement, utilisé dans les URL.',
                'required' => false,
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('description', null, [
                'label' => 'Description',
                'label_attr' => ['class' => 'label'],
                'help' => 'Description sommaire du jeu.',
                'attr' => ['class' => 'textarea',
                           'rows' => 6],
                'required' => false,
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('ageRecommendation', ChoiceType::class, [
                'label' => 'Âge recommandé',
                'help' => 'Indiquez la tranche d\'âge recommandée. Laissez vide si l\'âge n\'est pas déterminant.',
                'choices' => AgeRange::cases(),
                'choice_label' => fn(AgeRange $choice) => $choice->label(),
                'required' => false,
                'placeholder' => 'Aucune recommandation',
                'label_attr' => ['class' => 'label'],
                'row_attr' => ['class' => 'field'],
                'attr' => ['class' => 'input'],
                'help_attr' => ['class' => 'help'],
            ])

            ->add('picture', FileType::class, [
                'label' => 'Illustration',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'file-input'],
                'help' => 'Fichier JPEG, PNG ou webP, bannière horizontale, sera découpée et redimensionnée en 600×200',
                'required' => false,
                'mapped' => false,
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('genre', EntityType::class, [
                'label' => 'Genres',
                'attr' => ['class' => 'checkboxes'],
                'class' => Genre::class,
                'choice_label' => 'genre',
                'expanded' => true,
                'multiple' => true,
                'label_attr' => ['class' => 'label'],
                'choice_attr' => ['class' => 'checkbox'],
                'help_attr' => ['class' => 'help'],
            ])
            
            ->add('isInAssoLibrary', null, [
                'label' => 'Dans la ludothèque de l\'association ?',
                'label_attr' => ['class' => 'checkbox'],
                'help' => 'Cochez si ce jeu est disponible dans la ludothèque de l\'association.',
                'attr' => ['class' => 'checkbox'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('isPhysical', null, [
                'label' => 'Version papier ?',
                'label_attr' => ['class' => 'checkbox'],
                'help' => 'Cochez si ce jeu est disponible en version physique dans la ludothèque de l\'association.',
                'attr' => ['class' => 'checkbox'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('isNumerical', null, [
                'label' => 'Version numérique ?',
                'label_attr' => ['class' => 'checkbox'],
                'help' => 'Cochez si ce jeu est disponible en version numérique (PDF, epub...) dans la ludothèque de l\'association.',
                'attr' => ['class' => 'checkbox'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('urlNumericalVersion', UrlType::class, [
                'label' => 'URL de la version numérique',
                'help' => 'Lien privé pour accéder à la version numérique (PDF, epub...).',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'required' => false,
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('addBy', EntityType::class, [
                'label' => 'Créateur de la fiche du jeu',
                'class' => User::class,
                'choice_label' => 'fullName',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'required' => true,
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],               
            ])
            ->add('addDatetime', null, [
                'label' => 'Date de création de la fiche',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'view_timezone' => $_ENV['APP_TZ'],
                'required' => true,
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('isValidByAdmin', null, [
                'label' => 'Fiche validée par un administrateur ?',
                'label_attr' => ['class' => 'checkbox'],
                'attr' => ['class' => 'checkbox'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
                'help' => 'Les jeux qui ne sont pas validés ne peuvent pas être utilisés pour créer une partie.'
            ])
            ->add('validDatetime', null, [
                'label' => 'Date de validation de la fiche',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'view_timezone' => $_ENV['APP_TZ'],
                'required' => false,
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ]);

    }

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