<?php

namespace App\Model\Entities\Components\Event;

use App\Model\Entities\BaseNamedRecord;
use App\Model\Entities\Security\User;
use App\Model\Entities\Templates\EventTemplate;
use App\Model\Interfaces\IResource;
use App\Model\Interfaces\ISite;
use App\Model\Interfaces\ITemplate;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="event")
 */
class Event extends BaseNamedRecord implements IResource
{
    const CREATED = 'created';

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

    /**
     * @ORM\Column(type="datetime")
     */
    protected $startAt;

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

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

    /**
     * @var ISite
     * @ORM\ManyToOne(targetEntity="App\Model\Entities\PortalSite")
     */
    protected $site;

    /**
     * @var EventTemplate
     * @ORM\ManyToOne(targetEntity="App\Model\Entities\Templates\EventTemplate")
     */
    protected $template;

    /**
     * @var ArrayCollection
     * @ORM\ManyToMany(targetEntity="App\Model\Entities\Security\User")
     */
    protected $users;

    public function __construct(User $createdBy)
    {
        parent::__construct($createdBy);
        $this->setState(self::CREATED);
    }


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

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

    /**
     * @return DateTime
     */
    public function getStartAt()
    {
        return $this->startAt;
    }

    /**
     * @param DateTime $startAt
     */
    public function setStartAt(DateTime $startAt)
    {
        $this->startAt = $startAt;
    }

    /**
     * @return DateTime
     */
    public function getEndAt()
    {
        return $this->endAt;
    }

    /**
     * @param DateTime $endAt
     */
    public function setEndAt(DateTime $endAt = null)
    {
        $this->endAt = $endAt;
    }

    /**
     * @return int
     */
    public function getUserLimit()
    {
        return $this->userLimit;
    }

    /**
     * @param int $userLimit
     */
    public function setUserLimit(int $userLimit = null)
    {
        $this->userLimit = $userLimit;
    }

    /**
     * @return ISite
     */
    public function getSite() : ISite
    {
        return $this->site;
    }

    /**
     * @param ISite $site
     */
    public function setSite(ISite $site)
    {
        $this->site = $site;
    }

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

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

    /**
     * @return ArrayCollection|User[]
     */
    public function getUsers()
    {
        return $this->users;
    }

    public function addUser(User $user)
    {
        if (!$this->users->contains($user)) {
            $this->users->add($user);
        }
    }

    public function removeUser(User $user)
    {
        $this->users->removeElement($user);
    }

    protected function getValidStates() : array
    {
        $states = parent::getValidStates();
        $states[] = self::CREATED;
        return $states;
    }


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