<?php

namespace App\DataFixtures;

use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Faker;

class UserFixtures extends Fixture
{
    /**
     * @var ObjectManager
     **/
    private $manager;

    /**
     * @var Faker\Generator
     **/
    private $faker;

    /**
     * @var UserPasswordHasherInterface
     **/
    private $passwordEncoder;


    /**
     * @param ObjectManager $manager
     * @param UserPasswordHasherInterface $passwordEncoder
     */
    public function __construct(ObjectManager $manager, UserPasswordHasherInterface $passwordEncoder)
    {
        $this->manager = $manager;
        $this->passwordEncoder = $passwordEncoder;
        $this->faker= Faker\Factory::create('fr_FR');
    }

    /**
     * @return string[]
     */
    public function getDependencies() : array
    {
        return [
            ParentsFixtures::class
        ];
    }

    public function load(ObjectManager $manager): void
    {

        $this->generateUser(4);

        $this->manager->flush();
    }

    private function generateUser(int $number) :void
    {
        $isVerified=[true,false,false,false,false];
        for ( $i=1; $i<=$number; $i ++) {
            $user= new User();
            $user   ->setEmail($this->faker->email)
                    ->setPassword($this->passwordEncoder->hashPassword($user,'000'))
                    ->setIsVerified($isVerified[$i]);
            if ($i < 3)
            {
                if ($this->getReference("parents{$i}")!=null)
                    $user->setPerson($this->getReference("parents{$i}"));
            }


            $this->manager->persist($user);

        }
        $user= new User();
        $user   ->setEmail("secretariat@paroissedemarines.com")
                ->setPassword($this->passwordEncoder->hashPassword($user,'000'))
                ->setIsVerified(1)
                ->setAccountMustBeVerifiedBefore(new \DateTimeImmutable());
        $this->manager->persist($user);
    }
}
