<?php

declare(strict_types=1);

namespace App\Game\Form;

use App\Game\Entity\Game;
use App\Game\Enum\GameStatus;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Positive;

class AdminGameType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'constraints' => [new NotBlank()],
            ])
            ->add('numberOfPlayers', IntegerType::class, [
                'label' => 'Number of players',
                'constraints' => [new NotBlank(), new Positive()],
            ])
            ->add('status', ChoiceType::class, [
                'choices' => [
                    'In development' => GameStatus::IN_DEVELOPMENT,
                    'Locked' => GameStatus::LOCKED,
                    'Open' => GameStatus::OPEN,
                ],
            ])
            ->add('totalTime', IntegerType::class, [
                'mapped' => false,
                'required' => false,
                'label' => 'Total time (seconds)',
                'data' => $options['total_time'],
                'attr' => ['placeholder' => 'e.g. 3600 for 1 hour'],
            ]);
    }

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

        $resolver->setAllowedTypes('total_time', ['null', 'string', 'int']);
    }
}
