<?php

namespace App\Form\Frontend;

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\IntegerType;
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' => 'Mot de passe'],
                'second_options' => ['label' => 'Confirmation mot de passe'],
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please enter a password',
                    ]),
                    new Length([
                        'min' => 6,
                        'minMessage' => 'Your password should be at least {{ limit }} characters',
                        // max length allowed by Symfony for security reasons
                        'max' => 4096,
                    ]),
                ],
            ])
            ->add('firstname', TextType::class, array(
                'label' => 'Prénom',
                'required' => true
            ))
            ->add('lastname', TextType::class, array(
                'label' => 'Nom',
                'required' => true
            ))
            ->add('birthdate', BirthdayType::class, array(
                'label' => 'Date de naissance',
                'format' => 'dd/MM/yyyy',
                'widget' => 'single_text',
                'html5' => false,
                'attr' => [
                    'class'=> 'datepicker form-control unicase-form-control',
                ]
            ))
            ->add('phoneNumber', TextType::class, array(
                'label' => 'Numéro Téléphone',
                'required' => true
            ))
            ->add('gender', ChoiceType::class, array(
                'label' => 'Gender',
                'choices' => ['Femme' => 0, 'Homme' => 1 ],
                'required' => true
            ))
            ->add('pseudo')

            ->add('country', TextType::class, array(
                'label' => 'Pays',
                'required' => true
            ))
            
            ->add('zip', IntegerType::class, array(
                'label' => 'Code Postal',
                'required' => true
            ))
            
            ->add('adresse', TextType::class, array(
                'label' => 'Adresse',
                'required' => true
            ))

            ->add('newsletter', CheckboxType::class, array(
                'mapped' => false,
                'required' => false,
                'label' => 'Je m’inscris à la newsletter et accepte de recevoir des communications de la part TNpromo',
            ))
            ->add('cgu', CheckboxType::class, array(
                'mapped' => false,
                'constraints' => [
                    new IsTrue([
                        'message' => 'Merci d’accepter les conditions générales d’utilisation en cochant la case ci-dessous'
                    ])
                ],
            ))
            ->add("save", SubmitType::class, ['attr'=>['class'=>'save'], ])
        ;
    }

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