<?php

namespace App\Entity;

use App\Entity\Content\Type;
use App\Entity\Research\Status;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity()
 * @ORM\EntityListeners({"App\EntityListener\ResearchListener"})
 */
class Research implements BroadcastableInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Research\Status")
     */
    private $status;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Negotiation", mappedBy="research", cascade={"remove"})
     */
    private $negotiations;

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Content\Type")
     * @ORM\JoinColumn(nullable=true)
     */
    private $contentType;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Prospect", inversedBy="researches")
     */
    private $prospects;

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

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

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

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

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

        return $this;
    }

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

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

        return $this;
    }

    public function getStatus(): ?Status
    {
        return $this->status;
    }

    public function setStatus(?Status $status): self
    {
        $this->status = $status;

        return $this;
    }

    public function getProspectsNo(): int
    {
        return $this->getProspects()->count();
    }

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

    public function getNegotiationsNo(): int
    {
        return $this->getNegotiations()->count();
    }

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

    public function isProspectResearchActive(): ?bool
    {
        return $this->prospectResearchActive;
    }

    public function setProspectResearchActive(bool $prospectResearchActive): self
    {
        $this->prospectResearchActive = $prospectResearchActive;

        return $this;
    }

    public function getNewMessagesNo()
    {
        $return = 0;
        $this->getNegotiations()->map(function (Negotiation $negotiation) use (&$return) {
            $return += $negotiation->getNewMessagesNo();
        });

        return $return;
    }

    public function getMessageRepliesNo()
    {
        $return = 0;
        $this->getNegotiations()->map(function (Negotiation $negotiation) use (&$return) {
            $return += $negotiation->getRepliesNo();
        });

        return $return;
    }

    public function getContentType(): ?Type
    {
        return $this->contentType;
    }

    public function setContentType(?Type $contentType): self
    {
        $this->contentType = $contentType;

        return $this;
    }

    public function addProspect(Prospect $prospect): self
    {
        if (!$this->prospects->contains($prospect)) {
            $this->prospects[] = $prospect;
        }

        return $this;
    }

    public function removeProspect(Prospect $prospect): self
    {
        if ($this->prospects->contains($prospect)) {
            $this->prospects->removeElement($prospect);
        }

        return $this;
    }

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

    public function getStatusChangedAt(): ?\DateTimeInterface
    {
        return $this->statusChangedAt;
    }

    public function setStatusChangedAt(?\DateTimeInterface $statusChangedAt): self
    {
        $this->statusChangedAt = $statusChangedAt;

        return $this;
    }
}
