<?php

namespace App\DataFixtures;

use App\Entity\Baptism;
use DateTimeImmutable;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Exception;
use Faker;
use HttpException;

class BaptismFixtures extends Fixture implements DependentFixtureInterface
{
    /**
     * @var ObjectManager
     **/
    private $manager;

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

    /**
     * @throws HttpException
     */
    public function load(ObjectManager $manager): void
    {
        $this->manager=$manager;

        //https://github.com/fzaninotto/Faker#fakerprovideren_usperson
        $this->faker= Faker\Factory::create('fr_FR');

        $this->generateBaptism(2);


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


    public function getDependencies(): array
    {
        return [
            PadreFixtures::class,
            ParentsFixtures::class
        ];
    }

    /**
     * @throws HttpException
     */
    private function generateBaptism(int $number) :void {

        for ( $i=1; $i<=$number; $i ++)
        {
            $baptism = new Baptism();
            $parents = $this->getReference('parents'.mt_rand(1,2));
            $padre = $this->getReference('padre'.mt_rand(1,3));
            $lastname = $parents->getMother_lastname();

            [
                'dateObject'    => $dateObject,
                'dateString'    => $dateString
            ]=$this->generateRandomDateBetweenRange('01/01/2021','04/10/2021');

            if (rand(0,1) ==0)
                $baptism->setFirstname($this->faker->firstNameFemale);
            else
                $baptism->setFirstname($this->faker->firstNameMale);
            $baptism->setLastname($lastname);
            $baptism->setComments($this->faker->paragraph());
            $baptism->setCreatedAt($dateObject);
            $baptism->setParents($parents);
            $baptism->setPadre($padre);

            $this->manager->persist($baptism);
            $padre->addBaptism($baptism);
            $parents->addChild($baptism);
        }
    }

    /**
     * Generate Random DateTime Between two Dates
     * @param string $start start Date as dd/mm/Y
     * @param string $end end Date as dd/mm/Y
     * @return array{ dateObject:DateTimeImmutable,dateString:string}
     * @throws HttpException
     */
    private function generateRandomDateBetweenRange(string $start, string $end) : array {


        $startDate=(\DateTime::createFromFormat('d/m/Y',$start));
        $endDate=(\DateTime::createFromFormat('d/m/Y',$start));

        if(!$startDate || !$endDate)
            throw new HttpException('The dates must be in the dd/m/Y format');

        $startDateTimestamp=$startDate->getTimestamp();
        $endDateTimestamp=$endDate->getTimestamp();
        $randomTimeStamp=mt_rand( $startDateTimestamp,$endDateTimestamp);
        $dateTimeImmutable = (new DateTimeImmutable())->setTimestamp($randomTimeStamp);

        return
            [
                "dateObject"=> $dateTimeImmutable,
                "dateString"=>$dateTimeImmutable->format('d-m-Y')
            ];
    }

}