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

class ScheduleEntity extends BaseEntity
{
    const START_TIME_PERIOD_AM = 'AM';
    const START_TIME_PERIOD_PM = 'PM';
    const END_TIME_PERIOD_AM = 'AM';
    const END_TIME_PERIOD_PM = 'PM';
    const FAX_COVER_SELECTION_1 = '1';
    const FAX_COVER_SELECTION_2 = '2';
    const FAX_COVER_SELECTION_3 = '3';
    const FAX_COVER_SELECTION_4 = '4';
    const FAX_BODY_SELECTION_1 = '1';
    const FAX_BODY_SELECTION_2 = '2';
    const FAX_BODY_SELECTION_3 = '3';
    const FAX_BODY_SELECTION_4 = '4';
    const FAX_BODY_SELECTION_5 = '5';

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

    /**
     * @ORM\Column(name="start_time_hour", type="integer", nullable=true)
     */
    protected $startTimeHour;

    /**
     * @ORM\Column(name="start_time_minute", type="integer", nullable=true)
     */
    protected $startTimeMinute;

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

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

    /**
     * @ORM\Column(name="end_time_hour", type="integer", nullable=true)
     */
    protected $endTimeHour;

    /**
     * @ORM\Column(name="end_time_minute", type="integer", nullable=true)
     */
    protected $endTimeMinute;

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

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

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

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

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

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

//    /**
//     * @ORM\ManyToOne(targetEntity="LocationEntity", inversedBy="schedules")
//     * @ORM\JoinColumn(name="location_id", referencedColumnName="id")
//     * @Groups("forAudit")
//     * @MaxDepth(1)
//     */
//    protected $location;

    /**
     * @ORM\ManyToOne(targetEntity="HealthFacilityEntity", inversedBy="schedules")
     * @ORM\JoinColumn(name="health_facility_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $healthFacility;

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

    /**
     * @ORM\ManyToOne(targetEntity="ScheduleStatusEntity", inversedBy="schedules")
     * @ORM\JoinColumn(name="schedule_status_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $scheduleStatus;

    /**
     * @ORM\ManyToOne(targetEntity="ProviderEntity", inversedBy="schedules")
     * @ORM\JoinColumn(name="provider_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $provider;

    /**
     * @ORM\ManyToOne(targetEntity="ChartTypeEntity", inversedBy="schedules")
     * @ORM\JoinColumn(name="chart_type_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $chartType;

    /**
     * @ORM\OneToOne(targetEntity="ScheduleRequestEntity", inversedBy="schedule")
     * @ORM\JoinColumn(name="schedule_request_id", referencedColumnName="id")
     * @Groups("forAudit")
     */
    protected $scheduleRequest;

    /**
     * @ORM\ManyToOne(targetEntity="NoteTemplateEntity", inversedBy="schedules")
     * @ORM\JoinColumn(name="note_template_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $noteTemplate;

    /**
     * @ORM\OneToOne(targetEntity="PatientChartEntity", mappedBy="schedule", cascade={"remove"})
     */
    protected $patientChart;

    /**
     * @ORM\OneToMany(targetEntity="AppointmentNoticeFaxEntity", mappedBy="schedule")
     */
    protected $appointmentNoticeFaxes;

    public function __construct()
    {
        $this->appointmentNoticeFaxes = new ArrayCollection();
    }

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

    /**
     * Set updatedAt
     *
     * @ORM\PreUpdate
     */
    public function setUpdatedAtValue()
    {
        parent::setUpdatedAtValue();
        $this->updateStartEndTime();
    }

    /**
     * Concatenate start and end time
     */
    public function updateStartEndTime() {

        $this->startTime = sprintf('%002d', $this->startTimeHour) . ':' . sprintf('%002d', $this->startTimeMinute) . ' ' . $this->startTimePeriod;
        $this->endTime = sprintf('%002d', $this->endTimeHour) . ':' . sprintf('%002d', $this->endTimeMinute) . ' ' . $this->endTimePeriod;

        return $this;
    }

    /*--------------------------------------------------------------------------------------------------------*/
    /*					Use Defined Setters and Getters														  */
    /*--------------------------------------------------------------------------------------------------------*/
    /**
     * Get scheduleDateAsString
     *
     * @return string
     */
    public function getScheduleDateAsString() {

        $dttst = new DateTimeToStringTransformer();
        return !empty($this->scheduleDate) ? date('m/d/Y', strtotime($dttst->transform($this->scheduleDate))) : '';
    }
    /*--------------------------------------------------------------------------------------------------------*/
    /*					Setters and Getters																	  */
    /*--------------------------------------------------------------------------------------------------------*/


