<?php 
 
namespace App\Controller; 
 
use App\Entity\LandingPage; 
use App\Entity\User; 
use App\Form\RegistrationFormType; 
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\Annotation\Route; 
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; 
 
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; 
use Doctrine\ORM\EntityManagerInterface; 
use Symfony\Contracts\Translation\TranslatorInterface; 
use Symfony\Component\Form\FormError; 
 
use Symfony\Component\Form\Extension\Core\Type\PasswordType; 
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; 
use Symfony\Component\Validator\Constraints\Length; 
use Symfony\Component\Form\Extension\Core\Type\EmailType; 
use Symfony\Component\Validator\Constraints\NotBlank; 
use App\Helper\Utils; 
 
/** 
 * 
 * @Route("{_locale}", requirements={"_locale": "en|fr"}) 
 */ 
 
class RegistrationController extends AbstractController 
{ 
    private function authenticateUser(User $user) 
    { 
        $providerKey = 'secured_area'; // your firewall name 
        $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles()); 
 
        $this->container->get('security.token_storage')->setToken($token); 
    } 
     
    /** 
     * @Route({ 
     *     "fr": "/inscription", 
     *     "en": "/register" 
     * }, name="register") 
     */ 
    public function register(EntityManagerInterface $em, TranslatorInterface $translator, Request $request, UserPasswordEncoderInterface $passwordEncoder): Response 
    { 
        if (!$platform = $request->getSession()->get('platform')) { 
            throw $this->createNotFoundException('this domain is not allowed'); 
        } 
        $user = $this->getUser(); 
        if ($user) { 
            return $this->redirectToRoute('login'); 
        } 
        $utils = new Utils; 
        $user = new User(); 
        $user->setPlatform($platform); 
        $form = $this->createForm(RegistrationFormType::class, $user); 
        $data = $request->request->get('registration_form'); 
        $form->handleRequest($request); 
        $googleResponse = false; 
        if ($form->isSubmitted()) { 
            $googleResponse = $utils->validCaptcha($form, $translator); 
            if ($this->getDoctrine()->getRepository(User::class)->findOneBy(['deleted' => false, 'pseudo' => $data['pseudo'], 'platform' => $platform])) { 
                $form['pseudo']->addError(new FormError($translator->trans('this_username_address_is_already_in_use'))); 
            } 
            if ($this->getDoctrine()->getRepository(User::class)->findOneBy(['deleted' => false, 'email' => $data['email'], 'platform' => $platform])) { 
                $form['email']->addError(new FormError($translator->trans('this_email_address_is_already_in_use'))); 
            } 
            $user->setUsername($platform->getId().'_'.$user->getEmail()); 
        } 
        if ($form->isSubmitted() && $form->isValid()) { 
            $user->setNewResetToken(); 
            $em->persist($user); 
            $em->flush(); 
            $transport = (new \Swift_SmtpTransport($platform->getSmtpServer(), $platform->getSmtpPort(), 'tls')) 
                ->setUsername($platform->getEmail()) 
                ->setPassword($platform->getEmailPassword()) 
            ; 
            $mailer = new \Swift_Mailer($transport); 
            $message = (new \Swift_Message($translator->trans('confirm_your_account'))) 
                ->setFrom($platform->getEmail(), $platform->getName()) 
                ->setTo($data['email']) 
                ->setBody( 
                    $this->renderView( 
                        'emails/confirmYourAccount.html.twig', [ 
                            'user' => $user, 
                        ] 
                    ), 
                    'text/html' 
                ) 
            ; 
            $mailer->send($message); 
            return $this->redirectToRoute('register_confirmation'); 
        } 
 
        return $this->render('registration/register.html.twig', [ 
            'registrationForm' => $form->createView(), 
            'showCaptchaError' => $form->isSubmitted()&&!$googleResponse?true:false, 
        ]); 
    } 
     
    /** 
     * @Route({ 
     *     "fr": "/confirmation-inscription", 
     *     "en": "/register-confirmation" 
     * }, name="register_confirmation") 
     */ 
    public function registerConfirmation(Request $request, EntityManagerInterface $em): Response 
    { 
        if (!$platform = $request->getSession()->get('platform')) { 
            throw $this->createNotFoundException('this domain is not allowed'); 
        } 
        $utils = new Utils; 
        $user = $this->getUser(); 
        if ($user) { 
            return $this->redirectToRoute('login'); 
        } 
        return $this->render('registration/registerConfirmation.html.twig'); 
    } 
     
    /** 
     * @Route({ 
     *     "fr": "/reinitialiser-mon-mot-de-passe", 
     *     "en": "/reset-my-password" 
     * }, name="forgot_password") 
     */ 
    public function forgotPassword(TranslatorInterface $translator, EntityManagerInterface $em, Request $request): Response 
    { 
        if (!$platform = $request->getSession()->get('platform')) { 
            throw $this->createNotFoundException('this domain is not allowed'); 
        } 
        $utils = new Utils; 
        $user = $this->getUser(); 
        if ($user) { 
            return $this->redirectToRoute('login'); 
        } 
        $data = $request->request->get('form'); 
        $form = $this->createFormBuilder() 
            ->add('email', EmailType::class, [ 
                'mapped' => false, 
                'required' => true, 
                'label' => 'email', 
                'constraints' => [new NotBlank()], 
                'attr' => [ 
                    'class' => 'text-center form-control-lg', 
                    'placeholder' => 'email', 
                ], 
            ]) 
        ->getForm(); 
        $form->handleRequest($request); 
        $googleResponse = false; 
        if ($form->isSubmitted()) { 
            $googleResponse = $utils->validCaptcha($form, $translator); 
            $user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['deleted' => false, 'enabled' => true, 'email' => $data['email']]); 
            if (!$user) { 
                $form['email']->addError(new FormError($translator->trans('this_email_address_cannot_be_found'))); 
            } 
        } 
        if ($form->isSubmitted() && $form->isValid()) { 
            $user->setNewResetToken(); 
            $em->persist($user); 
            $em->flush(); 
            $transport = (new \Swift_SmtpTransport($platform->getSmtpServer(), $platform->getSmtpPort(), 'tls')) 
                ->setUsername($platform->getEmail()) 
                ->setPassword($platform->getEmailPassword()) 
            ; 
            $mailer = new \Swift_Mailer($transport); 
            $message = (new \Swift_Message($translator->trans('reset_my_password'))) 
                ->setFrom($platform->getEmail(), $platform->getName()) 
                ->setTo($data['email']) 
                ->setBody( 
                    $this->renderView( 
                        'emails/forgotPassword.html.twig', [ 
                            'user' => $user, 
                        ] 
                    ), 
                    'text/html' 
                ) 
            ; 
            $mailer->send($message); 
            return $this->redirectToRoute('forgot_password_confirmation'); 
        } 
        // $masseurs = $this->getDoctrine()->getRepository(User::class)->getMasseurs(); 
        return $this->render('registration/forgotPassword.html.twig', [ 
            'form' => $form->createView(), 
            'showCaptchaError' => $form->isSubmitted()&&!$googleResponse?true:false, 
        ]); 
    } 
     
    /** 
     * @Route({ 
     *     "fr": "/mot-de-passe-oublie-confirmation", 
     *     "en": "/forgot-password-confirmation" 
     * }, name="forgot_password_confirmation") 
     */ 
    public function forgotPasswordConfirmation(Request $request, EntityManagerInterface $em): Response 
    { 
        if (!$platform = $request->getSession()->get('platform')) { 
            throw $this->createNotFoundException('this domain is not allowed'); 
        } 
        $utils = new Utils; 
        $user = $this->getUser(); 
        if ($user) { 
            return $this->redirectToRoute('home'); 
        } 
        return $this->render('registration/forgotPasswordConfirmation.html.twig'); 
    } 
     
    /** 
     * @Route({ 
     *     "fr": "/reinitialiser-mon-mot-de-passe/{email}/{resetToken}", 
     *     "en": "/reset-my-password/{email}/{resetToken}" 
     * }, name="reset_password") 
     */ 
    public function resetPassword(TranslatorInterface $translator, EntityManagerInterface $em, Request $request, UserPasswordEncoderInterface $passwordEncoder, User $newUser): Response 
    { 
        if (!$platform = $request->getSession()->get('platform')) { 
            throw $this->createNotFoundException('this domain is not allowed'); 
        } 
        $utils = new Utils; 
        $user = $this->getUser(); 
        if ($user) { 
            return $this->redirectToRoute('home'); 
        } 
        $user = $newUser; 
        $data = $request->request->get('form'); 
        $form = $this->createFormBuilder() 
            ->add('plainPassword', RepeatedType::class, [ 
                'type' => PasswordType::class, 
                'invalid_message' => $translator->trans('the_password_fields_must_match'), 
                'options' => ['attr' => ['class' => 'password-field']], 
                'required' => true, 
                'label' => 'password', 
                'mapped' => false, 
                'attr' => [ 
                    'class' => 'text-center', 
                ], 
                'first_options'  => [ 
                    'label' => 'new_password', 
                    'attr' => [ 
                        'class' => 'text-center form-control-lg', 
                        'placeholder' => 'new_password' 
                    ], 
                ], 
                'second_options' => [ 
                    'label' => 'new_repeat_password', 
                    'attr' => [ 
                        'class' => 'text-center form-control-lg', 
                        'placeholder' => 'new_repeat_password' 
                    ], 
                ], 
                'constraints' => [ 
                    new NotBlank([ 
                        'message' => $translator->trans('please_enter_your_password'), 
                    ]), 
                    new Length([ 
                        'min' => 6, 
                        'minMessage' => $translator->trans('password_min_characters_message', [ '%limit%' => 6 ]), 
                        // max length allowed by Symfony for security reasons 
                        'max' => 4096, 
                    ]), 
                ], 
            ]) 
        ->getForm(); 
        $form->handleRequest($request); 
        if ($form->isSubmitted() && $form->isValid()) { 
            $user->setPassword( 
                $passwordEncoder->encodePassword( 
                    $user, 
                    $form->get('plainPassword')->getData() 
                ) 
            ); 
            $user->setResetToken(null); 
            $user->setEnabled(true); 
            $em->persist($user); 
            $em->flush(); 
            $this->authenticateUser($user); 
            return $this->redirectToRoute('login'); 
        } 
        // $masseurs = $this->getDoctrine()->getRepository(User::class)->getMasseurs(); 
        return $this->render('registration/resetPassword.html.twig', [ 
            'form' => $form->createView(), 
        ]); 
    } 
     
    /** 
     * @Route({ 
     *     "fr": "/validez-votre-compte/{email}/{resetToken}", 
     *     "en": "/confirm-your-account/{email}/{resetToken}" 
     * }, name="confirm_account") 
     */ 
    public function confirmAccount(TranslatorInterface $translator, EntityManagerInterface $em, Request $request, UserPasswordEncoderInterface $passwordEncoder, User $newUser): Response 
    { 
        if (!$platform = $request->getSession()->get('platform')) { 
            throw $this->createNotFoundException('this domain is not allowed'); 
        } 
        $utils = new Utils; 
        $user = $this->getUser(); 
        if ($user) { 
            return $this->redirectToRoute('offers'); 
        } 
        $user = $newUser; 
        $data = $request->request->get('form'); 
        $form = $this->createFormBuilder() 
            ->add('plainPassword', RepeatedType::class, [ 
                'type' => PasswordType::class, 
                'invalid_message' => $translator->trans('the_password_fields_must_match'), 
                'options' => ['attr' => ['class' => 'password-field']], 
                'required' => true, 
                'label' => 'password', 
                'mapped' => false, 
                'attr' => [ 
                    'class' => 'text-center', 
                ], 
                'first_options'  => [ 
                    'label' => 'new_password', 
                    'attr' => [ 
                        'class' => 'text-center form-control-lg', 
                        'placeholder' => 'new_password' 
                    ], 
                ], 
                'second_options' => [ 
                    'label' => 'new_repeat_password', 
                    'attr' => [ 
                        'class' => 'text-center form-control-lg', 
                        'placeholder' => 'new_repeat_password' 
                    ], 
                ], 
                'constraints' => [ 
                    new NotBlank([ 
                        'message' => $translator->trans('please_enter_your_password'), 
                    ]), 
                    new Length([ 
                        'min' => 6, 
                        'minMessage' => $translator->trans('password_min_characters_message', [ '%limit%' => 6 ]), 
                        // max length allowed by Symfony for security reasons 
                        'max' => 4096, 
                    ]), 
                ], 
            ]) 
        ->getForm(); 
        $form->handleRequest($request); 
        if ($form->isSubmitted() && $form->isValid()) { 
            $user->setPassword( 
                $passwordEncoder->encodePassword( 
                    $user, 
                    $form->get('plainPassword')->getData() 
                ) 
            ); 
            $user->setResetToken(null); 
            $user->setEnabled(true); 
            $em->persist($user); 
            $em->flush(); 
            $this->authenticateUser($user); 
            return $this->redirectToRoute('login'); 
        } 
        // $masseurs = $this->getDoctrine()->getRepository(User::class)->getMasseurs(); 
        return $this->render('registration/confirmAccount.html.twig', [ 
            'form' => $form->createView(), 
        ]); 
    } 
}