<?php

namespace App\Entity;

use App\Repository\ModpackRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

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

    #[ORM\Column(length: 50, unique: true, nullable: false)]
    private ?string $version = null;

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

    #[ORM\Column]
    private bool $downloadable = false;


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

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

    public function getVersion(): ?string
    {
        return $this->version;
    }

    public function setVersion(string $version): static
    {
        $this->version = $version;

        return $this;
    }

    /**
     * @return Collection<int, Constitue>
     */
    public function getConstitues(): Collection
    {
        return $this->constitues;
    }

    public function addConstitue(Constitue $constitue): static
    {
        if (!$this->constitues->contains($constitue)) {
            $this->constitues->add($constitue);
            $constitue->setModpack($this);
        }

        return $this;
    }

    public function removeConstitue(Constitue $constitue): static
    {
        if ($this->constitues->removeElement($constitue)) {
            // set the owning side to null (unless already changed)
            if ($constitue->getModpack() === $this) {
                $constitue->setModpack(null);
            }
        }

        return $this;
    }

    public function getConstituteByMod(Mod $mod): ?Constitue
    {
        foreach ($this->constitues as $constitue) {
            if ($constitue->getMod() === $mod) {
                return $constitue;
            }
        }

        return null;
    }

    public function getMods(): Collection
    {
        $mods = new ArrayCollection();
        foreach ($this->constitues as $constitue) {
            $mods->add($constitue->getMod());
        }

        return $mods;
    }

    public function isDownloadable(): bool
    {
        return $this->downloadable;
    }

    public function setDownloadable(bool $downloadable): static
    {
        $this->downloadable = $downloadable;

        return $this;
    }

    public function numberOfMods(): int
    {
        return count($this->constitues);
    }
}
