/// <summary>
        /// Attempts to register the user
        /// </summary>
        /// <param name="parameter">The SecureString passed in from the view for the users password</param>
        /// <returns></returns>
        public async void Register(PasswordBox parameter, PasswordBox repeat)
        {
            if (this.CanRegister != true || parameter.Password != repeat.Password || parameter.Password.Length <= 7 || !parameter.Password.Any(char.IsDigit))
            {
                ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("Please validate your password correctly.");
                return;
            }

            CommandHelper ch = new CommandHelper();

            await ch.RunCommand(() => IsRegisterRunning, async() =>
            {
                await Task.Delay(2000);

                try
                {
                    using (var db = new ApirsDatabase())
                    {
                        var paramLoginName = new SqlParameter
                        {
                            ParameterName = "pLogin",
                            Value         = UserName,
                            Direction     = ParameterDirection.Input
                        };

                        var paramPass = new SqlParameter
                        {
                            ParameterName = "pPassword",
                            Value         = parameter.Password,
                            Direction     = ParameterDirection.Input
                        };


                        var paramMail = new SqlParameter
                        {
                            ParameterName = "pMail",
                            Value         = Email,
                            Direction     = ParameterDirection.Input
                        };

                        var paramResponse = new SqlParameter
                        {
                            ParameterName = "responseMessage",
                            Size          = 250,
                            SqlDbType     = SqlDbType.NVarChar,
                            Direction     = ParameterDirection.Output
                        };


                        string par = db.Database.SqlQuery <string>("exec dbo.spAddUser @pLogin, @pPassword, @pMail, @responseMessage", paramLoginName, paramPass, paramMail, paramResponse).First();

                        //Forward the user to the home view or denying the login based on the response of the server
                        switch (par)
                        {
                        case "Message":
                            ((ShellViewModel)IoC.Get <IShell>()).ShowInformation(par + ". Please try it again.");
                            return;

                        case "Success":

                            var paramLoginName1 = new SqlParameter
                            {
                                ParameterName = "pLogin",
                                Value         = Email,
                                Direction     = ParameterDirection.Input
                            };

                            var paramFirstName = new SqlParameter
                            {
                                ParameterName = "pFirstName",
                                Value         = FirstName,
                                Direction     = ParameterDirection.Input
                            };

                            var paramLastName = new SqlParameter
                            {
                                ParameterName = "pLastName",
                                Value         = LastName,
                                Direction     = ParameterDirection.Input
                            };


                            var paramAffiliation = new SqlParameter
                            {
                                ParameterName = "pAffiliation",
                                Value         = Affiliation,
                                Direction     = ParameterDirection.Input
                            };

                            var paramStatus = new SqlParameter
                            {
                                ParameterName = "pStatus",
                                SqlDbType     = SqlDbType.Int,
                                Value         = 3,
                                Direction     = ParameterDirection.Input
                            };

                            var paramResponse1 = new SqlParameter
                            {
                                ParameterName = "responseMessage",
                                Size          = 250,
                                SqlDbType     = SqlDbType.NVarChar,
                                Direction     = ParameterDirection.Output
                            };

                            string par1  = db.Database.SqlQuery <string>("exec dbo.spAddPerson @pFirstName, @pLastName, @pAffiliation, @pStatus, @pLogin, @responseMessage", paramFirstName, paramLastName, paramAffiliation, paramStatus, paramLoginName1, paramResponse1).First();
                            string param = par1.ToString();

                            switch (par1)
                            {
                            case "Success":
                                ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("You successfully created a profile for GeoReVi. You can login now with your user name and password.");
                                UserName    = "";
                                Affiliation = "";
                                LastName    = "";
                                FirstName   = "";
                                Email       = "";
                                _events.PublishOnUIThreadAsync(new ChangeViewModelMessage("LoginView"));
                                break;

                            case "Message":
                            default:
                                var paramLoginName2 = new SqlParameter
                                {
                                    ParameterName = "pLogin",
                                    Value         = Email,
                                    Direction     = ParameterDirection.Input
                                };

                                var paramResponse2 = new SqlParameter
                                {
                                    ParameterName = "responseMessage",
                                    Size          = 250,
                                    SqlDbType     = SqlDbType.NVarChar,
                                    Direction     = ParameterDirection.Output
                                };
                                //Triggering the delete user sp
                                string par2 = db.Database.SqlQuery <string>("exec dbo.spDeleteUser @pLogin, @responseMessage", paramLoginName2, paramResponse2).First();
                                ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("Please try it again.");
                                break;
                            }

                            break;

                        default:
                            return;
                        }

                        ////Stored procedures
                        //SqlCommand spAddUser = new SqlCommand("dbo.spAddUser", SqlConn);
                        //SqlCommand spAddPerson = new SqlCommand("dbo.spAddPerson", SqlConn);

                        ////Testing if a connection is established
                        //if (ServerInteractionHelper.IsNetworkAvailable() && ServerInteractionHelper.TryAccessDatabase())
                        //{
                        //    //Preparing the stored procedures
                        //    spAddUser.CommandType = System.Data.CommandType.StoredProcedure;
                        //    spAddPerson.CommandType = System.Data.CommandType.StoredProcedure;

                        //    //Adding the parameters
                        //    spAddUser.Parameters.Add("@pLogin", SqlDbType.NVarChar, 50);
                        //    spAddUser.Parameters.Add("@pPassword", SqlDbType.NVarChar, 50);
                        //    spAddUser.Parameters.Add("@pMail", SqlDbType.NVarChar, 255);
                        //    spAddUser.Parameters.Add("@responseMessage", SqlDbType.NVarChar, 255).Direction = ParameterDirection.Output;

                        //    spAddUser.Parameters["@pLogin"].Value = this.UserName;
                        //    spAddUser.Parameters["@pMail"].Value = this.Email;
                        //    spAddUser.Parameters["@pPassword"].Value = parameter.Password;
                        //}
                        //else
                        //{
                        //    return;
                        //}
                    }
                }
                catch (NullReferenceException ne)
                {
                    Console.WriteLine(ne.Message);
                }
                catch (Exception e)
                {
                    ((ShellViewModel)IoC.Get <IShell>()).ShowInformation(e.Message);
                }
            });
        }
        /// <summary>
        /// Attempts to register the user
        /// </summary>
        /// <param name="parameter">The SecureString passed in from the view for the users password</param>
        /// <returns></returns>
        public async void Delete()
        {
            if (SelectedPerson.persIdPk == 0)
            {
                ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("You have to be logged in.");
            }

            // If existing window is visible, delete the customer and all their orders.
            // In a real application, you should add warnings and allow the user to cancel the operation.
            if (((ShellViewModel)IoC.Get <IShell>()).ShowQuestion("Are you REALLY sure to delete your user profile?" + Environment.NewLine
                                                                  + "You won't be able to reconstruct your created projects, rock samples and measurements." + Environment.NewLine
                                                                  + "Please be sure, that you have exported all relevant data.") == MessageBoxViewResult.No)
            {
                return;
            }

            CommandHelper ch = new CommandHelper();

            await ch.RunCommand(() => IsUpdateRunning, async() =>
            {
                await Task.Delay(2000);

                try
                {
                    //Establishing a sql connection
                    using (SqlConnection SqlConn = new SqlConnection(this.apirsDatabase.Database.Connection.ConnectionString.ToString()))
                    {
                        //Testing if a connection is established
                        if (ServerInteractionHelper.IsNetworkAvailable() && ServerInteractionHelper.TryAccessDatabase())
                        {
                            //Triggering the delete user sp
                            SqlCommand spDeleteUser  = new SqlCommand("dbo.spDeleteUser", SqlConn);
                            spDeleteUser.CommandType = CommandType.StoredProcedure;
                            //Adding the parameters
                            spDeleteUser.Parameters.Add("@pLogin", SqlDbType.NVarChar, 50);
                            spDeleteUser.Parameters["@pLogin"].Value = SelectedPerson.persUserName;
                            spDeleteUser.Parameters.Add("@responseMessage", SqlDbType.NVarChar, 250).Direction = ParameterDirection.Output;
                            //Executing the stored procedure
                            SqlConn.Open();
                            spDeleteUser.ExecuteNonQuery();
                            var par = Convert.ToString(spDeleteUser.Parameters["@responseMessage"].Value);
                            SqlConn.Close();

                            switch (par)
                            {
                            case "1":
                                ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("You successfully deleted your profile.");
                                _events.PublishOnUIThreadAsync(new ChangeUserMessage(0, "Logged out"));
                                _events.PublishOnUIThreadAsync(new ChangeViewModelMessage("LoginView"));
                                break;

                            default:
                                ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("An unexpected error occured.");
                                break;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                catch (NullReferenceException ne)
                {
                    Console.WriteLine(ne.Message);
                }
                catch (Exception e)
                {
                    ((ShellViewModel)IoC.Get <IShell>()).ShowInformation(e.Message);
                }
            });
        }
        /// <summary>
        /// Attempts to log the user in
        /// </summary>
        /// <param name="parameter">The SecureString passed in from the view for the users password</param>
        /// <returns></returns>
        public async void Login(PasswordBox parameter)
        {
            CommandHelper ch = new CommandHelper();

            await ch.RunBackgroundWorkerWithFlagHelperAsync(() => IsLoginRunning, async() =>
            {
                try
                {
                    //var pwBox = (PasswordBox)parameter;
                    string username = this.Email ?? "";

                    using (var db = new ApirsDatabase())
                    {
                        var paramLoginName = new SqlParameter
                        {
                            ParameterName = "pLoginName",
                            Value         = username,
                            Direction     = ParameterDirection.Input
                        };

                        var paramPass = new SqlParameter
                        {
                            ParameterName = "pPassword",
                            Value         = parameter.Password,
                            Direction     = ParameterDirection.Input
                        };

                        var paramResponse = new SqlParameter
                        {
                            ParameterName = "responseMessage",
                            Size          = 250,
                            SqlDbType     = SqlDbType.NVarChar,
                            Direction     = ParameterDirection.Output
                        };


                        string par = db.Database.SqlQuery <string>("exec dbo.spUserLogin @pLoginName, @pPassword, @responseMessage", paramLoginName, paramPass, paramResponse).First();

                        //Forward the user to the home view or denying the login based on the response of the server
                        switch (par)
                        {
                        case "Invalid login":
                        case "Incorrect password":
                            _events.PublishOnUIThreadAsync(new MessageBoxMessage("Wrong password. Please try it again", "", MessageBoxViewType.Information, MessageBoxViewButton.Ok));
                            break;

                        case "User successfully logged in":
                            //Get the actual user id and set it as a property in the shellview
                            tblPerson result = (from p in db.tblPersons
                                                where p.persUserName == username.ToString()
                                                select p).First();
                            _events.PublishOnUIThreadAsync(new ChangeUserMessage(Convert.ToInt32(result.persIdPk), result.persFullName));
                            //Changing the viewmodel to the homeview
                            _events.PublishOnUIThreadAsync(new ChangeViewModelMessage("HomeView"));
                            break;

                        default:
                            break;
                        }
                    }

                    return;
                }
                catch (Exception e)
                {
                    _events.PublishOnUIThreadAsync(new MessageBoxMessage(UserMessageValueConverter.ConvertBack(1), "", MessageBoxViewType.Error, MessageBoxViewButton.Ok));
                }
            });
        }