    /**
     * Set startTime
     *
     * @param string $startTime
     *
     * @return ScheduleEntity
     */
    public function setStartTime($startTime)
    {
        $this->startTime = $startTime;

        return $this;
    }

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

    /**
     * Set startTimeHour
     *
     * @param integer $startTimeHour
     *
     * @return ScheduleEntity
     */
    public function setStartTimeHour($startTimeHour)
    {
        $this->startTimeHour = $startTimeHour;

        return $this;
    }

    /**
     * Get startTimeHour
     *
     * @return integer
     */
    public function getStartTimeHour()
    {
        return $this->startTimeHour;
    }

    /**
     * Set startTimeMinute
     *
     * @param integer $startTimeMinute
     *
     * @return ScheduleEntity
     */
    public function setStartTimeMinute($startTimeMinute)
    {
        $this->startTimeMinute = $startTimeMinute;

        return $this;
    }

    /**
     * Get startTimeMinute
     *
     * @return integer
     */
    public function getStartTimeMinute()
    {
        return $this->startTimeMinute;
    }

    /**
     * Set startTimePeriod
     *
     * @param string $startTimePeriod
     *
     * @return ScheduleEntity
     */
    public function setStartTimePeriod($startTimePeriod)
    {
        $this->startTimePeriod = $startTimePeriod;

        return $this;
    }

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

    /**
     * Set endTime
     *
     * @param string $endTime
     *
     * @return ScheduleEntity
     */
    public function setEndTime($endTime)
    {
        $this->endTime = $endTime;

        return $this;
    }

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

    /**
     * Set endTimeHour
     *
     * @param integer $endTimeHour
     *
     * @return ScheduleEntity
     */
    public function setEndTimeHour($endTimeHour)
    {
        $this->endTimeHour = $endTimeHour;

        return $this;
    }

    /**
     * Get endTimeHour
     *
     * @return integer
     */
    public function getEndTimeHour()
    {
        return $this->endTimeHour;
    }

    /**
     * Set endTimeMinute
     *
     * @param integer $endTimeMinute
     *
     * @return ScheduleEntity
     */
    public function setEndTimeMinute($endTimeMinute)
    {
        $this->endTimeMinute = $endTimeMinute;

        return $this;
    }

    /**
     * Get endTimeMinute
     *
     * @return integer
     */
    public function getEndTimeMinute()
    {
        return $this->endTimeMinute;
    }

    /**
     * Set endTimePeriod
     *
     * @param string $endTimePeriod
     *
     * @return ScheduleEntity
     */
    public function setEndTimePeriod($endTimePeriod)
    {
        $this->endTimePeriod = $endTimePeriod;

        return $this;
    }

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

    /**
     * Set scheduleDate
     *
     * @param \DateTime $scheduleDate
     *
     * @return ScheduleEntity
     */
    public function setScheduleDate($scheduleDate)
    {
        //$this->scheduleDate = empty($scheduleDate) ? NULL : new \DateTime($scheduleDate);
        $scheduleDateExploded = explode('/', $scheduleDate);
        if((count($scheduleDateExploded) == 3 && !checkdate($scheduleDateExploded[0], $scheduleDateExploded[1], $scheduleDateExploded[2])) || empty($scheduleDate)){
            $this->scheduleDate = NULL;
        }else{
            $this->scheduleDate = new \DateTime($scheduleDate);
        }

        return $this;
    }

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

    /**
     * Set note
     *
     * @param string $note
     *
     * @return ScheduleEntity
     */
    public function setNote($note)
    {
        $this->note = $note;

        return $this;
    }

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

