<?php

namespace App\Model\Entities\Components\Gallery;

use App\Model\Entities\BaseNamedRecord;
use App\Model\Entities\Image;
use App\Model\Entities\Security\User;
use App\Model\Entities\Templates\GalleryTemplate;
use App\Model\Interfaces\ICommonRecord;
use App\Model\Interfaces\IResource;
use App\Model\Interfaces\ITemplate;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity
 * @ORM\Table(name="gallery")
 */
class Gallery extends BaseNamedRecord implements IResource, ICommonRecord
{
    /**
     * @var GalleryTemplate
     * @ORM\ManyToOne(targetEntity="App\Model\Entities\Templates\GalleryTemplate")
     */
    protected $template;

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

    /**
     * @var Gallery
     * @ORM\ManyToOne(targetEntity="Gallery", inversedBy="subgalleries")
     * @ORM\JoinColumn(nullable=true)
     */
    protected $parentGallery;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Gallery", mappedBy="parentGallery")
     */
    protected $subgalleries;

    /**
     * @var ArrayCollection
     * @ORM\ManyToMany(targetEntity="App\Model\Entities\Image")
     */
    protected $images;

    public function __construct(User $createdBy)
    {
        parent::__construct($createdBy);

        $this->subgalleries = new ArrayCollection();
        $this->images = new ArrayCollection();
    }

    /**
     * @return GalleryTemplate
     */
    public function getTemplate()
    {
        return $this->template;
    }

    /**
     * @param GalleryTemplate $template
     */
    public function setTemplate(GalleryTemplate $template)
    {
        $this->template = $template;
    }

    /**
     * @return mixed
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param mixed $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * @return Gallery
     */
    public function getParentGallery()
    {
        return $this->parentGallery;
    }

    /**
     * @param Gallery $parentGallery
     */
    public function setParentGallery(Gallery $parentGallery)
    {
        $this->parentGallery = $parentGallery;
    }

    /**
     * @return ArrayCollection
     */
    public function getSubgalleries()
    {
        return $this->subgalleries;
    }

    /**
     * @return ArrayCollection
     */
    public function getImages()
    {
        return $this->images;
    }

    public function addImage(Image $image)
    {
        if (!$this->images->contains($image)) {
            $this->images->add($image);
        }
    }

    public function removeImage(Image $image)
    {
        $this->images->remove($image);
    }

    public function getResourceTemplate() : ITemplate
    {
        if ($this->getTemplate()) {
            return $this->getTemplate();
        }

        return $this->getParentGallery()->getResourceTemplate();
    }
}