<?php

namespace App\Entity;

use App\Repository\AppareilRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity(repositoryClass=AppareilRepository::class)
 * @ORM\Table(schema="sav")
 */
class Appareil
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"api", "site_list"})
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity=TypeAppareil::class, inversedBy="appareils")
     * @ORM\JoinColumn(nullable=false, name="idtype_appareil")
     * @Groups({"api"})
     */
    private $type_appareil;

    /**
     * @SerializedName("type_appareil")
     * @Groups({"site_list"})
     */
    public function getTypeAppareilId() {
        return $this->getTypeAppareil()->getId();
    }

    /**
     * @ORM\Column(type="text", nullable=true)
     * @Groups({"site_list"})
     */
    private $commentaire;

    /**
     * @ORM\Column(type="string", length=32, nullable=true)
     * @Groups({"api", "site_list"})
     */
    private $marque;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     * @Groups({"api", "site_list"})
     */
    private $modele;

    /**
     * @ORM\Column(type="boolean", nullable=true, options={"default":1})
     * @Groups({"api", "site_list"})
     */
    private $metrologique;

    /**
     * @SerializedName("typeMetrologique")
     * @Groups({"site_list"})
     */
    public function getTypeMetrologique() {
        return ($this->metrologique == true) ? 'Métrologique' : (($this->metrologique === false) ? 'Hors métrologique' : 'Autre');
    }

    /**
     * @ORM\Column(type="boolean",  options={"default":true})
     * @Groups({"site_list"})
     */
    private $actif = true;

    /**
     * @ORM\ManyToOne(targetEntity=Site::class, inversedBy="appareils")
     * @ORM\JoinColumn(nullable=false, name="idsite")
     * @Ignore()
     */
    private $site;

    /**
     * @ORM\OneToMany(targetEntity=AgentAppareil::class, mappedBy="appareil", orphanRemoval=true)
     * @Ignore()
     */
    private $agentAppareils;

    /**
     * @ORM\OneToMany(targetEntity=TypePlanificationTypeAppareil::class, mappedBy="appareil", orphanRemoval=true)
     * @Ignore()
     */
    private $typePlanificationTypeAppareils;

    public function __construct()
    {
        $this->agentAppareils = new ArrayCollection();
        $this->typePlanificationTypeAppareils = new ArrayCollection();
    }

    /**
     * @SerializedName("site")
     * @Groups({"site_list"})
     */
    public function getSiteId() {
        return $this->getSite()->getId();
    }

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

    public function getCommentaire(): ?string
    {
        return $this->commentaire;
    }

    public function setCommentaire(?string $commentaire): self
    {
        $this->commentaire = $commentaire;

        return $this;
    }

    public function getMarque(): ?string
    {
        return $this->marque;
    }

    public function setMarque(?string $marque): self
    {
        $this->marque = $marque;

        return $this;
    }

    public function getModele(): ?string
    {
        return $this->modele;
    }

    public function setModele(?string $modele): self
    {
        $this->modele = $modele;

        return $this;
    }

    public function getMetrologique(): ?bool
    {
        return $this->metrologique;
    }

    public function setMetrologique(?bool $metrologique): self
    {
        $this->metrologique = $metrologique;

        return $this;
    }

    public function getActif(): ?bool
    {
        return $this->actif;
    }

    public function setActif(?bool $actif): self
    {
        $this->actif = $actif;

        return $this;
    }

    public function getSite(): ?Site
    {
        return $this->site;
    }

    public function setSite(?Site $site): self
    {
        $this->site = $site;

        return $this;
    }

    public function setTypeMetrologique($metrologique) {
        if ($metrologique == 'Métrologique') $this->setMetrologique(true);
        else if ($metrologique == 'Autre') $this->setMetrologique(null);
        else $this->setMetrologique(false);
    }

    /**
     * @return Collection|AgentAppareil[]
     */
    public function getAgentAppareils(): Collection
    {
        return $this->agentAppareils;
    }

    public function addAgentAppareil(AgentAppareil $agentAppareil): self
    {
        if (!$this->agentAppareils->contains($agentAppareil)) {
            $this->agentAppareils[] = $agentAppareil;
            $agentAppareil->setAppareil($this);
        }

        return $this;
    }

    public function removeAgentAppareil(AgentAppareil $agentAppareil): self
    {
        if ($this->agentAppareils->removeElement($agentAppareil)) {
            // set the owning side to null (unless already changed)
            if ($agentAppareil->getAppareil() === $this) {
                $agentAppareil->setAppareil(null);
            }
        }

        return $this;
    }

    /**
     * @SerializedName("libelle_long")
     * @Groups({"site_list"})
     */
    public function getLibelleAppareilAgent() {
        return $this->type_appareil->getLibelle() . ' - ' . $this->getModele() . ' - ' . $this->getMarque();
    }

    /**
     * @SerializedName("libelle_type")
     * @Groups({"site_list"})
     */
    public function getlibelleTypeAppareil()
    {
        return ($this->getTypeAppareil()) ? $this->getTypeAppareil()->getLibelle() : '';
    }

    public function getTypeAppareil() {
        return $this->type_appareil;
    }

    public function setTypeAppareil(?TypeAppareil $type_appareil): self
    {
        $this->type_appareil = $type_appareil;

        return $this;
    }

    /**
     * @return Collection<int, TypePlanificationTypeAppareil>
     */
    public function getTypePlanificationTypeAppareils(): Collection
    {
        return $this->typePlanificationTypeAppareils;
    }

    public function addTypePlanificationTypeAppareil(TypePlanificationTypeAppareil $typePlanificationTypeAppareil): self
    {
        if (!$this->typePlanificationTypeAppareils->contains($typePlanificationTypeAppareil)) {
            $this->typePlanificationTypeAppareils[] = $typePlanificationTypeAppareil;
            $typePlanificationTypeAppareil->setTypeAppareil($this);
        }

        return $this;
    }

    public function removeTypePlanificationTypeAppareil(TypePlanificationTypeAppareil $typePlanificationTypeAppareil): self
    {
        if ($this->typePlanificationTypeAppareils->removeElement($typePlanificationTypeAppareil)) {
            // set the owning side to null (unless already changed)
            if ($typePlanificationTypeAppareil->getTypeAppareil() === $this) {
                $typePlanificationTypeAppareil->setTypeAppareil(null);
            }
        }

        return $this;
    }
}
