<?php

namespace App\Form;

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;

use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;

class UserRegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('email', EmailType::class, array(
                'label' => 'Email'
            ))
            ->add('password', RepeatedType::class, [
                'type' => PasswordType::class,
                'invalid_message' => 'The password fields must match.',
                'options' => ['attr' => ['class' => 'password-field']],
                'required' => true,
                'first_options'  => ['label' => 'password_first_options'],
                'second_options' => ['label' => 'password_second_options'],
                'constraints' => [
                    new NotBlank([
                        'message' => 'password_clt_empty',
                    ]),
                    new Length([
                        'min' => 6,
                        'minMessage' => 'password_clt_min_length',
                        'max' => 4096,
                    ]),
                ],
            ])

            ->add('birthdate', BirthdayType::class, array(
                'label' => 'birthdate',
                'format' => 'dd/MM/yyyy',
                'widget' => 'single_text',
                'html5' => false,
                'attr' => [
                    'class' => 'datepicker form-control unicase-form-control',
                ]
            ))
            ->add('phoneNumber', TextType::class, array(
                'label' => 'phoneNumber',
                'required' => true
            ))
            ->add('gender', ChoiceType::class, array(
                'label' => 'gender',
                'choices' => ['Femme' => 0, 'Homme' => 1],
                'required' => true
            ))
            ->add('pseudo', TextType::class, array(
                'label' => 'pseudo',
                'required' => true
            ))

            ->add('country', TextType::class, array(
                'label' => 'country',
                'required' => true
            ))

            ->add('cgu', CheckboxType::class, array(
                'mapped' => false,
                'label' => "label_cgu",
                'constraints' => [
                    new IsTrue([
                        'message' => 'cgu_clt'
                    ])
                ],
            ))
            ->add("save", SubmitType::class, [
                'attr' => ['class' => 'btn btn-primary'],
                'label' => 'btn_save'
            ])
        ;
    }

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