<?php

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Lc\SovBundle\Form\User;

use Lc\SovBundle\Doctrine\EntityManager;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Component\Validator\Constraints\NotBlank;

class ChangePasswordFormType extends AbstractType
{

    protected $em;
    protected $translatorAdmin;

    public function __construct(EntityManager $em, TranslatorAdmin $translatorAdmin)
    {
        $this->em = $em;
        $this->translatorAdmin = $translatorAdmin;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $constraintsOptions = [
                'message' => 'Mot de passe invalide',
        ];

        if (!empty($options['validation_groups'])) {
            $constraintsOptions['groups'] = [reset($options['validation_groups'])];
        }

        $builder->add(
                'current_password',
                PasswordType::class,
                [
                        'label' => 'form.account_password.field.current_password',
                        'translation_domain' => 'admin',
                        'mapped' => false,
                        'constraints' => [
                                new NotBlank(),
                                new UserPassword($constraintsOptions),
                        ],
                ]
        );

        $builder->add(
                'plain_password',
                RepeatedType::class,
                [
                        'type' => PasswordType::class,
                        'mapped' => false,
                        'first_options' => ['label' => 'form.account_password.field.new_password'],
                        'second_options' => ['label' => 'form.account_password.field.new_password_repeat'],
                        'invalid_message' => $this->translatorAdmin->trans('form.account_password.message.invalid_passwords'),
                        'translation_domain' => 'admin',
                ]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
                [
                        'data_class' => $this->em->getEntityName(UserInterface::class),
                ]
        );
    }
}
