<?php

namespace RxConnexionBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
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\EmailAttachmentFileRepository")
 * @ORM\Table(name="email_attachment_file")
 * @ORM\HasLifecycleCallbacks()
 */
 
class EmailAttachmentFileEntity extends BaseEntity
{
    /**
     * @ORM\Column(type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
	protected $id;

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\EmailEntity", inversedBy="emailAttachmentFiles")
     * @ORM\JoinColumn(name="email_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $email;

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

    /**
     * Remove the file from the disk
     *
     * @ORM\PreRemove
     */
    public function removeFile() {

        $file = $this->getUploadRootDir() . '/' . $this->parsedFileDesc;
        if(!empty($this->parsedFileDesc) && file_exists($file)) unlink($file);
    }

    public function __construct($data=null)
    {
    	if(!is_null($data)) {
    		$this->id = $data->getId();
    		$this->fileDesc = $data->getFileDesc();
    		$this->parsedFileDesc = $data->getParsedFileDesc();
    		$this->email = $data->getEmail();
    	}
    }

	/*--------------------------------------------------------------------------------------------------------*/
	/*					File Defined Setters and Getters													  */
	/*--------------------------------------------------------------------------------------------------------*/

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

        return base64_encode($this->id);
    }

    /**
     * Get uploadDir
     *
     * @return string
     */
    public function getUploadDir() {

        return 'uploads/fax_option_file/attachments';
    }

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

        return __DIR__ . '/../../../web/' . $this->getUploadDir();
    }

    /**
     * Get uploadDir
     *
     * @return string
     */
    public function getUploadDir2() {

        return 'uploads/covertedpdf';
    }

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

        return __DIR__ . '/../../../web/' . $this->getUploadDir2();
    }

    /**
     * get fileWebPath
     *
     * @return string
     */
    public function getFileWebPath() {

        $parsedDesc = $this->parsedFileDesc;
        $file = $this->getUploadRootDir() . '/' . $parsedDesc;

        if(!empty($parsedDesc) && file_exists($file)) {
            return '/' . $this->getUploadDir() . '/' . $parsedDesc;
        } else {
            return '';
        }
    }
    
	/*--------------------------------------------------------------------------------------------------------*/
	/*					Setters and Getters																	  */
	/*--------------------------------------------------------------------------------------------------------*/


    /**
     * Set fileDesc
     *
     * @param string $fileDesc
     *
     * @return FaxingOptionFileEntity
     */
    public function setFileDesc($fileDesc)
    {
        $this->fileDesc = $fileDesc;

        return $this;
    }

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

    /**
     * Set parsedFileDesc
     *
     * @param string $parsedFileDesc
     *
     * @return FaxingOptionFileEntity
     */
    public function setParsedFileDesc($parsedFileDesc)
    {
        $this->parsedFileDesc = $parsedFileDesc;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set faxingOption
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionEntity $faxingOption
     *
     * @return FaxingOptionFileEntity
     */
    public function setFaxingOption(\RxConnexionBundle\Entity\FaxingOptionEntity $faxingOption = null)
    {
        $this->faxingOption = $faxingOption;

        return $this;
    }

    /**
     * Get faxingOption
     *
     * @return \RxConnexionBundle\Entity\FaxingOptionEntity
     */
    public function getFaxingOption()
    {
        return $this->faxingOption;
    }

    /**
     * Set email
     *
     * @param \RxConnexionBundle\Entity\EmailEntity $email
     *
     * @return EmailAttachmentFileEntity
     */
    public function setEmail(\RxConnexionBundle\Entity\EmailEntity $email = null)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return \RxConnexionBundle\Entity\EmailEntity
     */
    public function getEmail()
    {
        return $this->email;
    }
}
