<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\MinistryRepository")
 */
class Ministry
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=16)
     */
    private $code;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="json")
     */
    private $years = [];

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Programm", mappedBy="ministry", orphanRemoval=true, fetch="EAGER")
     */
    private $programms;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Direction", mappedBy="ministry", orphanRemoval=true)
     */
    private $directions;

    /**
     * @ORM\Column(type="string", length=16)
     */
    private $mtype;

    /**
     * @ORM\Column(type="string", length=16)
     */
    private $grouping;

    /**
     * @ORM\Column(type="string", length=8)
     */
    private $color;

    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $poste;

    /**
     * @ORM\OneToMany(targetEntity=Indicator::class, mappedBy="ministere")
     */
    private $indicators;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $description;

    /**
     * @ORM\ManyToMany(targetEntity=Costing::class, mappedBy="ministries")
     */
    private $costings;

    public function __construct()
    {
        $this->programms = new ArrayCollection();
        $this->directions = new ArrayCollection();
        $this->indicators = new ArrayCollection();
        $this->costings = new ArrayCollection();
    }

    public function __toString()
    {
        return $this->getName();
    }

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

    public function getCode(): ?string
    {
        return $this->code;
    }

    public function setCode(string $code): self
    {
        $this->code = $code;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getYears(): array
    {
        $years = $this->years;
        
        return array_unique($years);
    }

    public function setYears(array $years): self
    {
        $this->years = $years;

        return $this;
    }

    public function hasYear($year)
    {
        return in_array($year, $this->getYears(), true);
    }

    public function addYear($year)
    {
        if (!in_array($year, $this->years, true)) {
            $this->years[] = $year;
        }

        return $this;
    }

    public function removeYear($year)
    {
        if (false !== $key = array_search($year, $this->years, true)) {
            unset($this->years[$key]);
            $this->years = array_values($this->years);
        }

        return $this;
    }

    /**
     * @return Collection|Programm[]
     */
    public function getProgramms(): Collection
    {
        return $this->programms;
    }

    public function addProgramm(Programm $programm): self
    {
        if (!$this->programms->contains($programm)) {
            $this->programms[] = $programm;
            $programm->setMinistry($this);
        }

        return $this;
    }

    public function removeProgramm(Programm $programm): self
    {
        if ($this->programms->contains($programm)) {
            $this->programms->removeElement($programm);
            // set the owning side to null (unless already changed)
            if ($programm->getMinistry() === $this) {
                $programm->setMinistry(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|Direction[]
     */
    public function getDirections(): Collection
    {
        return $this->directions;
    }

    public function addDirection(Direction $direction): self
    {
        if (!$this->directions->contains($direction)) {
            $this->directions[] = $direction;
            $direction->setMinistry($this);
        }

        return $this;
    }

    public function removeDirection(Direction $direction): self
    {
        if ($this->directions->contains($direction)) {
            $this->directions->removeElement($direction);
            // set the owning side to null (unless already changed)
            if ($direction->getMinistry() === $this) {
                $direction->setMinistry(null);
            }
        }

        return $this;
    }

    public function getMtype(): ?string
    {
        return $this->mtype;
    }

    public function setMtype(string $mtype): self
    {
        $this->mtype = $mtype;

        return $this;
    }

    public function getGrouping(): ?string
    {
        return $this->grouping;
    }

    public function setGrouping(?string $grouping): self
    {
        $this->grouping = $grouping;

        return $this;
    }

    public function getColor(): ?string
    {
        return $this->color;
    }

    public function setColor(string $color): self
    {
        $this->color = $color;

        return $this;
    }

    public function getPoste(): ?bool
    {
        return $this->poste;
    }

    public function setPoste(?bool $poste): self
    {
        $this->poste = $poste;

        return $this;
    }

    /**
     * @return Collection|Indicator[]
     */
    public function getIndicators(): Collection
    {
        return $this->indicators;
    }

    public function addIndicator(Indicator $indicator): self
    {
        if (!$this->indicators->contains($indicator)) {
            $this->indicators[] = $indicator;
            $indicator->setMinistere($this);
        }

        return $this;
    }

    public function removeIndicator(Indicator $indicator): self
    {
        if ($this->indicators->removeElement($indicator)) {
            // set the owning side to null (unless already changed)
            if ($indicator->getMinistere() === $this) {
                $indicator->setMinistere(null);
            }
        }

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;

        return $this;
    }

    /**
     * @return Collection<int, Costing>
     */
    public function getCostings(): Collection
    {
        return $this->costings;
    }

    public function addCosting(Costing $costing): self
    {
        if (!$this->costings->contains($costing)) {
            $this->costings[] = $costing;
            $costing->addMinistry($this);
        }

        return $this;
    }

    public function removeCosting(Costing $costing): self
    {
        if ($this->costings->removeElement($costing)) {
            $costing->removeMinistry($this);
        }

        return $this;
    }

}
