<?php

namespace RxConnexionBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use RxConnexionBundle\Entity\Base\BaseEntity;

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\HealthFacilityPhotoRepository")
 * @ORM\Table(name="health_facility_photo")
 */

class HealthFacilityPhotoEntity extends BaseEntity
{

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

    /**
     * @ORM\Column(name="photo_description", type="string", nullable=true)
     */
    protected $photoDescription;

    /**
     * @ORM\Column(name="parsed_photo_description", type="string", nullable=true)
     */
    protected $parsedPhotoDescription;

    /**
     * @ORM\ManyToOne(targetEntity="HealthFacilityEntity", inversedBy="photos")
     * @ORM\JoinColumn(name="health_facility_id", referencedColumnName="id")
     */
    protected $healthFacility;

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

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

        $parsedPhotoDescription = $this->getParsedPhotoDescription();
        $file = $this->getUploadPhotoRootDir() . '/' . $parsedPhotoDescription;

        if(!empty($parsedPhotoDescription) && file_exists($file)) unlink($file);
    }

    /*--------------------------------------------------------------------------------------------------------*/
    /*					User Defined Setters and Getters													  */
    /*--------------------------------------------------------------------------------------------------------*/

    /**
     * Get uploadPhotoDir
     *
     * @return string
     */
    public function getUploadPhotoDir() {

        return 'uploads/healthFacility/photos';
    }

    /**
     * Get uploadPhotoRootDir
     *
     * @return string
     */
    public function getUploadPhotoRootDir() {

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

    /**
     * get photoWebPath
     *
     * @return string
     */
    public function getPhotoWebPath() {

        $parsedPhotoDescription = $this->getParsedPhotoDescription();
        $file = $this->getUploadPhotoRootDir() . '/' . $parsedPhotoDescription;

        if(!empty($parsedPhotoDescription) && file_exists($file))
            return $this->getUploadPhotoDir() . '/' . $parsedPhotoDescription;
        else
            return '';
    }

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


    /**
     * Set photoDescription
     *
     * @param string $photoDescription
     *
     * @return HealthFacilityPhotoEntity
     */
    public function setPhotoDescription($photoDescription)
    {
        $this->photoDescription = $photoDescription;

        return $this;
    }

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

    /**
     * Set parsedPhotoDescription
     *
     * @param string $parsedPhotoDescription
     *
     * @return HealthFacilityPhotoEntity
     */
    public function setParsedPhotoDescription($parsedPhotoDescription)
    {
        $this->parsedPhotoDescription = $parsedPhotoDescription;

        return $this;
    }

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

    /**
     * Set healthFacility
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility
     *
     * @return HealthFacilityPhotoEntity
     */
    public function setHealthFacility(\RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility = null)
    {
        $this->healthFacility = $healthFacility;

        return $this;
    }

    /**
     * Get healthFacility
     *
     * @return \RxConnexionBundle\Entity\HealthFacilityEntity
     */
    public function getHealthFacility()
    {
        return $this->healthFacility;
    }

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

        return $this;
    }

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