<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\DBAL\Types\Types;

use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity(repositoryClass: UserRepository::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private int $id;

    #[ORM\Column(length: 255)]
    private ?string $pseudo;

    #[ORM\Column(type: 'string', length: 180, unique: true)]
    private ?string $email;

    #[ORM\Column(type: 'json')]
    private array $roles = [];

    #[ORM\Column(type: 'string')]
    private string $password;

    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    private ?\DateTimeInterface $lastLogin = null;

    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    private ?\DateTimeInterface $dateAdd = null;

    #[ORM\Column(length: 5, options:["default"=>"fr"])]
    private ?string $translator = null;

    #[ORM\Column(type: Types::SMALLINT)]
    private ?int $TypeUser = null;

    /**
     * @var Collection<int, Investigation>
     */
    #[ORM\OneToMany(targetEntity: Investigation::class, mappedBy: 'create_by')]
    private Collection $investigations;

    /**
     * @var Collection<int, Broadcast>
     */
    #[ORM\OneToMany(targetEntity: Broadcast::class, mappedBy: 'create_by')]
    private Collection $broadcasts;

    
    public function __construct()
    {
        $this->dateAdd = new \DateTime();
        $this->lastLogin = new \DateTime();
        $this->translator = "fr";
        $this->roles = [];
        $this->investigations = new ArrayCollection();
        $this->broadcasts = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getPseudo(): ?string
    {
        return $this->pseudo;
    }

    public function setPseudo(string $pseudo): self
    {
        $this->pseudo = $pseudo;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    /**
     * The public representation of the user (e.g. a username, an email address, etc.)
     *
     * @see UserInterface
     */
    public function getUserIdentifier(): string
    {
        return (string) $this->email;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    public function hasRole(string $role): ?bool
    {
        $Indice = array_search($role, $this->roles);
        if(is_numeric($Indice)){
            return true;
        }else{
            return false;
        }
    }

    public function addRole(string $role): ?bool
    {
        if(!$this->hasRole($role)){
            $this->roles[] = $role;
            return true;
        }
        return false;
    }

    public function removeRole(string $role)
    {
        $Indice = array_search($role, $this->roles);
        if(is_numeric($Indice)){
            unset($this->roles[$Indice]);
        }
    }

    /**
     * @see PasswordAuthenticatedUserInterface
     */
    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials(): void
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

    public function getLastLogin(): ?\DateTimeInterface
    {
        return $this->lastLogin;
    }

    public function getLastLoginFormat($format): string
    {
        return $this->lastLogin->format($format);
    }

    public function setLastLogin(\DateTimeInterface $lastLogin): self
    {
        $this->lastLogin = $lastLogin;

        return $this;
    }

    public function getDateAdd(): ?\DateTimeInterface
    {
        return $this->dateAdd;
    }

    public function getDateAddText($format): ?string
    {
        return $this->dateAdd->format($format);
    }

    public function setDateAdd(\DateTimeInterface $dateAdd): self
    {
        $this->dateAdd = $dateAdd;

        return $this;
    }

    public function getTranslator(): ?string
    {
        return $this->translator;
    }

    public function setTranslator(?string $translator): static
    {
        if($translator){
            $this->translator = $translator;
        }
        return $this;
    }

    public function getTypeUser(): ?int
    {
        return $this->TypeUser;
    }

    public function setTypeUser(int $TypeUser): static
    {
        $this->TypeUser = $TypeUser;

        return $this;
    }

    /**
     * @return Collection<int, Investigation>
     */
    public function getInvestigations(): Collection
    {
        return $this->investigations;
    }

    public function addInvestigation(Investigation $investigation): static
    {
        if (!$this->investigations->contains($investigation)) {
            $this->investigations->add($investigation);
            $investigation->setCreateBy($this);
        }

        return $this;
    }

    public function removeInvestigation(Investigation $investigation): static
    {
        if ($this->investigations->removeElement($investigation)) {
            // set the owning side to null (unless already changed)
            if ($investigation->getCreateBy() === $this) {
                $investigation->setCreateBy(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Broadcast>
     */
    public function getBroadcasts(): Collection
    {
        return $this->broadcasts;
    }

    public function addBroadcast(Broadcast $broadcast): static
    {
        if (!$this->broadcasts->contains($broadcast)) {
            $this->broadcasts->add($broadcast);
            $broadcast->setCreateBy($this);
        }

        return $this;
    }

    public function removeBroadcast(Broadcast $broadcast): static
    {
        if ($this->broadcasts->removeElement($broadcast)) {
            // set the owning side to null (unless already changed)
            if ($broadcast->getCreateBy() === $this) {
                $broadcast->setCreateBy(null);
            }
        }

        return $this;
    }

    
}