<?php

namespace RxConnexionBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use RxConnexionBundle\Entity\Base\BaseEntity;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\MaxDepth;

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\EmailRepository")
 * @ORM\Table(name="email")
 */

class EmailEntity extends BaseEntity
{
    const EMAIL_STATUS_PENDING = 'Pending';
    const EMAIL_STATUS_SENT = 'Sent';

    /**
     * @ORM\Column(name="email", type="string")
     * @Groups("forAudit")
     */
    protected $email;

    /**
     * @ORM\Column(name="subject", type="string")
     * @Groups("forAudit")
     */
    protected $subject;

    /**
     * @ORM\Column(name="detail", type="text", nullable=true)
     * @Groups("forAudit")
     */
    protected $detail;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\EmailAttachmentFileEntity", mappedBy="email", cascade={"remove"})
     */
    protected $emailAttachmentFiles;

    /**
     * @ORM\Column(name="is_deleted", type="boolean")
     */
    protected $isDeleted;


    /**
     * Set createdAt
     *
     * @ORM\PrePersist
     */
    public function setCreatedAtValue()
    {
        parent::setCreatedAtValue();
        $this->isDeleted = 0;
    }

    public function __construct($data=null)
    {
        if(!is_null($data)) {
            $this->id = $data->getId();
            $this->email = $data->getEmail();
            $this->subject = $data->getSubject();
            $this->detail = $data->getDetail();
        }

        $this->emailAttachmentFiles = new ArrayCollection();
    }

//    /**
//     * Remove the photo from the disk
//     *
//     * @ORM\PreRemove
//     */
//    public function removeFile() {
//
//        $file = $this->getUploadRootDir() . '/' . $this->parsedDescription;
//        if(!empty($this->parsedDescription) && file_exists($file)) unlink($file);
//    }

    /*--------------------------------------------------------------------------------------------------------*/
    /*					Use Defined Setters and Getters														  */
    /*--------------------------------------------------------------------------------------------------------*/


    /**
     * Get idEncoded
     *
     * @return string
     */
    public function getIdEncoded() {

        return base64_encode($this->id);
    }


    /**
     * Get uploadRootDir
     *
     * @return string
     */
    public function getTmpRootDir() {

        return __DIR__ . '/../../../web/uploads/patient/letters/tmp';
    }

    /**
     * Get uploadRootDir
     *
     * @return string
     */
    public function getTmpRootDir2() {

        return '/../../../uploads/patient/letters/tmp';
    }

    /**
     * Get birthDateAsString
     *
     * @return string
     */
    public function getBirthDateAsString() {

        $dttst = new DateTimeToStringTransformer();
        return !empty($this->birthDate) ? date('m/d/Y', strtotime($dttst->transform($this->birthDate))) : '';
    }

//    /**
//     * get fileWebPath
//     *
//     * @return string
//     */
//    public function getFileWebPath() {
//
//        $parsedDesc = $this->parsedDescription;
//        $file = $this->getUploadRootDir() . '/' . $parsedDesc;
//
//        if(!empty($parsedDesc) && file_exists($file)) {
//            return '/' . $this->getUploadDir() . '/' . $parsedDesc;
//        } else {
//            return '';
//        }
//    }

    /*--------------------------------------------------------------------------------------------------------*/
    /*					Setters and Getters																	  */
    /*--------------------------------------------------------------------------------------------------------*/


    /**
     * Set email
     *
     * @param string $email
     *
     * @return EmailEntity
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set subject
     *
     * @param string $subject
     *
     * @return EmailEntity
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;

        return $this;
    }

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

    /**
     * Set detail
     *
     * @param string $detail
     *
     * @return EmailEntity
     */
    public function setDetail($detail)
    {
        $this->detail = $detail;

        return $this;
    }

    /**
     * Get detail
     *
     * @return string
     */
    public function getDetail()
    {
        return $this->detail;
    }

    /**
     * Set isDeleted
     *
     * @param boolean $isDeleted
     *
     * @return EmailEntity
     */
    public function setIsDeleted($isDeleted)
    {
        $this->isDeleted = $isDeleted;

        return $this;
    }

    /**
     * Get isDeleted
     *
     * @return boolean
     */
    public function getIsDeleted()
    {
        return $this->isDeleted;
    }

    /**
     * Add emailAttachmentFile
     *
     * @param \RxConnexionBundle\Entity\EmailAttachmentFileEntity $emailAttachmentFile
     *
     * @return EmailEntity
     */
    public function addEmailAttachmentFile(\RxConnexionBundle\Entity\EmailAttachmentFileEntity $emailAttachmentFile)
    {
        $this->emailAttachmentFiles[] = $emailAttachmentFile;

        return $this;
    }

    /**
     * Remove emailAttachmentFile
     *
     * @param \RxConnexionBundle\Entity\EmailAttachmentFileEntity $emailAttachmentFile
     */
    public function removeEmailAttachmentFile(\RxConnexionBundle\Entity\EmailAttachmentFileEntity $emailAttachmentFile)
    {
        $this->emailAttachmentFiles->removeElement($emailAttachmentFile);
    }

    /**
     * Get emailAttachmentFiles
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getEmailAttachmentFiles()
    {
        return $this->emailAttachmentFiles;
    }
}
