<?php

namespace AppBundle\Entity\Message;

use AppBundle\Entity\AccessAndFunction\Access;
use AppBundle\Entity\Core\Tagg;
use Doctrine\ORM\Mapping as ORM;
use Dunglas\ApiBundle\Annotation\Iri;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use AppBundle\Entity\Traits\TimestampableEntity;
use AppBundle\Entity\Traits\CreatorUpdaterEntity;
use AppBundle\Entity\Organization\Organization;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Annotation\DefaultField;
use AppBundle\Annotation\ExportTranslate;

/**
 * Classe de base de @see Mail, Email, Sms
 *
 * @Iri("http://schema.org/Message")
 */
#[ORM\Entity(repositoryClass: 'AppBundle\Entity\Message\Repository\MessageRepository')]
#[ORM\Table(name: 'Message')]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
#[ORM\DiscriminatorMap(['message' => 'Message', 'mail' => 'Mail', 'email' => 'Email', 'sms' => 'Sms'])]
class Message implements MessageInterface
{
    use TimestampableEntity;
    use CreatorUpdaterEntity;
    /**
     * @var int
     */
    #[ORM\Column(type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Groups(['message', 'email_detail', 'mail', 'email', 'sms', 'newsletter', 'reportmessage', 'message_list', 'messagesend_list', 'template_list'])]
    private $id;
    /**
     * @var Access
     *
     * @DefaultField
     */
    #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\AccessAndFunction\Access', inversedBy: 'messages')]
    #[ORM\JoinColumn(nullable: true)]
    #[Groups(['message', 'mail', 'email', 'sms', 'newsletter', 'reportmessage', 'message_list', 'messagesend_list', 'template_list', 'email_detail'])]
    private $author;
    /**
     * @var Organization
     *
     * @DefaultField
     */
    #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\Organization\Organization')]
    #[ORM\JoinColumn(nullable: true)]
    #[Groups(['message', 'mail', 'email', 'sms', 'newsletter', 'reportmessage'])]
    private $organization;
    /**
     * @var String
     *
     * @ExportTranslate(true)
     */
    #[ORM\Column(type: 'string', options: ['default' => 'ORGANIZATION'])]
    #[Assert\Type(type: 'string')]
    #[Groups(['message', 'mail', 'email', 'newsletter', 'reportmessage', 'message_list', 'email_detail', 'messagesend_list'])]
    private $sender;
    /**
     * @var String
     */
    #[ORM\Column(type: 'string', options: ['default' => 'MEMBER_AND_GUARDIANS'])]
    #[Assert\Type(type: 'string')]
    #[Assert\Choice(callback: ['AppBundle\Enum\Message\SendToEnum', 'toArray'])]
    #[Groups(['message', 'mail', 'email', 'sms', 'newsletter', 'message_list', 'messagesend_list', 'email_detail', 'template_list'])]
    private $sendTo;
    /**
     * @var bool
     */
    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    #[Assert\Type(type: 'boolean')]
    #[Groups(['mail', 'email', 'newsletter', 'message_list', 'messagesend_list', 'template_list'])]
    private $sharingWithAdministration;
    /**
     * @var string
     */
    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    #[Assert\Type(type: 'string')]
    #[Groups(['message', 'mail', 'email', 'sms', 'newsletter', 'reportmessage', 'mail_reference', 'email_reference', 'sms_reference', 'message_list', 'messagesend_list', 'email_detail', 'email_detail_mailattached', 'template_list'])]
    private $about;
    /**
     * @var string
     */
    #[ORM\Column(type: 'text', nullable: true)]
    #[Assert\Type(type: 'string')]
    #[Groups(['message', 'mail', 'email', 'newsletter', 'reportmessage'])]
    private $headline;
    /**
     * @var string
     */
    #[ORM\Column(type: 'text', nullable: true)]
    #[Assert\Type(type: 'string')]
    #[Groups(['message', 'mail', 'email', 'sms', 'newsletter', 'reportmessage', 'email_detail'])]
    private $text;
    /**
     * @var string
     */
    #[ORM\Column(type: 'string', options: ['default' => 'DRAFT'])]
    #[Assert\Type(type: 'string')]
    #[Assert\Choice(callback: ['AppBundle\Enum\Message\MessageStatusEnum', 'toArray'])]
    #[Groups(['message', 'mail', 'email', 'sms', 'newsletter', 'reportmessage', 'message_list', 'messagesend_list'])]
    private $status;
    /**
     * @var bool
     */
    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    #[Assert\Type(type: 'boolean')]
    #[Assert\NotNull]
    #[Groups(['super_admin'])]
    private $isSystem = false;
    /**
     * @var bool
     */
    #[ORM\Column(type: 'boolean', options: ['default' => 0])]
    #[Assert\Type(type: 'boolean')]
    #[Assert\NotNull]
    #[Groups(['message', 'mail', 'email', 'sms', 'newsletter'])]
    private $template = false;
    /**
     * @var ArrayCollection<AppBundle\Entity\Core\File>.
     */
    #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\Core\File', cascade: ['persist'], orphanRemoval: true)]
    #[ORM\JoinTable(name: 'messages_files', joinColumns: [], inverseJoinColumns: [])]
    #[ORM\JoinColumn(name: 'message_id', referencedColumnName: 'id', onDelete: 'cascade')]
    #[ORM\JoinColumn(name: 'file_id', referencedColumnName: 'id', onDelete: 'cascade')]
    #[Groups(['message', 'mail', 'email', 'newsletter', 'reportmessage', 'email_detail'])]
    private $files;
    /**
     * @var ArrayCollection<AppBundle\Entity\Core\Document>.
     */
    #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\Core\Document')]
    #[ORM\JoinTable(name: 'messages_documents', joinColumns: [], inverseJoinColumns: [])]
    #[ORM\JoinColumn(name: 'message_id', referencedColumnName: 'id')]
    #[ORM\JoinColumn(name: 'document_id', referencedColumnName: 'id')]
    #[Groups(['message', 'mail', 'email', 'newsletter', 'reportmessage'])]
    private $documents;
    /**
     * @var array
     */
    #[ORM\Column(type: 'text', nullable: true)]
    #[Groups(['message', 'mail', 'email', 'sms', 'newsletter', 'reportmessage', 'email_detail'])]
    private $recipientRule;
    /**
     * @var \DateTime
     */
    #[ORM\Column(type: 'datetime', nullable: true)]
    #[Assert\Date]
    #[Groups(['reportmessage', 'message_list', 'messagesend_list'])]
    private $dateSent;
    /**
     *
     * @var string
     */
    #[ORM\Column(type: 'string', length: 35, nullable: true)]
    #[Assert\Length(min: 0, max: 35)]
    #[Groups(['message', 'mail', 'email', 'sms', 'newsletter', 'reportmessage', 'message_list', 'messagesend_list', 'template_list', 'mail_reference', 'email_reference', 'email_detail', 'sms_reference'])]
    private $reference;
    /**
     * @var boolean
     */
    #[ORM\Column(type: 'boolean', options: ['default' => 0])]
    #[Assert\Type(type: 'boolean')]
    #[Groups(['email', 'reportmessage', 'message_list', 'email_detail', 'messagesend_list'])]
    private $acknowledgment;
    /**
     * @var ArrayCollection<Access>
     */
    #[ORM\OneToMany(targetEntity: 'AppBundle\Entity\Message\ReportMessage', mappedBy: 'message', orphanRemoval: true, cascade: ['persist'])]
    #[Groups(['messagesend_list'])]
    private $reportMessage;
    /**
     * @var string
     */
    #[Groups(['messagesend_list'])]
    private $reportMessageTemplate;
    /**
     * @var string
     */
    #[Groups(['template', 'message_list', 'messagesend_list'])]
    private $type;
    /**
     * @var bool
     */
    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    #[Assert\Type(type: 'boolean')]
    #[Groups(['mail', 'email', 'newsletter', 'message_list', 'messagesend_list', 'email_detail', 'template_list'])]
    private $childrenOrganization;
    /**
     * @var ArrayCollection<Tagg>
     */
    #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\Core\Tagg', cascade: ['persist'], inversedBy: 'messages')]
    #[Assert\Valid]
    #[ORM\JoinTable(name: 'tag_message', joinColumns: [], inverseJoinColumns: [])]
    #[ORM\JoinColumn(name: 'message_id', referencedColumnName: 'id')]
    #[ORM\JoinColumn(name: 'tag_id', referencedColumnName: 'id')]
    #[Groups(['message_tags', 'manage_tags', 'messagesend_list', 'message_list', 'template_list', 'email_detail'])]
    private $tags;
    /**
     * The constructor
     */
    public function __construct() {
        $this->sender = \AppBundle\Enum\Message\SenderEnum::ORGANIZATION;
        $this->sendTo = \AppBundle\Enum\Message\SendToEnum::MEMBER;
        $this->status = \AppBundle\Enum\Message\MessageStatusEnum::DRAFT;
        $this->sharingWithAdministration = false;
        $this->childrenOrganization = false;
        $this->attachments = new ArrayCollection();
        $this->files = new ArrayCollection();
        $this->documents = new ArrayCollection();
        $this->reportMessage = new ArrayCollection();
        $this->tags = new ArrayCollection();

        $this->acknowledgment = false;

    }
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Sets id
     *
     * @param int $id
     * @return this
     */
    public function setId($id)
    {
        $this->id = $id;
        
        return $this;
    }
    /**
     * Set about
     *
     * @param string $about
     *
     * @return Message
     */
    public function setAbout($about)
    {
        $this->about = $about;

        return $this;
    }
    /**
     * Get about
     *
     * @return string
     */
    public function getAbout()
    {
        return $this->about;
    }
    /**
     * Set text
     *
     * @param string $text
     *
     * @return Message
     */
    public function setText($text)
    {
        $this->text = $text;

        return $this;
    }
    /**
     * Get text
     *
     * @return string
     */
    public function getText()
    {
        return html_entity_decode($this->text);
    }
    /**
     * Set status
     *
     * @param string $status
     *
     * @return Message
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }
    /**
     * Get status
     *
     * @return string
     */
    public function getStatus()
    {
        return $this->status;
    }
    /**
     * Set isSystem
     *
     * @param boolean $isSystem
     *
     * @return Message
     */
    public function setIsSystem($isSystem)
    {
        $this->isSystem = $isSystem;

        return $this;
    }
    /**
     * Get isSystem
     *
     * @return boolean
     */
    public function getIsSystem()
    {
        return $this->isSystem;
    }
    /**
     * Set template
     *
     * @param boolean $template
     *
     * @return Message
     */
    public function setTemplate($template)
    {
        $this->template = $template;

        return $this;
    }
    /**
     * Get template
     *
     * @return boolean
     */
    public function getTemplate()
    {
        return $this->template;
    }
    /**
     * Set author
     *
     * @param \AppBundle\Entity\AccessAndFunction\Access $author
     *
     * @return Message
     */
    public function setAuthor(\AppBundle\Entity\AccessAndFunction\Access $author = null)
    {
        $this->author = $author;

        return $this;
    }
    /**
     * Get author
     *
     * @return \AppBundle\Entity\AccessAndFunction\Access
     */
    public function getAuthor()
    {
        return $this->author;
    }
    /**
     * Set organization
     *
     * @param \AppBundle\Entity\Organization\Organization $organization
     *
     * @return Message
     */
    public function setOrganization(\AppBundle\Entity\Organization\Organization $organization = null)
    {
        $this->organization = $organization;

        return $this;
    }
    /**
     * Get organization
     *
     * @return \AppBundle\Entity\Organization\Organization
     */
    public function getOrganization()
    {
        return $this->organization;
    }
    /**
     * Set recipientRule
     *
     * @param String $recipientRule
     *
     * @return Message
     */
    public function setRecipientRule($recipientRule)
    {
        $this->recipientRule = $recipientRule;

        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function getRecipientRule()
    {
        return $this->recipientRule;
    }
    /**
     * Set dateSent
     *
     * @param \DateTime $dateSent
     *
     * @return Message
     */
    public function setDateSent($dateSent)
    {
        $this->dateSent = $dateSent;

        return $this;
    }
    /**
     * Get dateSent
     *
     * @return \DateTime
     */
    public function getDateSent()
    {
        return $this->dateSent;
    }
    /**
     * Set sender
     *
     * @param String $sender
     *
     * @return Message
     */
    public function setSender($sender)
    {
        $this->sender = $sender;

        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function getSender()
    {
        return $this->sender;
    }
    /**
     * Set reference
     *
     * @param string $reference
     *
     * @return Message
     */
    public function setReference($reference)
    {
        $this->reference = $reference;

        return $this;
    }
    /**
     * Get reference
     *
     * @return string
     */
    public function getReference()
    {
        return $this->reference;
    }
    /**
     * Add file
     *
     * @param \AppBundle\Entity\Core\File $file
     *
     * @return Message
     */
    public function addFile(\AppBundle\Entity\Core\File $file)
    {
        $this->files[] = $file;

        return $this;
    }
    /**
     * Remove file
     *
     * @param \AppBundle\Entity\Core\File $file
     */
    public function removeFile(\AppBundle\Entity\Core\File $file)
    {
        $this->files->removeElement($file);
    }
    /**
     * Get files
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getFiles()
    {
        return $this->files;
    }
    /**
     * Add document
     *
     * @param \AppBundle\Entity\Core\Document $document
     *
     * @return Message
     */
    public function addDocument(\AppBundle\Entity\Core\Document $document)
    {
        $this->documents[] = $document;

        return $this;
    }
    /**
     * Remove document
     *
     * @param \AppBundle\Entity\Core\Document $document
     */
    public function removeDocument(\AppBundle\Entity\Core\Document $document)
    {
        $this->documents->removeElement($document);
    }
    /**
     * Get documents
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getDocuments()
    {
        return $this->documents;
    }
    /**
     * Set headline
     *
     * @param string $headline
     *
     * @return Message
     */
    public function setHeadline($headline)
    {
        $this->headline = $headline;

        return $this;
    }
    /**
     * Get headline
     *
     * @return string
     */
    public function getHeadline()
    {
        return $this->headline;
    }
    /**
     * Set sendTo
     *
     * @param string $sendTo
     *
     * @return Message
     */
    public function setSendTo($sendTo)
    {
        $this->sendTo = $sendTo;

        return $this;
    }
    /**
     * Get sendTo
     *
     * @return string
     */
    public function getSendTo()
    {
        return $this->sendTo;
    }
    /**
     * Set sharingWithAdministration
     *
     * @param boolean $sharingWithAdministration
     *
     * @return Message
     */
    public function setSharingWithAdministration($sharingWithAdministration)
    {
        $this->sharingWithAdministration = $sharingWithAdministration;

        return $this;
    }
    /**
     * Get sharingWithAdministration
     *
     * @return boolean
     */
    public function getSharingWithAdministration()
    {
        return $this->sharingWithAdministration;
    }
    /**
     * Set childrenOrganization
     *
     * @param boolean $childrenOrganization
     *
     * @return Message
     */
    public function setChildrenOrganization($childrenOrganization)
    {
        $this->childrenOrganization = $childrenOrganization;

        return $this;
    }
    /**
     * Get childrenOrganization
     *
     * @return boolean
     */
    public function getChildrenOrganization()
    {
        return $this->childrenOrganization;
    }
    /**
     * Set acknowledgment
     *
     * @param boolean $acknowledgment
     *
     * @return $this
     */
    public function setAcknowledgment($acknowledgment)
    {
        $this->acknowledgment = $acknowledgment;

        return $this;
    }
    /**
     * Get acknowledgment
     *
     * @return boolean
     */
    public function getAcknowledgment()
    {
        return $this->acknowledgment;
    }
    public function getAllReportMessage()
    {
        return $this->reportMessage;
    }
    /**
     * @return ArrayCollection
     */
    public function getReportMessage()
    {
        return $this->reportMessage->slice(0,2);
    }
    /**
     * @param ArrayCollection $reportMessage
     */
    public function setReportMessage(ArrayCollection $reportMessage)
    {
        $this->reportMessage = $reportMessage;
    }
    public function addReportMessage(ReportMessage $reportMessage)
    {
        $reportMessage->setMessage($this);
        $this->reportMessage[] = $reportMessage;

        return $this;
    }
    public function getReportMessageTemplate()
    {
        $reportMessageTemplate = "";
        $reportMessages =  $this->getReportMessage();
        if(count($reportMessages) > 1){
            $reportMessageTemplate = "Multi-destinataires";
        }else if(count($reportMessages) === 1){
            $reportMessageTemplate = $reportMessages[0] ? $reportMessages[0]->getRecipient() : '';
        }
        return $reportMessageTemplate;
    }
    /**
     * get type
     *
     * @return mixed
     */
    public function getType()
    {
        $explodeClass = explode("\\",get_class($this));
        return lcfirst(end($explodeClass));
    }
    /**
     * Add tag
     *
     * @param \AppBundle\Entity\Core\Tagg $tag
     *
     * @return Message
     */
    public function addTag(\AppBundle\Entity\Core\Tagg $tag)
    {
        $this->tags[] = $tag;

        return $this;
    }
    /**
     * Remove tag
     *
     * @param \AppBundle\Entity\Core\Tagg $tag
     */
    public function removeTag(\AppBundle\Entity\Core\Tagg $tag)
    {
        $this->tags->removeElement($tag);
    }
    /**
     * Get tags
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTags()
    {
        return $this->tags;
    }
}
