<?php

namespace App\Entity\Campaign\Tasklist;

use App\Entity\BroadcastableInterface;
use App\Entity\Campaign;
use App\Entity\TimeLogType;
use App\Entity\Traits\TimestampableTrait;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\OrderBy;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity()
 * @ORM\Table(name="campaign_tasklist")
 */
class Tasklist implements BroadcastableInterface
{
    use TimestampableTrait;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    /**
     * @ORM\OneToMany(targetEntity="Task", mappedBy="tasklist", cascade={"persist"})
     * @OrderBy({"priority" = "DESC"})
     */
    private $tasks;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     * @ORM\JoinColumn(nullable=false)
     * @Gedmo\Blameable(on="create")
     */
    private $createdBy;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Campaign", inversedBy="tasklists")
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    private $campaign;

    /**
     * @ORM\Column(name="template", type="boolean", nullable=false)
     * @Assert\NotBlank()
     */
    private $template;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\TimeLogType")
     * @Assert\NotBlank()
     */
    private $timeLogType;

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

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

    /**
     * @return Collection
     */
    public function getTasks(): Collection
    {
        return $this->tasks;
    }

    public function addTask(Task $task): self
    {
        if (!$this->tasks->contains($task)) {
            $this->tasks[] = $task;
            $task->setTasklist($this);
        }

        return $this;
    }

    public function removeTask(Task $task): self
    {
        if ($this->tasks->contains($task)) {
            $this->tasks->removeElement($task);
            // set the owning side to null (unless already changed)
            if ($task->getTasklist() === $this) {
                $task->setTasklist(null);
            }
        }

        return $this;
    }

    public function getCreatedBy(): ?User
    {
        return $this->createdBy;
    }

    public function setCreatedBy(?User $createdBy): self
    {
        $this->createdBy = $createdBy;

        return $this;
    }

    public function getCampaign(): ?Campaign
    {
        return $this->campaign;
    }

    public function setCampaign(?Campaign $campaign): self
    {
        $this->campaign = $campaign;

        return $this;
    }

    /**
     * @param mixed $title
     *
     * @return Tasklist
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

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

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

        return $this;
    }

    /**
     * @param mixed $template
     *
     * @return Tasklist
     */
    public function setTemplate($template)
    {
        $this->template = $template;

        return $this;
    }

    /**
     * @return bool|null
     */
    public function isTemplate(): ?bool
    {
        return $this->template;
    }

    public function getTimeLogType(): ?TimeLogType
    {
        return $this->timeLogType;
    }

    public function setTimeLogType(?TimeLogType $timeLogType): self
    {
        $this->timeLogType = $timeLogType;

        return $this;
    }

    public function __clone()
    {
        if ($this->id) {
            $this->id = null;
            $this->createdAt = null;
            $this->updatedAt = null;
            $this->campaign = null;

            $tasks = $this->getTasks();
            $this->tasks = new ArrayCollection();
            if (!$tasks->isEmpty()) {
                foreach ($tasks as $task) {
                    $this->addTask(clone $task);
                }
            }
        }
    }

    public function getDataType(): string
    {
        return 'tasklist';
    }
}
