/// <summary>
        /// Gets the time stamp of the last administered prescription.
        /// </summary>
        /// <returns></returns>
        private DateTime?ReturnLastAdministeredPrescription()
        {
            if (AdministeredPrescriptions == null || !AdministeredPrescriptions.Any())
            {
                return(null);
            }

            PrescriptionGroup group = AdministeredPrescriptions.OrderBy(grp => grp.Prescriptions[0].AdministeredOn).Last();

            return(group.Prescriptions[0].AdministeredOn ?? null);
        }
        /// <summary>
        /// Gets all of the prescriptions from the IMS API and groups them into their schedules.
        /// </summary>
        /// <param name="imsId">ID of admitted patient at the IMS.</param>
        /// <returns>An observable collection of grouped prescriptions.</returns>
        protected async Task <ObservableCollection <PrescriptionGroup> > GetPrescriptionsAsync(int imsId, PatientAdministrationStatus?status = null)
        {
            imsSvc = new ImsDataService(ApiSettings);

            List <Prescription> medicines = await imsSvc.GetPrescriptionsAsync(imsId);

            var grpedMedicines = from meds in medicines
                                 group meds by meds.Schedule into grp
                                 orderby grp.Key
                                 select new { TimeStamp = grp.Key, Items = grp };

            var prescriptionsGrouped = new ObservableCollection <PrescriptionGroup>();

            foreach (var groups in grpedMedicines)
            {
                PrescriptionGroup grp = new PrescriptionGroup();

                grp.TimeStamp = groups.TimeStamp;

                ObservableCollection <Prescription> sortedMedicines = new ObservableCollection <Prescription>();
                foreach (var meds in groups.Items)
                {
                    sortedMedicines.Add(meds);
                }
                grp.Prescriptions = sortedMedicines;

                prescriptionsGrouped.Add(grp);
            }

            if (status.HasValue)
            {
                switch (status.Value)
                {
                case PatientAdministrationStatus.MedicineSelected:
                    var isSelected = prescriptionsGrouped.Where(grp => grp.IsSelected).ToList();
                    return(new ObservableCollection <PrescriptionGroup>(isSelected));

                case PatientAdministrationStatus.MedicineLoaded:
                    var isLoaded = prescriptionsGrouped.Where(grp => grp.IsLoaded).ToList();
                    return(new ObservableCollection <PrescriptionGroup>(isLoaded));

                case PatientAdministrationStatus.MedicineAdministered:
                    var isAdministered = prescriptionsGrouped.Where(grp => grp.IsAdministered).ToList();
                    return(new ObservableCollection <PrescriptionGroup>(isAdministered));

                default:
                    return(prescriptionsGrouped);
                }
            }
            else
            {
                return(prescriptionsGrouped);
            }
        }
        /// <summary>
        /// Removes the prescription group.
        /// </summary>
        /// <param name="selectedItems">The prescription group</param>
        public async void DeletePrescriptionGroup(object selectedItems)
        {
            try
            {
                PrescriptionGroup group = selectedItems as PrescriptionGroup;
                var count = group.Prescriptions.Count;

                await imsSvc.DeletePrescriptionGroupAsync(group.Prescriptions);

                AllPrescriptions.Remove(group);
                Prescriptions.Remove(group);
            }
            catch (Exception e)
            {
                MessengerInstance.Send(new ExceptionDialogMessage(e));
            }
        }
        /// <summary>
        /// Picks a prescription group for administration.
        /// </summary>
        /// <param name="user">The currently logged user</param>
        /// <param name="prescriptionGroup">The selected prescription group</param>
        /// <param name="isChecked">Indicates whether the control associated with the action is checked</param>
        public async void SelectPrescriptionGroup(Personnel user, object prescriptionGroup, bool isChecked)
        {
            try
            {
                IsLoading = true;
                PrescriptionGroup group = prescriptionGroup as PrescriptionGroup;

                if (SelectedPrescriptions == null)
                {
                    SelectedPrescriptions = new ObservableCollection <PrescriptionGroup>();
                }

                if (loggedUser == null)
                {
                    loggedUser = user;
                }

                if (isChecked)
                {
                    SelectedPrescriptions.Add(group);
                    foreach (var item in group.Prescriptions)
                    {
                        item.SelectedOn = DateTime.Now;
                        item.Selector   = loggedUser.Username;
                    }
                }
                else
                {
                    SelectedPrescriptions.Remove(group);
                    foreach (var item in group.Prescriptions)
                    {
                        item.SelectedOn = null;
                        item.Selector   = null;
                    }
                }

                await imsSvc.UpdatePrescriptionsAsync(AdmittedPatient.ID, group.Prescriptions);

                IsLoading = false;
            }
            catch (Exception)
            {
                throw;
            }
        }