<?php

namespace App\Form\Frontend;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;

use App\Entity\User;

class UserUpdatePasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
                ->add('password', PasswordType::class, array(
                    'constraints' => [
                        new \Symfony\Component\Security\Core\Validator\Constraints\UserPassword(),
                    ],
                    'label' => 'Mot de passe actuel',
                    'required' => true,
                ))
                ->add('new_password', PasswordType::class, [
                    'label' => 'Nouveau mot de passe *',
                    'required' => true,
                    'mapped' => false,
                    'constraints' =>[
                        new Assert\Regex(['pattern'=> "/^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z]).{8,200}$/",
                                        'message' => 'Mot de passe très faible']),
                    ],
                    
                ])
                ->add('confirm_password', PasswordType::class, [
                    'required' => true,
                    'mapped' => false,
                    'label' => 'Confirmation du nouveau mot de passe *',
                    'constraints' => [
                        new Assert\EqualTo(['propertyPath'=>'parent.all[new_password].data',
                                            'message' => 'Les 2 mots de passe ne sont pas identiques.'])
                    ],
                ])
                ->add('submit', SubmitType::class, array(
                    'label' => 'Envoyer'
                ))
        ;
    }

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