private void OnCancel()
 {
     if (AddMode)
     {
         ScheduleCollection.Remove(SelectedSchedule);
         SelectedSchedule = null;
         AddMode          = false;
     }
     else if (EditMode)
     {
         SelectedSchedule.CancelEdit();
         EditMode = false;
     }
     else if (RemoveMode)
     {
         foreach (var item in ScheduleCollection)
         {
             if (item.IsChecked)
             {
                 item.IsChecked = false;
             }
         }
         RemoveMode = false;
     }
     CanUseDatagrid = true;
     CanEditFields  = false;
 }
        private async void OnSave()
        {
            if (AddMode)
            {
                var    schedule = Converter.ConvertFromScheduleModel(SelectedSchedule);
                int    id       = 0;
                bool   success  = true;
                string error    = "";
                OnInformationRequested("Loading...");
                await Task.Run(() =>
                {
                    try
                    {
                        using (Repository <Schedule> repo = new Repository <Schedule>())
                        {
                            var result = repo.InsertWithResult(schedule);
                            if (result.Status.Success)
                            {
                                id = result.Data.ID;
                            }
                            else
                            {
                                success = false;
                                error   = result.Status.ErrorMessage;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        success = false;
                        error   = $"An error occured.Message: {ex.Message}";
                    }
                });

                if (success)
                {
                    SelectedSchedule.Id = id;
                    OnInformationRequested("Data added successfully, refreshing service in the background...");
                    await Task.Run(() =>
                    {
                        var result = Controller.RefreshService();
                        if (!result.Success)
                        {
                            success = false;
                            error   = result.ErrorMessage;
                        }
                    });

                    if (success)
                    {
                        OnInformationRequested("Service refreshed successfully");
                    }
                    else
                    {
                        OnErrorOccured(error);
                    }
                }
                else
                {
                    ScheduleCollection.Remove(SelectedSchedule);
                    SelectedSchedule = null;
                    OnErrorOccured(error);
                }
                AddMode = false;
            }
            else if (EditMode)
            {
                var schedule = Converter.ConvertFromScheduleModel(SelectedSchedule);

                bool   success = true;
                string error   = "";
                OnInformationRequested("Loading...");
                await Task.Run(() =>
                {
                    try
                    {
                        using (Repository <Schedule> repo = new Repository <Schedule>())
                        {
                            var result = repo.Update(schedule);
                            if (!result.Success)
                            {
                                success = false;
                                error   = result.ErrorMessage;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        success = false;
                        error   = $"An error occured.Message: {ex.Message}";
                    }
                });

                if (success)
                {
                    SelectedSchedule.EndEdit();
                    OnInformationRequested("Data updated successfully, refreshing service in the background...");
                    await Task.Run(() =>
                    {
                        var result = Controller.RefreshService();
                        if (!result.Success)
                        {
                            success = false;
                            error   = result.ErrorMessage;
                        }
                    });

                    if (success)
                    {
                        OnInformationRequested("Service refreshed successfully");
                    }
                    else
                    {
                        OnErrorOccured(error);
                    }
                }
                else
                {
                    SelectedSchedule.CancelEdit();
                    OnErrorOccured(error);
                }
                EditMode = false;
            }
            else if (RemoveMode)
            {
                var temp = from item in ScheduleCollection
                           where item.IsChecked
                           select item;
                var    schedulesToRemove = temp.Select(x => Converter.ConvertFromScheduleModel(x));
                bool   success           = true;
                string error             = "";
                OnInformationRequested("Loading...");
                await Task.Run(() =>
                {
                    try
                    {
                        using (Repository <Schedule> repo = new Repository <Schedule>())
                        {
                            var result = repo.Delete(schedulesToRemove);
                            if (!result.Success)
                            {
                                success = false;
                                error   = result.ErrorMessage;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        success = false;
                        error   = $"An error occured.Message: {ex.Message}";
                    }
                });

                if (success)
                {
                    //to avoid Invalid Operation Exception
                    for (int i = 0; i < ScheduleCollection.Count; i++)
                    {
                        if (ScheduleCollection[i].IsChecked)
                        {
                            ScheduleCollection.RemoveAt(i);
                            i--;
                        }
                    }
                    OnInformationRequested("Data removed successfully, refreshing service in the background...");
                    await Task.Run(() =>
                    {
                        var result = Controller.RefreshService();
                        if (!result.Success)
                        {
                            success = false;
                            error   = result.ErrorMessage;
                        }
                    });

                    if (success)
                    {
                        OnInformationRequested("Service refreshed successfully");
                    }
                    else
                    {
                        OnErrorOccured(error);
                    }
                }
                else
                {
                    OnErrorOccured(error);
                }
                RemoveMode = false;
            }
            CanUseDatagrid = true;
            CanEditFields  = false;
        }