<?php


namespace App\Entity;

use App\Service\JudgeManager;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JsonSerializable;

/**
 * @ORM\Entity
 * @ORM\Table(
 *     name="judges",
 *     indexes={
 *         @ORM\Index(name="j_name_idx", columns={"name"})
 *     }
 * )
 * @ORM\HasLifecycleCallbacks
 */
class Judge implements JsonSerializable
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @var int
     */
    private $id;

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

    /**
     * @ORM\Column(name="prefix", type="string", length=14, nullable=true)
     * @var string
     */
    private $prefix;

    /**
     * @ORM\Column(name="slug", type="string", length=250)
     * @Gedmo\Slug(fields={"name"})
     * @var string
     */
    private $slug;

    /**
     * @ORM\OneToMany(targetEntity="JudgeTitle", mappedBy="judge", cascade={"persist", "remove"}, orphanRemoval=true)
     * @var Collection|JudgeTitle[]
     */
    private $titles;

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

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

    /**
     * @param int $id
     * @return Judge
     */
    public function setId(?int $id): Judge
    {
        $this->id = $id;
        return $this;
    }

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

    /**
     * @param string $name
     * @return Judge
     */
    public function setName(string $name): Judge
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getPrefix(): ?string
    {
        return $this->prefix;
    }

    /**
     * @param string $prefix
     * @return Judge
     */
    public function setPrefix(?string $prefix): Judge
    {
        $this->prefix = $prefix;
        return $this;
    }

    /**
     * @return string
     */
    public function getSlug(): ?string
    {
        return $this->slug;
    }

    /**
     * @param string $slug
     * @return Judge
     */
    public function setSlug(string $slug): Judge
    {
        $this->slug = $slug;
        return $this;
    }

    /**
     * @return JudgeTitle[]|Collection
     */
    public function getTitles(): ?Collection
    {
        return $this->titles;
    }

    /**
     * @param \Traversable $titles
     * @return Judge
     */
    public function addTitles(\Traversable $titles): Judge
    {
        foreach ($titles as $title) {
            $this->addTitle($title);
        }
        return $this;
    }

    /**
     * @param JudgeTitle $title
     * @return Judge
     */
    public function addTitle(JudgeTitle $title): Judge
    {
        if(!$this->titles->contains($title)) {
            $this->titles->add($title);
            $title->setJudge($this);
        }
        return $this;
    }

    /**
     * @param JudgeTitle $title
     * @return Judge
     */
    public function removeTitle(JudgeTitle $title): Judge
    {
        if($this->titles->contains($title)) {
            $this->titles->removeElement($title);
            $title->setJudge(null);
        }
        return $this;
    }

    /**
     * @param \Traversable $titles
     * @return Judge
     */
    public function removeTitles(\Traversable $titles): Judge
    {
        foreach ($titles as $title) {
            $this->removeTitle($title);
        }
        return $this;
    }

    /**
     * @param JudgeTitle[]|Collection $titles
     * @return Judge
     */
    public function setTitles($titles): Judge
    {
        $this->titles = $titles;
        return $this;
    }

    public function hasProfileImage(): bool
    {
        return file_exists(sprintf('%s/%s.jpg', JudgeManager::IMAGE_DIRECTORY, $this->getId()));
    }

    public function hasLegacyProfileImage(): bool
    {
        return file_exists(sprintf('public/img/judges/%s.jpg', $this->getSlug()));
    }

    /**
     * @ORM\PreRemove
     */
    public function cleanUpImagesBeforeDelete()
    {
        if ($this->hasProfileImage()) {
            unlink(sprintf('%s/%s.jpg', JudgeManager::IMAGE_DIRECTORY, $this->getId()));
        }
    }

    /**
     * @return array
     */
    public function jsonSerialize()
    {
        return [
            'id' => $this->getId(),
            'name' => $this->getName(),
            'prefix' => $this->getPrefix(),
            'titles' => $this->getTitles()->getValues(),
            'slug' => $this->getSlug(),
            'hasProfileImage' => $this->hasProfileImage(),
        ];
    }
}