    /**
     * Set healthFacility
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility
     *
     * @return ScheduleEntity
     */
    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 patient
     *
     * @param \RxConnexionBundle\Entity\PatientEntity $patient
     *
     * @return ScheduleEntity
     */
    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;
    }

    /**
     * Set scheduleStatus
     *
     * @param \RxConnexionBundle\Entity\ScheduleStatusEntity $scheduleStatus
     *
     * @return ScheduleEntity
     */
    public function setScheduleStatus(\RxConnexionBundle\Entity\ScheduleStatusEntity $scheduleStatus = null)
    {
        $this->scheduleStatus = $scheduleStatus;

        return $this;
    }

    /**
     * Get scheduleStatus
     *
     * @return \RxConnexionBundle\Entity\ScheduleStatusEntity
     */
    public function getScheduleStatus()
    {
        return $this->scheduleStatus;
    }

    /**
     * Set provider
     *
     * @param \RxConnexionBundle\Entity\ProviderEntity $provider
     *
     * @return ScheduleEntity
     */
    public function setProvider(\RxConnexionBundle\Entity\ProviderEntity $provider = null)
    {
        $this->provider = $provider;

        return $this;
    }

    /**
     * Get provider
     *
     * @return \RxConnexionBundle\Entity\ProviderEntity
     */
    public function getProvider()
    {
        return $this->provider;
    }

    /**
     * Set chartType
     *
     * @param \RxConnexionBundle\Entity\ChartTypeEntity $chartType
     *
     * @return ScheduleEntity
     */
    public function setChartType(\RxConnexionBundle\Entity\ChartTypeEntity $chartType = null)
    {
        $this->chartType = $chartType;

        return $this;
    }

    /**
     * Get chartType
     *
     * @return \RxConnexionBundle\Entity\ChartTypeEntity
     */
    public function getChartType()
    {
        return $this->chartType;
    }

    /**
     * Set scheduleRequest
     *
     * @param \RxConnexionBundle\Entity\ScheduleRequestEntity $scheduleRequest
     *
     * @return ScheduleEntity
     */
    public function setScheduleRequest(\RxConnexionBundle\Entity\ScheduleRequestEntity $scheduleRequest = null)
    {
        $this->scheduleRequest = $scheduleRequest;

        return $this;
    }

    /**
     * Get scheduleRequest
     *
     * @return \RxConnexionBundle\Entity\ScheduleRequestEntity
     */
    public function getScheduleRequest()
    {
        return $this->scheduleRequest;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set createdBy
     *
     * @param string $createdBy
     *
     * @return ScheduleEntity
     */
    public function setCreatedBy($createdBy)
    {
        $this->createdBy = $createdBy;

        return $this;
    }

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

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     *
     * @return ScheduleEntity
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

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

    /**
     * Set updatedBy
     *
     * @param string $updatedBy
     *
     * @return ScheduleEntity
     */
    public function setUpdatedBy($updatedBy)
    {
        $this->updatedBy = $updatedBy;

        return $this;
    }

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

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return ScheduleEntity
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

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

    /**
     * Set patientChart
     *
     * @param \RxConnexionBundle\Entity\PatientChartEntity $patientChart
     *
     * @return ScheduleEntity
     */
    public function setPatientChart(\RxConnexionBundle\Entity\PatientChartEntity $patientChart = null)
    {
        $this->patientChart = $patientChart;

        return $this;
    }

    /**
     * Get patientChart
     *
     * @return \RxConnexionBundle\Entity\PatientChartEntity
     */
    public function getPatientChart()
    {
        return $this->patientChart;
    }

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set faxBodySelection
     *
     * @param string $faxBodySelection
     *
     * @return ScheduleEntity
     */
    public function setFaxBodySelection($faxBodySelection)
    {
        $this->faxBodySelection = $faxBodySelection;

        return $this;
    }

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

    /**
     * Add appointmentNoticeFax
     *
     * @param \RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax
     *
     * @return ScheduleEntity
     */
    public function addAppointmentNoticeFax(\RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax)
    {
        $this->appointmentNoticeFaxes[] = $appointmentNoticeFax;

        return $this;
    }

    /**
     * Remove appointmentNoticeFax
     *
     * @param \RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax
     */
    public function removeAppointmentNoticeFax(\RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax)
    {
        $this->appointmentNoticeFaxes->removeElement($appointmentNoticeFax);
    }

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

    /**
     * Set noteTemplate
     *
     * @param \RxConnexionBundle\Entity\NoteTemplateEntity $noteTemplate
     *
     * @return ScheduleEntity
     */
    public function setNoteTemplate(\RxConnexionBundle\Entity\NoteTemplateEntity $noteTemplate = null)
    {
        $this->noteTemplate = $noteTemplate;

        return $this;
    }

    /**
     * Get noteTemplate
     *
     * @return \RxConnexionBundle\Entity\NoteTemplateEntity
     */
    public function getNoteTemplate()
    {
        return $this->noteTemplate;
    }
}
