/// <summary>
        /// Searches the Required Passwords
        /// </summary>
        /// <param name="user">User whose Passwords are used as source for Search.</param>
        /// <param name="Search">Search Keyword</param>
        /// <param name="LooksFor">Looks for Password Name, Email or Username</param>
        /// <param name="Options">Options for Search Keywords Matched either Equals or Contains </param>
        /// <returns>List of Password: Passwords matching the search criteria.</returns>
        public Task <List <Password> > SearchUserPasswordsAsync(User user, string Search, string LooksFor, string Options)
        {
            //we can send the search query to database -gul:0301171513

            return(Task.Factory.StartNew(() =>
            {
                List <Password> AllPasswords = GetAllUserPasswords(user);
                if (ValidationService.Instance().Passwords(AllPasswords))
                {
                    List <Password> searchedPasswords = null;

                    if (string.IsNullOrEmpty(Search))
                    {
                        return AllPasswords;
                    }
                    else
                    {
                        string notContains = string.Empty;
                        string contains = string.Empty;
                        int indexPosition = Search.ToLower().LastIndexOf('^');
                        if (indexPosition > 0)
                        {
                            contains = Search.ToLower().Substring(0, Search.ToLower().LastIndexOf('^')).Trim();
                            notContains = Search.ToLower().Substring(Search.ToLower().LastIndexOf('^')).Replace("^", "").Trim();
                        }
                        else
                        {
                            contains = Search.ToLower();
                        }

                        switch (Options)
                        {
                        case "Contains":
                            if (LooksFor == "Username")
                            {
                                searchedPasswords = AllPasswords.Where(p => contains.Split(' ').Any(inner => p.Username.ToLower().Trim().Contains(inner))).ToList();
                                if (indexPosition > 0)
                                {
                                    searchedPasswords.RemoveAll(p => notContains.Split(' ').Any(inner => p.Username.ToLower().Trim().Contains(inner)));
                                }
                            }
                            else if (LooksFor == "Email")
                            {
                                searchedPasswords = AllPasswords.Where(p => contains.Split(' ').Any(inner => p.Email.ToLower().Trim().Contains(inner))).ToList();
                                if (indexPosition > 0)
                                {
                                    searchedPasswords.RemoveAll(p => notContains.Split(' ').Any(inner => p.Email.ToLower().Trim().Contains(inner)));
                                }
                            }
                            else
                            {
                                searchedPasswords = AllPasswords.Where(p => contains.Split(' ').Any(inner => p.Name.ToLower().Trim().Contains(inner))).ToList();
                                if (indexPosition > 0)
                                {
                                    searchedPasswords.RemoveAll(p => notContains.Split(' ').Any(inner => p.Name.ToLower().Trim().Contains(inner)));
                                }
                            }
                            break;

                        case "Equals":
                            if (LooksFor == "Username")
                            {
                                searchedPasswords = AllPasswords.Where(p => p.Username.ToLower().Equals(Search.ToLower())).ToList();
                            }
                            else if (LooksFor == "Email")
                            {
                                searchedPasswords = AllPasswords.Where(p => p.Email.ToLower().Equals(Search.ToLower())).ToList();
                            }
                            else
                            {
                                searchedPasswords = AllPasswords.Where(p => p.Name.ToLower().Equals(Search.ToLower())).ToList();
                            }
                            break;
                        }
                        return searchedPasswords;
                    }
                }
                else
                {
                    return null;
                }
            }));
        }
        /// <summary>
        /// Generates a new Random Password
        /// </summary>
        /// <param name="user">User whose PasswordOptions settings is used.</param>
        /// <returns>String: Random Characters to be used as Password.</returns>
        public Task <string> GeneratePasswordAsync(User user)
        {
            return(Task.Factory.StartNew(() =>
            {
                PasswordOptions passwordOptions = null;

                if (ValidationService.Instance().User(user))
                {
                    passwordOptions = PasswordOptionsData.Instance().GetPasswordOptionsBySettings(user.Settings);
                }
                else
                {
                    passwordOptions = Globals.Defaults.PasswordOptions;
                }

                // Make a list of allowed characters.
                string allowed = "";

                if (passwordOptions.AllowLowercaseCharacters)
                {
                    allowed += passwordOptions.LowercaseCharacters;
                }
                if (passwordOptions.AllowUppercaseCharacters)
                {
                    allowed += passwordOptions.UppercaseCharacters;
                }
                if (passwordOptions.AllowNumberCharacters)
                {
                    allowed += passwordOptions.NumberCharacters;
                }
                if (passwordOptions.AllowSpecialCharacters)
                {
                    allowed += passwordOptions.SpecialCharacters;
                }
                if (passwordOptions.AllowUnderscoreCharacters)
                {
                    allowed += passwordOptions.UnderscoreCharacters;
                }
                if (passwordOptions.AllowSpaceCharacters)
                {
                    allowed += passwordOptions.SpaceCharacters;
                }
                if (passwordOptions.AllowOtherCharacters)
                {
                    allowed += passwordOptions.OtherCharacters;
                }

                Random random = new Random();

                int RequiredLength = random.Next(passwordOptions.MinimumCharacters, passwordOptions.MaximumCharacters);

                // Satisfy requirements.
                string password = "";

                password = GenerateRandomWord(RequiredLength - (int)(RequiredLength * 0.5));

                if (passwordOptions.RequireLowercaseCharacters &&
                    (password.IndexOfAny(passwordOptions.LowercaseCharacters.ToCharArray()) == -1))
                {
                    password += RandomCharacter(passwordOptions.LowercaseCharacters, random);
                }
                if (passwordOptions.RequireUppercaseCharacters &&
                    (password.IndexOfAny(passwordOptions.UppercaseCharacters.ToCharArray()) == -1))
                {
                    password = AddUpperCaseOnString(password);
                }
                if (passwordOptions.RequireNumberCharacters &&
                    (password.IndexOfAny(passwordOptions.NumberCharacters.ToCharArray()) == -1))
                {
                    password += RandomCharacter(passwordOptions.NumberCharacters, random);
                }
                if (passwordOptions.RequireSpecialCharacters &&
                    (password.IndexOfAny(passwordOptions.SpecialCharacters.ToCharArray()) == -1))
                {
                    password = RandomCharacter(passwordOptions.SpecialCharacters, random) + password;
                }
                if (passwordOptions.RequireUnderscoreCharacters &&
                    (password.IndexOfAny(passwordOptions.UnderscoreCharacters.ToCharArray()) == -1))
                {
                    password += passwordOptions.UnderscoreCharacters;
                }
                if (passwordOptions.RequireSpaceCharacters &&
                    (password.IndexOfAny(passwordOptions.SpaceCharacters.ToCharArray()) == -1))
                {
                    password += passwordOptions.SpaceCharacters;
                }
                if (passwordOptions.RequireOtherCharacters &&
                    (password.IndexOfAny(passwordOptions.OtherCharacters.ToCharArray()) == -1))
                {
                    password += passwordOptions.OtherCharacters;
                }

                password = GenerateRandomWord(RequiredLength - password.Length) + password;

                while (password.Length < RequiredLength)
                {
                    password += allowed.Substring(
                        random.Next(0, allowed.Length - 1), 1);
                }

                return password;
            }));
        }
 /// <summary>
 /// Updates the PasswordOptions for supplied User Settings
 /// </summary>
 /// <param name="user">User for Settings Access.</param>
 /// <param name="settings">Settings to be updated with PasswordOptions.</param>
 /// <param name="passwordOptions">PasswordOptions to be updated.</param>
 /// <returns>User: User with the updated Settings and PasswordOptions.</returns>
 public Task <User> UpdateUserPasswordOptionsAsync(User user, Settings settings, PasswordOptions passwordOptions)
 {
     return(Task.Factory.StartNew(() =>
     {
         if (ValidationService.Instance().User(user) && ValidationService.Instance().Settings(settings) && ValidationService.Instance().PasswordOptions(passwordOptions))
         {
             if (PasswordOptionsData.Instance().UpdatePasswordOptionsBySettings(settings, passwordOptions) > 0)
             {
                 user.Settings.PasswordOptions = passwordOptions;
                 return user;
             }
             else
             {
                 return null;
             }
         }
         else
         {
             return null;
         }
     }));
 }