<?php

namespace App\Model\Entities\Components\Forum;

use App\Model\Entities\BaseNamedRecord;
use App\Model\Entities\Security\User;
use App\Model\Entities\Templates\ForumTemplate;
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="forum")
 */
class Forum extends BaseNamedRecord implements IResource, ICommonRecord
{
    /**
     * @var ForumTemplate
     * @ORM\ManyToOne(targetEntity="App\Model\Entities\Templates\ForumTemplate")
     * @ORM\JoinColumn(nullable=true)
     */
    protected $template;

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

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

    /**
     * @var Forum
     * @ORM\ManyToOne(targetEntity="Forum", inversedBy="subforums")
     * @ORM\JoinColumn(nullable=true)
     */
    protected $parentForum;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Forum", mappedBy="parentForum")
     */
    protected $subforums;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Topic", mappedBy="forum")
     */
    protected $topics;

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

        $this->topics = new ArrayCollection();
        $this->subforums = new ArrayCollection();
    }

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

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

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

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

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

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

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

    /**
     * @return Forum
     */
    public function getParentForum()
    {
        return $this->parentForum;
    }

    /**
     * @param Forum $parentForum
     */
    public function setParentForum(Forum $parentForum = null)
    {
        $this->parentForum = $parentForum;
    }

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

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

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