<?php

namespace App\Entity;

use App\Entity\Traits\AddressableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Class Client.
 *
 * @ORM\Entity
 */
class Client implements BroadcastableInterface
{
    use AddressableTrait;

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

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     * @Assert\Length(
     *      min = 2,
     *      max = 255,
     *      minMessage = "Organization name must be at least {{ limit }} characters long",
     *      maxMessage = "Organization name cannot be longer than {{ limit }} characters"
     * )
     */
    private $title;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\ClientContact", mappedBy="client", cascade={"persist", "remove"})
     */
    private $contacts;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Campaign", mappedBy="client", cascade={"persist", "remove"}, orphanRemoval=true)
     */
    private $campaigns;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="client")
     */
    private $users;

    /**
     * @ORM\Column(type="string", length=10, nullable=true)
     * @Assert\Regex(pattern="/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/",
     *     message="Please provide valid HEX colour value (with #)")
     */
    private $primaryColour;

    /**
     * @ORM\Column(type="string", length=10, nullable=true)
     * @Assert\Regex(pattern="/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/",
     *     message="Please provide valid HEX colour value (with #)")
     */
    private $secondaryColour;

    public function __construct()
    {
        $this->contacts = new ArrayCollection();
        $this->campaigns = new ArrayCollection();
        $this->users = 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 getMainContact(): ?ClientContact
    {
        $main = $this->contacts->filter(function ($contact) {
            return $contact->isMain();
        });
        if ($main->count() > 0) {
            return $main->first();
        }

        return null;
    }

    public function addContact(ClientContact $contact): self
    {
        if (!$this->contacts->contains($contact)) {
            $this->contacts[] = $contact;
            $contact->setClient($this);
        }

        return $this;
    }

    public function removeContact(ClientContact $contact): self
    {
        if ($this->contacts->contains($contact)) {
            $this->contacts->removeElement($contact);
            // set the owning side to null (unless already changed)
            if ($contact->getClient() === $this) {
                $contact->setClient(null);
            }
        }

        return $this;
    }

    public function addCampaign(Campaign $campaign): self
    {
        if (!$this->campaigns->contains($campaign)) {
            $this->campaigns[] = $campaign;
            $campaign->setClient($this);
        }

        return $this;
    }

    public function removeCampaign(ClientContact $campaign): self
    {
        if ($this->campaigns->contains($campaign)) {
            $this->campaigns->removeElement($campaign);
            // set the owning side to null (unless already changed)
            if ($campaign->getClient() === $this) {
                $campaign->setClient(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection||Contact[]
     */
    public function getContacts(): Collection
    {
        return $this->contacts;
    }

    /**
     * @return Collection||Campaign[]
     */
    public function getCampaigns(): Collection
    {
        return $this->campaigns;
    }

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

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

    public function addUser(User $user): self
    {
        if (!$this->users->contains($user)) {
            $this->users[] = $user;
            $user->setClient($this);
        }

        return $this;
    }

    public function removeUser(User $user): self
    {
        if ($this->users->contains($user)) {
            $this->users->removeElement($user);
            // set the owning side to null (unless already changed)
            if ($user->getClient() === $this) {
                $user->setClient(null);
            }
        }

        return $this;
    }

    public function getPrimaryColour(): ?string
    {
        return $this->primaryColour;
    }

    public function setPrimaryColour(?string $primaryColour): self
    {
        $this->primaryColour = $primaryColour;

        return $this;
    }

    public function getSecondaryColour(): ?string
    {
        return $this->secondaryColour;
    }

    public function setSecondaryColour(?string $secondaryColour): self
    {
        $this->secondaryColour = $secondaryColour;

        return $this;
    }
}
