<?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\FaxingOptionRepository")
 * @ORM\Table(name="faxing_option")
 */

class FaxingOptionEntity extends BaseEntity
{
    const FAXING_OPTION_STATUS_PENDING = 'Pending';
    const FAXING_OPTION_STATUS_SENT = 'Sent';

    const FAXING_OPTION_TEMPLATE_DSRC = 'Dynamic Sleep and Respiratory Care';
    const FAXING_OPTION_TEMPLATE_FRSC = 'Freedom Respiratory & Sleep Centre';
    const FAXING_OPTION_TEMPLATE_PSS = 'Perfect Sleep Solutions WC Inc.';

    const FAXING_OPTION_FAX_TO_INSURANCE = 'Insurance';
    const FAXING_OPTION_FAX_TO_REFERRING_PROVIDER = 'Referring Physician';
    const FAXING_OPTION_FAX_TO_REFERRING_HEALTH_FACILITY = 'Referring Affiliate';

    const FAX_COVER_SELECTION_1 = '1';
    const FAX_COVER_SELECTION_2 = '2';
    const FAX_COVER_SELECTION_3 = '3';
    const FAX_COVER_SELECTION_4 = '4';

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

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

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

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

    /**
     * @ORM\Column(name="fax_cover_selection", type="string", length = 20, nullable=true)
     */
    protected $faxCoverSelection;

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

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\PatientEntity", inversedBy="faxingOptions")
     * @ORM\JoinColumn(name="patient_id", referencedColumnName="id", nullable=true)
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $patient;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\ReferringHealthFacilityEntity", inversedBy="faxingOptions")
     * @ORM\JoinColumn(name="referring_health_facility_id", referencedColumnName="id", nullable=true)
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $referringHealthFacility;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\ReferringProviderEntity", inversedBy="faxingOptions")
     * @ORM\JoinColumn(name="referring_provider_id", referencedColumnName="id", nullable=true)
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $referringProvider;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\InsuranceEntity", inversedBy="faxingOptions")
     * @ORM\JoinColumn(name="insurance_id", referencedColumnName="id", nullable=true)
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $insurance;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\CompanyEntity", inversedBy="faxingOptions")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true)
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $company;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\FaxingOptionFileEntity", mappedBy="faxingOption", cascade={"remove"})
     */
    protected $faxingOptionFiles;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\FaxingOptionOutboundEntity", mappedBy="faxingOption", cascade={"remove"})
     */
    protected $faxingOptionOutbound;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\FaxingOptionConfirmationEntity", mappedBy="faxingOption", cascade={"remove"})
     */
    protected $faxingOptionConfirmations;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\PatientFileEntity", mappedBy="faxingOption", cascade={"remove"})
     */
    protected $patientFiles;

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


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

