<?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\CardRepository")
 */
class Card
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

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

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\UserCard", mappedBy="card")
     */
    private $userCards;

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

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

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

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

        return $this;
    }

    public function getIcon(): ?string
    {
        return $this->icon;
    }

    public function setIcon(string $icon): self
    {
        $this->icon = $icon;

        return $this;
    }

    /**
     * @return Collection|UserCard[]
     */
    public function getUserCards(): Collection
    {
        return $this->userCards;
    }

    public function addUserCard(UserCard $userCard): self
    {
        if (!$this->userCards->contains($userCard)) {
            $this->userCards[] = $userCard;
            $userCard->setCard($this);
        }

        return $this;
    }

    public function removeUserCard(UserCard $userCard): self
    {
        if ($this->userCards->contains($userCard)) {
            $this->userCards->removeElement($userCard);
            // set the owning side to null (unless already changed)
            if ($userCard->getCard() === $this) {
                $userCard->setCard(null);
            }
        }

        return $this;
    }

    public function getJSON() {
        return array(
            'id' => $this->id,
            'title' => $this->title,
            'description' => $this->description,
            'icon' => $_ENV['ASSET_BASE_URL']."/".$_ENV['ASSET_CARD_FOLDER']."/".$this->icon,
        );
    }
}
