<?php

namespace App\Entity\Campaign\Tasklist;

use App\Entity\BroadcastableInterface;
use App\Entity\Campaign\Tasklist\Task\Activity;
use App\Entity\FileAssociationTask;
use App\Entity\Traits\TimestampableTrait;
use App\Entity\User;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity()
 * @ORM\Table(name="campaign_task")
 * @ORM\EntityListeners({"App\EntityListener\TaskListener"})
 */
class Task implements BroadcastableInterface
{
    use TimestampableTrait;

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

    /**
     * @ORM\ManyToOne(targetEntity="Tasklist", inversedBy="tasks")
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     * @Assert\NotBlank()
     */
    private $tasklist;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $title;

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     * @ORM\JoinColumn(nullable=true)
     */
    private $user;

    /**
     * @var DateTime
     * @ORM\Column(type="datetime", nullable=true)
     * @Gedmo\Timestampable(on="change", field="completed")
     */
    private $completedAt;

    /**
     * @ORM\Column(type="boolean", nullable=true, options={"default" : 0})
     */
    private $completed;

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

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\CommentTask", mappedBy="task", cascade={"remove"})
     */
    private $comments;

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

    /**
     * @ORM\Column(type="smallint", options={"default": 0}, nullable=true)
     */
    private $priority;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Campaign\Tasklist\Task\Activity", mappedBy="task",
     *     cascade={"persist"}, orphanRemoval=true)
     */
    private $activities;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\FileAssociationTask", mappedBy="task", orphanRemoval=true)
     */
    private $files;

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

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

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

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

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

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

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

        return $this;
    }

    public function __clone()
    {
        if ($this->id) {
            $this->id = null;
            $this->createdAt = null;
            $this->updatedAt = null;
            $this->completed = 0;
            $this->completedAt = null;
            $this->createdBy = null;

            if (!$this->getTasklist()->isTemplate()) {
                $this->user = null;
            }
        }
    }

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

    /**
     * @param mixed $tasklist
     *
     * @return Task
     */
    public function setTasklist($tasklist)
    {
        $this->tasklist = $tasklist;

        return $this;
    }

    /**
     * @param $completed
     *
     * @return Task
     */
    public function setCompleted($completed): self
    {
        $this->completed = $completed;

        return $this;
    }

    public function isCompleted(): ?bool
    {
        return $this->completed;
    }

    /**
     * @return DateTime
     */
    public function getCompletedAt(): ?DateTime
    {
        return $this->completedAt;
    }

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

    public function getDueAt(): ?DateTimeInterface
    {
        return $this->dueAt;
    }

    public function setDueAt(?DateTimeInterface $dueAt): self
    {
        $this->dueAt = $dueAt;

        return $this;
    }

    public function getPriority(): ?int
    {
        return $this->priority;
    }

    public function setPriority(?int $priority): self
    {
        $this->priority = $priority;

        return $this;
    }

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

    public function addActivity(Activity $activity): self
    {
        if (!$this->activities->contains($activity)) {
            $this->activities[] = $activity;
            $activity->setTask($this);
        }

        return $this;
    }

    public function removeActivity(Activity $activity): self
    {
        if ($this->activities->contains($activity)) {
            $this->activities->removeElement($activity);
            // set the owning side to null (unless already changed)
            if ($activity->getTask() === $this) {
                $activity->setTask(null);
            }
        }

        return $this;
    }

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

    public function addFile(FileAssociationTask $file): self
    {
        if (!$this->files->contains($file)) {
            $this->files[] = $file;
            $file->setTask($this);
        }

        return $this;
    }

    public function removeFile(FileAssociationTask $file): self
    {
        if ($this->files->contains($file)) {
            $this->files->removeElement($file);
            // set the owning side to null (unless already changed)
            if ($file->getTask() === $this) {
                $file->setBle(null);
            }
        }

        return $this;
    }

    public function getCampaign()
    {
        $campaign = $this->getTasklist()->getCampaign();
        if (null === $campaign) {
            return [];
        }

        return ['id' => $campaign->getId(), 'title' => $campaign->getTitle()];
    }
}