    public function __construct($data=null)
    {
        if(!is_null($data)) {
            $this->id = $data->getId();
            $this->faxNo = $data->getFaxNo();
            $this->birthDate = $data->getBirthDateAsString();
            $this->patient = $data->getPatient();
            $this->referringHealthFacility = $data->getReferringHealthFacility();
            $this->faxCoverSelection = $data->getFaxCoverSelection();
            $this->faxTo = $data->getFaxTo();
            $this->company = $data->getCompany();
        }

        $this->faxingOptionFiles = new ArrayCollection();
        $this->faxingOptionOutbound = new ArrayCollection();
        $this->patientFiles = 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 faxNo
     *
     * @param string $faxNo
     *
     * @return FaxingOptionEntity
     */
    public function setFaxNo($faxNo)
    {
        $this->faxNo = $faxNo;

        return $this;
    }

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

    /**
     * Set customBody
     *
     * @param string $customBody
     *
     * @return FaxingOptionEntity
     */
    public function setCustomBody($customBody)
    {
        $this->customBody = $customBody;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set birthDate
     *
     * @param \DateTime $birthDate
     *
     * @return FaxingOptionEntity
     */
    public function setBirthDate($birthDate)
    {
        $this->birthDate =  empty($birthDate) ? NULL : new \DateTime($birthDate);

        return $this;
    }

    /**
     * Get birthDate
     *
     * @return \DateTime
     */
    public function getBirthDate()
    {
        return $this->birthDate;
    }

    /**
     * Set patient
     *
     * @param \RxConnexionBundle\Entity\PatientEntity $patient
     *
     * @return FaxingOptionEntity
     */
    public function setPatient(\RxConnexionBundle\Entity\PatientEntity $patient = null)
    {
        $this->patient = $patient;

        return $this;
    }

    /**
     * Get patient
     *
     * @return \RxConnexionBundle\Entity\PatientEntity
     */
    public function getPatient()
    {
        return $this->patient;
    }

    /**
     * Add faxingOptionFile
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionFileEntity $faxingOptionFile
     *
     * @return FaxingOptionEntity
     */
    public function addFaxingOptionFile(\RxConnexionBundle\Entity\FaxingOptionFileEntity $faxingOptionFile)
    {
        $this->faxingOptionFiles[] = $faxingOptionFile;

        return $this;
    }

    /**
     * Remove faxingOptionFile
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionFileEntity $faxingOptionFile
     */
    public function removeFaxingOptionFile(\RxConnexionBundle\Entity\FaxingOptionFileEntity $faxingOptionFile)
    {
        $this->faxingOptionFiles->removeElement($faxingOptionFile);
    }

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

    /**
     * Add faxingOptionOutbound
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionOutboundEntity $faxingOptionOutbound
     *
     * @return FaxingOptionEntity
     */
    public function addFaxingOptionOutbound(\RxConnexionBundle\Entity\FaxingOptionOutboundEntity $faxingOptionOutbound)
    {
        $this->faxingOptionOutbound[] = $faxingOptionOutbound;

        return $this;
    }

    /**
     * Remove faxingOptionOutbound
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionOutboundEntity $faxingOptionOutbound
     */
    public function removeFaxingOptionOutbound(\RxConnexionBundle\Entity\FaxingOptionOutboundEntity $faxingOptionOutbound)
    {
        $this->faxingOptionOutbound->removeElement($faxingOptionOutbound);
    }

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

    /**
     * Set status
     *
     * @param string $status
     *
     * @return FaxingOptionEntity
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

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

    /**
     * Set referringHealthFacility
     *
     * @param \RxConnexionBundle\Entity\ReferringHealthFacilityEntity $referringHealthFacility
     *
     * @return FaxingOptionEntity
     */
    public function setReferringHealthFacility(\RxConnexionBundle\Entity\ReferringHealthFacilityEntity $referringHealthFacility = null)
    {
        $this->referringHealthFacility = $referringHealthFacility;

        return $this;
    }

    /**
     * Get referringHealthFacility
     *
     * @return \RxConnexionBundle\Entity\ReferringHealthFacilityEntity
     */
    public function getReferringHealthFacility()
    {
        return $this->referringHealthFacility;
    }

    /**
     * Set company
     *
     * @param string $company
     *
     * @return FaxingOptionEntity
     */
    public function setCompany($company)
    {
        $this->company = $company;

        return $this;
    }

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

    /**
     * Set template
     *
     * @param string $template
     *
     * @return FaxingOptionEntity
     */
    public function setTemplate($template)
    {
        $this->template = $template;

        return $this;
    }

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

    /**
     * Set name
     *
     * @param string $name
     *
     * @return FaxingOptionEntity
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set page
     *
     * @param string $page
     *
     * @return FaxingOptionEntity
     */
    public function setPage($page)
    {
        $this->page = $page;

        return $this;
    }

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

    /**
     * Set faxCoverSelection
     *
     * @param string $faxCoverSelection
     *
     * @return FaxingOptionEntity
     */
    public function setFaxCoverSelection($faxCoverSelection)
    {
        $this->faxCoverSelection = $faxCoverSelection;

        return $this;
    }

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

    /**
     * Add faxingOptionConfirmation
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionConfirmationEntity $faxingOptionConfirmation
     *
     * @return FaxingOptionEntity
     */
    public function addFaxingOptionConfirmation(\RxConnexionBundle\Entity\FaxingOptionConfirmationEntity $faxingOptionConfirmation)
    {
        $this->faxingOptionConfirmations[] = $faxingOptionConfirmation;

        return $this;
    }

    /**
     * Remove faxingOptionConfirmation
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionConfirmationEntity $faxingOptionConfirmation
     */
    public function removeFaxingOptionConfirmation(\RxConnexionBundle\Entity\FaxingOptionConfirmationEntity $faxingOptionConfirmation)
    {
        $this->faxingOptionConfirmations->removeElement($faxingOptionConfirmation);
    }

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

    /**
     * Set faxTo
     *
     * @param string $faxTo
     *
     * @return FaxingOptionEntity
     */
    public function setFaxTo($faxTo)
    {
        $this->faxTo = $faxTo;

        return $this;
    }

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

    /**
     * Set referringProvider
     *
     * @param \RxConnexionBundle\Entity\ReferringProviderEntity $referringProvider
     *
     * @return FaxingOptionEntity
     */
    public function setReferringProvider(\RxConnexionBundle\Entity\ReferringProviderEntity $referringProvider = null)
    {
        $this->referringProvider = $referringProvider;

        return $this;
    }

    /**
     * Get referringProvider
     *
     * @return \RxConnexionBundle\Entity\ReferringProviderEntity
     */
    public function getReferringProvider()
    {
        return $this->referringProvider;
    }

    /**
     * Set insurance
     *
     * @param \RxConnexionBundle\Entity\InsuranceEntity $insurance
     *
     * @return FaxingOptionEntity
     */
    public function setInsurance(\RxConnexionBundle\Entity\InsuranceEntity $insurance = null)
    {
        $this->insurance = $insurance;

        return $this;
    }

    /**
     * Get insurance
     *
     * @return \RxConnexionBundle\Entity\InsuranceEntity
     */
    public function getInsurance()
    {
        return $this->insurance;
    }

    /**
     * Add patientFile
     *
     * @param \RxConnexionBundle\Entity\PatientFileEntity $patientFile
     *
     * @return FaxingOptionEntity
     */
    public function addPatientFile(\RxConnexionBundle\Entity\PatientFileEntity $patientFile)
    {
        $this->patientFiles[] = $patientFile;

        return $this;
    }

    /**
     * Remove patientFile
     *
     * @param \RxConnexionBundle\Entity\PatientFileEntity $patientFile
     */
    public function removePatientFile(\RxConnexionBundle\Entity\PatientFileEntity $patientFile)
    {
        $this->patientFiles->removeElement($patientFile);
    }

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