<?php

namespace App\Entity;

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

#[ORM\Entity(repositoryClass: PositionRepository::class)]
class Position
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

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

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

    #[ORM\Column(type: Types::SMALLINT, nullable: true)]
    private ?int $ligne = null;


    #[ORM\Column(type: Types::SMALLINT, nullable: true)]
    private ?int $priorite = null;

    #[ORM\ManyToOne(inversedBy: 'positions')]
    private ?Annonce $annonce = null;

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

    #[ORM\ManyToOne(inversedBy: 'positions')]
    #[ORM\JoinColumn(nullable: false)]
    private ?User $create_by = null;

    #[ORM\OneToMany(mappedBy: 'position', targetEntity: Affichage::class)]
    private Collection $affichages;


    public function __construct()
    {
        $this->affichages = new ArrayCollection();
        $this->date_add = new \DateTime();
    }

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

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

    public function setDateDebut(\DateTimeInterface $date_debut): static
    {
        $this->date_debut = $date_debut;

        return $this;
    }

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

    public function setDateFin(\DateTimeInterface $date_fin): static
    {
        $this->date_fin = $date_fin;

        return $this;
    }


    public function getLigne(): ?int
    {
        return $this->ligne;
    }

    public function setLigne(?int $ligne): static
    {
        $this->ligne = $ligne;

        return $this;
    }

    

    public function getPriorite(): ?int
    {
        return $this->priorite;
    }

    public function setPriorite(?int $priorite): static
    {
        $this->priorite = $priorite;

        return $this;
    }

    public function getAnnonce(): ?Annonce
    {
        return $this->annonce;
    }

    public function setAnnonce(?Annonce $annonce): static
    {
        $this->annonce = $annonce;

        return $this;
    }

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

    public function setDateAdd(\DateTimeInterface $date_add): static
    {
        $this->date_add = $date_add;

        return $this;
    }

    public function getCreateBy(): ?User
    {
        return $this->create_by;
    }

    public function setCreateBy(?User $create_by): static
    {
        $this->create_by = $create_by;

        return $this;
    }

    /**
     * @return Collection<int, Affichage>
     */
    public function getAffichages(): Collection
    {
        return $this->affichages;
    }

    public function addAffichage(Affichage $affichage): static
    {
        if (!$this->affichages->contains($affichage)) {
            $this->affichages->add($affichage);
            $affichage->setPosition($this);
        }

        return $this;
    }

    public function removeAffichage(Affichage $affichage): static
    {
        if ($this->affichages->removeElement($affichage)) {
            // set the owning side to null (unless already changed)
            if ($affichage->getPosition() === $this) {
                $affichage->setPosition(null);
            }
        }

        return $this;
    }

    public function getTotalPrix(): ?int
    {
        $prix = 0;
        foreach($this->affichages as $affichage){
            $prix += $affichage->getPrix();
        }
        return $prix;
    }


    public function getLigneRange()
    {
        switch ($this->ligne):
            case 1:
                return "A";
            case 2:
                return "B";
            case 3:
                return "C";  
            case 4:
                return "D";
            case 5:
                return "E";
            case 6:
                return "F";
            case 7:
                return "G";
            case 8:
                return "H";
            case 9:
                return "I";
            case 10:
                return "J";
            case 11:
                return "K";
            case 12:
                return "L";   
            case 13:
                return "M"; 
        endswitch;
    }

}
