<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Prospect", inversedBy="negotiations")
     * @ORM\JoinColumn(nullable=true)
     */
    private $prospect;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\NegotiationStatus", inversedBy="negotiations")
     * @ORM\JoinColumn(nullable=true)
     */
    private $status;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Content", inversedBy="negotiations")
     * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
     */
    private $content;

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="negotiations")
     * @Gedmo\Blameable(on="create")
     */
    private $user;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\NegotiationMessage", mappedBy="negotiation", fetch="EXTRA_LAZY")
     */
    private $messages;

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Currency")
     */
    private $currency;

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

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

    public function getProspect(): ?Prospect
    {
        return $this->prospect;
    }

    public function setProspect(?Prospect $prospect): self
    {
        $this->prospect = $prospect;

        return $this;
    }

    public function getContent(): ?Content
    {
        return $this->content;
    }

    public function setContent(?Content $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getCostProposed(): ?float
    {
        return $this->costProposed;
    }

    public function setCostProposed(?int $costProposed): self
    {
        $this->costProposed = $costProposed;

        return $this;
    }

    public function getCostAgreed(): ?float
    {
        return $this->costAgreed;
    }

    public function setCostAgreed(?int $costAgreed): self
    {
        $this->costAgreed = $costAgreed;

        return $this;
    }

    /**
     * @return NegotiationStatus|null
     */
    public function getStatus(): ?NegotiationStatus
    {
        return $this->status;
    }

    /**
     * @param NegotiationStatus $status
     *
     * @return Negotiation
     */
    public function setStatus(NegotiationStatus $status): self
    {
        $this->status = $status;

        return $this;
    }

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

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

        return $this;
    }

    public function getMessagesNo(): int
    {
        return $this->messages->count();
    }

    public function getNewMessagesNo()
    {
        return $this->messages->filter(function (NegotiationMessage $message) {
            return 'INBOUND' === $message->getDirection() && is_null($message->getReadAt());
        })->count();
    }

    public function getRepliesNo()
    {
        return $this->messages->filter(function (NegotiationMessage $message) {
            return 'INBOUND' === $message->getDirection();
        })->count();
    }

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

    public function addMessage(NegotiationMessage $negotiationMessage): self
    {
        if (!$this->messages->contains($negotiationMessage)) {
            $this->messages[] = $negotiationMessage;
            $negotiationMessage->setNegotiation($this);
        }

        return $this;
    }

    public function removeMessage(NegotiationMessage $negotiationMessage): self
    {
        if ($this->messages->contains($negotiationMessage)) {
            $this->messages->removeElement($negotiationMessage);
            // set the owning side to null (unless already changed)
            if ($negotiationMessage->getNegotiation() === $this) {
                $negotiationMessage->setNegotiation(null);
            }
        }

        return $this;
    }

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

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

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(?string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getResearch(): ?Research
    {
        return $this->research;
    }

    public function setResearch(?Research $research): self
    {
        $this->research = $research;

        return $this;
    }

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

    public function getCurrency(): ?Currency
    {
        return $this->currency;
    }

    public function setCurrency(?Currency $currency): self
    {
        $this->currency = $currency;

        return $this;
    }
}
