<?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\PatientLetterFileRepository")
 * @ORM\Table(name="patient_letter_file")
 * @ORM\HasLifecycleCallbacks()
 */
 
class PatientLetterFileEntity 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="PatientLetterEntity", inversedBy="patientLetterFiles")
     * @ORM\JoinColumn(name="patient_letter_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $patientLetter;

    /**
     * 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->patientLetter = $data->getPatientLetter();
    	}
    }

	/*--------------------------------------------------------------------------------------------------------*/
	/*					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/patient/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 PatientLetterFileEntity
     */
    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 PatientLetterFileEntity
     */
    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 PatientLetterFileEntity
     */
    public function setIsDeleted($isDeleted)
    {
        $this->isDeleted = $isDeleted;

        return $this;
    }

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

    /**
     * Set patientLetter
     *
     * @param \RxConnexionBundle\Entity\PatientLetterEntity $patientLetter
     *
     * @return PatientLetterFileEntity
     */
    public function setPatientLetter(\RxConnexionBundle\Entity\PatientLetterEntity $patientLetter = null)
    {
        $this->patientLetter = $patientLetter;

        return $this;
    }

    /**
     * Get patientLetter
     *
     * @return \RxConnexionBundle\Entity\PatientLetterEntity
     */
    public function getPatientLetter()
    {
        return $this->patientLetter;
    }
}
