<?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="frequently_asked_question_file")
 * @ORM\HasLifecycleCallbacks()
 */
 
class FrequentlyAskedQuestionFileEntity 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\FrequentlyAskedQuestionEntity", inversedBy="frequentlyAskedQuestionFiles")
     * @ORM\JoinColumn(name="frequently_asked_question_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $frequentlyAskedQuestion;

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

	/*--------------------------------------------------------------------------------------------------------*/
	/*					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/faq';
    }

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

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

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

        return $this;
    }

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

    /**
     * Set frequentlyAskedQuestion
     *
     * @param \RxConnexionBundle\Entity\FrequentlyAskedQuestionEntity $frequentlyAskedQuestion
     *
     * @return FrequentlyAskedQuestionFileEntity
     */
    public function setFrequentlyAskedQuestion(\RxConnexionBundle\Entity\FrequentlyAskedQuestionEntity $frequentlyAskedQuestion = null)
    {
        $this->frequentlyAskedQuestion = $frequentlyAskedQuestion;

        return $this;
    }

    /**
     * Get frequentlyAskedQuestion
     *
     * @return \RxConnexionBundle\Entity\FrequentlyAskedQuestionEntity
     */
    public function getFrequentlyAskedQuestion()
    {
        return $this->frequentlyAskedQuestion;
    }
}
