<?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;

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\GeneralUserRepository")
 * @ORM\Table(name="general_user")
 */

class GeneralUserEntity extends BaseEntity
{

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * @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="CityEntity", inversedBy="generalUsers")
     * @ORM\JoinColumn(name="city_id", referencedColumnName="id")
     */
    protected $city;

    /**
     * @ORM\OneToMany(targetEntity="PatientEntity", mappedBy="generalUser")
     */
    protected $patients;

    /**
     * @ORM\OneToMany(targetEntity="HealthFacilityEntity", mappedBy="generalUser")
     */
    protected $healthFacilities;

    /**
     * @ORM\OneToMany(targetEntity="ScheduleRequestEntity", mappedBy="generalUser")
     */
    protected $scheduleRequests;

    /**
     * @ORM\OneToMany(targetEntity="GeneralUserHealthFacilityEntity", mappedBy="generalUser")
     */
    protected $generalUserHealthFacilities;

    public function __construct()
    {
        $this->patients = new ArrayCollection();
        $this->healthFacilities = new ArrayCollection();
        $this->scheduleRequests = new ArrayCollection();
        $this->generalUserHealthFacilities = new ArrayCollection();
    }

    /**
     * Set createdAt
     *
     * @ORM\PrePersist
     */
    public function setCreatedAtValue()
    {
        $this->createdBy = isSet($_COOKIE['username']) ? $_COOKIE['username'] : 'System';
        $this->createdAt = new \DateTime();
        $this->createdBy = isSet($_COOKIE['username']) ? $_COOKIE['username'] : 'System';
        $this->updatedAt = new \DateTime();
        $this->fullName = ucfirst($this->firstName) . ' ' . ucfirst($this->lastName);
        $this->updateAddress();
    }

    /**
     * Set updatedAt
     *
     * @ORM\PreUpdate
     */
    public function setUpdatedAtValue()
    {
        $this->updatedBy = isSet($_COOKIE['username']) ? $_COOKIE['username'] : 'System';
        $this->updatedAt = new \DateTime();
        $this->fullName = ucfirst($this->firstName) . ' ' . ucfirst($this->lastName);
        $this->updateAddress();
    }

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

    /**
     * Concatenate address
     */
    public function updateAddress() {
        $this->address = $this->street . ' ' . ($this->city ? $this->city->getDescription() . ', ' . $this->city->getProvince()->getDescription() . ' ' . $this->city->getProvince()->getCountry()->getDescription()  . ' ' : '') . $this->zipCode;
    }

    /*--------------------------------------------------------------------------------------------------------*/
    /*					Use Defined Setters and Getters														  */
    /*--------------------------------------------------------------------------------------------------------*/

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

        return 'uploads/patient/photo';
    }

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

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

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

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

        if(!empty($parsedPhotoDescription) && file_exists($file))
            return $this->getUploadPhotoDir() . '/' . $parsedPhotoDescription;
        else
            return '/bundles/rxgrab/images/default-photo-ken.jpg';
    }

    /**
     * Get age
     *
     * @return integer
     */
    public function getAge()
    {
        $dateTimeToStringTransformer = new DateTimeToStringTransformer();
        $year = date('Y');
        $month = date('m');
        $day = date('d');
        $birth_date_long = strtotime($dateTimeToStringTransformer->transform($this->birthDate));
        $birth_date_year = date('Y', $birth_date_long);
        $birth_date_month = date('m', $birth_date_long);
        $birth_date_day = date('d', $birth_date_long);
        $age = $year - $birth_date_year;
        if($month<$birth_date_month || ($month==$birth_date_month && $day < $birth_date_day))
            $age--;
        return $age;
    }

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

    /**
     * Set username
     *
     * @param string $username
     *
     * @return GeneralUserEntity
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

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

    /**
     * Set password
     *
     * @param string $password
     *
     * @return GeneralUserEntity
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

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

    /**
     * Set firstName
     *
     * @param string $firstName
     *
     * @return GeneralUserEntity
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;

        return $this;
    }

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

    /**
     * Set middleName
     *
     * @param string $middleName
     *
     * @return GeneralUserEntity
     */
    public function setMiddleName($middleName)
    {
        $this->middleName = $middleName;

        return $this;
    }

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

    /**
     * Set lastName
     *
     * @param string $lastName
     *
     * @return GeneralUserEntity
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }

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

    /**
     * Set fullName
     *
     * @param string $fullName
     *
     * @return GeneralUserEntity
     */
    public function setFullName($fullName)
    {
        $this->fullName = $fullName;

        return $this;
    }

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

    /**
     * Set gender
     *
     * @param string $gender
     *
     * @return GeneralUserEntity
     */
    public function setGender($gender)
    {
        $this->gender = $gender;

        return $this;
    }

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

    /**
     * Set birthDate
     *
     * @param \DateTime $birthDate
     *
     * @return GeneralUserEntity
     */
    public function setBirthDate($birthDate)
    {
        $this->birthDate = $birthDate;

        return $this;
    }

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

    /**
     * Set address
     *
     * @param string $address
     *
     * @return GeneralUserEntity
     */
    public function setAddress($address)
    {
        $this->address = $address;

        return $this;
    }

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

    /**
     * Set street
     *
     * @param string $street
     *
     * @return GeneralUserEntity
     */
    public function setStreet($street)
    {
        $this->street = $street;

        return $this;
    }

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

    /**
     * Set zipCode
     *
     * @param string $zipCode
     *
     * @return GeneralUserEntity
     */
    public function setZipCode($zipCode)
    {
        $this->zipCode = $zipCode;

        return $this;
    }

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

    /**
     * Set mobileNo
     *
     * @param string $mobileNo
     *
     * @return GeneralUserEntity
     */
    public function setMobileNo($mobileNo)
    {
        $this->mobileNo = $mobileNo;

        return $this;
    }

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

    /**
     * Set mobileNoTwo
     *
     * @param string $mobileNoTwo
     *
     * @return GeneralUserEntity
     */
    public function setMobileNoTwo($mobileNoTwo)
    {
        $this->mobileNoTwo = $mobileNoTwo;

        return $this;
    }

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

    /**
     * Set mobileNoThree
     *
     * @param string $mobileNoThree
     *
     * @return GeneralUserEntity
     */
    public function setMobileNoThree($mobileNoThree)
    {
        $this->mobileNoThree = $mobileNoThree;

        return $this;
    }

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

    /**
     * Set phoneNo
     *
     * @param string $phoneNo
     *
     * @return GeneralUserEntity
     */
    public function setPhoneNo($phoneNo)
    {
        $this->phoneNo = $phoneNo;

        return $this;
    }

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

    /**
     * Set phoneNoTwo
     *
     * @param string $phoneNoTwo
     *
     * @return GeneralUserEntity
     */
    public function setPhoneNoTwo($phoneNoTwo)
    {
        $this->phoneNoTwo = $phoneNoTwo;

        return $this;
    }

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

    /**
     * Set phoneNoThree
     *
     * @param string $phoneNoThree
     *
     * @return GeneralUserEntity
     */
    public function setPhoneNoThree($phoneNoThree)
    {
        $this->phoneNoThree = $phoneNoThree;

        return $this;
    }

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

    /**
     * Set email
     *
     * @param string $email
     *
     * @return GeneralUserEntity
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

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

    /**
     * Set emailTwo
     *
     * @param string $emailTwo
     *
     * @return GeneralUserEntity
     */
    public function setEmailTwo($emailTwo)
    {
        $this->emailTwo = $emailTwo;

        return $this;
    }

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

    /**
     * Set emailThree
     *
     * @param string $emailThree
     *
     * @return GeneralUserEntity
     */
    public function setEmailThree($emailThree)
    {
        $this->emailThree = $emailThree;

        return $this;
    }

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

    /**
     * Set photoDescription
     *
     * @param string $photoDescription
     *
     * @return GeneralUserEntity
     */
    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 GeneralUserEntity
     */
    public function setParsedPhotoDescription($parsedPhotoDescription)
    {
        $this->parsedPhotoDescription = $parsedPhotoDescription;

        return $this;
    }

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

    /**
     * Set city
     *
     * @param \RxConnexionBundle\Entity\CityEntity $city
     *
     * @return GeneralUserEntity
     */
    public function setCity(\RxConnexionBundle\Entity\CityEntity $city = null)
    {
        $this->city = $city;

        return $this;
    }

    /**
     * Get city
     *
     * @return \RxConnexionBundle\Entity\CityEntity
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Add patient
     *
     * @param \RxConnexionBundle\Entity\PatientEntity $patient
     *
     * @return GeneralUserEntity
     */
    public function addPatient(\RxConnexionBundle\Entity\PatientEntity $patient)
    {
        $this->patients[] = $patient;

        return $this;
    }

    /**
     * Remove patient
     *
     * @param \RxConnexionBundle\Entity\PatientEntity $patient
     */
    public function removePatient(\RxConnexionBundle\Entity\PatientEntity $patient)
    {
        $this->patients->removeElement($patient);
    }

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

    /**
     * Add healthFacility
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility
     *
     * @return GeneralUserEntity
     */
    public function addHealthFacility(\RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility)
    {
        $this->healthFacilities[] = $healthFacility;

        return $this;
    }

    /**
     * Remove healthFacility
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility
     */
    public function removeHealthFacility(\RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility)
    {
        $this->healthFacilities->removeElement($healthFacility);
    }

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

    /**
     * Add scheduleRequest
     *
     * @param \RxConnexionBundle\Entity\ScheduleRequestEntity $scheduleRequest
     *
     * @return GeneralUserEntity
     */
    public function addScheduleRequest(\RxConnexionBundle\Entity\ScheduleRequestEntity $scheduleRequest)
    {
        $this->scheduleRequests[] = $scheduleRequest;

        return $this;
    }

    /**
     * Remove scheduleRequest
     *
     * @param \RxConnexionBundle\Entity\ScheduleRequestEntity $scheduleRequest
     */
    public function removeScheduleRequest(\RxConnexionBundle\Entity\ScheduleRequestEntity $scheduleRequest)
    {
        $this->scheduleRequests->removeElement($scheduleRequest);
    }

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

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

    /**
     * Set createdBy
     *
     * @param string $createdBy
     *
     * @return GeneralUserEntity
     */
    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 GeneralUserEntity
     */
    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 GeneralUserEntity
     */
    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 GeneralUserEntity
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

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

    /**
     * Add generalUserHealthFacility
     *
     * @param \RxConnexionBundle\Entity\GeneralUserHealthFacilityEntity $generalUserHealthFacility
     *
     * @return GeneralUserEntity
     */
    public function addGeneralUserHealthFacility(\RxConnexionBundle\Entity\GeneralUserHealthFacilityEntity $generalUserHealthFacility)
    {
        $this->generalUserHealthFacilities[] = $generalUserHealthFacility;

        return $this;
    }

    /**
     * Remove generalUserHealthFacility
     *
     * @param \RxConnexionBundle\Entity\GeneralUserHealthFacilityEntity $generalUserHealthFacility
     */
    public function removeGeneralUserHealthFacility(\RxConnexionBundle\Entity\GeneralUserHealthFacilityEntity $generalUserHealthFacility)
    {
        $this->generalUserHealthFacilities->removeElement($generalUserHealthFacility);
    }

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