<?php

namespace App\Entity;

use App\Entity\Traits\TimestampableTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity()
 * @ORM\EntityListeners({"App\EntityListener\TimelogListener"})
 */
class TimeLog implements BroadcastableInterface
{
    use TimestampableTrait;

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

    /**
     * @ORM\ManyToOne(targetEntity="\App\Entity\Campaign", inversedBy="schedules")
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     * @Assert\NotBlank()
     */
    private $campaign;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     * @ORM\JoinColumn(onDelete="CASCADE")
     * @Assert\NotBlank()
     */
    private $user;

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

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

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

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

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

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

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

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

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

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

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

        return $this;
    }

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

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

        return $this;
    }

    public function getTimeSpent(): ?int
    {
        return $this->timeSpent;
    }

    public function setTimeSpent(?int $timeSpent): self
    {
        $this->timeSpent = $timeSpent;

        return $this;
    }

    public function getDateFrom(): ?string
    {
        return $this->dateFrom;
    }

    public function setDateFrom(?string $dateFrom): self
    {
        $this->dateFrom = $dateFrom;

        return $this;
    }

    public function getDateTo(): ?string
    {
        return $this->dateTo;
    }

    public function setDateTo(?string $dateTo): self
    {
        $this->dateTo = $dateTo;

        return $this;
    }

    public function getType(): ?TimeLogType
    {
        return $this->type;
    }

    public function setType(?TimeLogType $type): self
    {
        $this->type = $type;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * @param mixed $subjectType
     *
     * @return TimeLog
     */
    public function setSubjectType($subjectType)
    {
        $this->subjectType = $subjectType;

        return $this;
    }

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

    /**
     * @param mixed $subjectId
     *
     * @return TimeLog
     */
    public function setSubjectId($subjectId)
    {
        $this->subjectId = $subjectId;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getSubject()
    {
        return json_decode($this->subject);
    }

    /**
     * @param mixed $subject
     *
     * @return TimeLog
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;

        return $this;
    }

    public function getDataType(): String
    {
        return 'timelog';
    }

    public function getTitle(): string
    {
        switch ($this->subjectType) {
            case 'research':
                return sprintf('Outreach for %s', $this->getSubject()->campaign->title);
            case 'negotiation':
                return sprintf('Negotiation with %s about %s', $this->getSubject()->prospect->domain->url,
                    $this->getSubject()->campaign->title);
            default:
                return '';
        }
    }
}
