def verify_unused_email(email):
    query = "SELECT u.Email FROM tUsers u WHERE u.Email = '" + email + "';"
    result = DBConn.query_return(query)
    if len(result) > 0:
        return False
    else:
        return True
def verify_unused_username(username):
    query = "SELECT u.Username FROM tUsers u WHERE u.Username = '******';"
    result = DBConn.query_return(query)
    if len(result) > 0:
        return False
    else:
        return True
def get_user_salt(username):
    query = "SELECT u.Salt FROM tUsers u WHERE u.Username = '******';"
    result = DBConn.query_return(query)
    if len(result) == 1:
        salt = next(iter(result))["Salt"]
        # Encode back into byte array
        salt = salt.encode("utf-8", "ignore")
        return salt
def login(user):
    if user.UserName is not None and user.Password is not None:
        user.Password = get_hashed_password(user.UserName, user.Password)
        # The 'BINARY' keyword forces case sensitive searches
        query = "SELECT u.UserID, u.Salt, u.Email, u.FirstName, u.LastName FROM tUsers u WHERE u.Username = '******' and u.Password = BINARY '" + user.Password + "';"
        result = DBConn.query_return(query)
        user.mapper(result)
        return user
    else:
        return
def fill(user):
    if user.UserID == 0:
        return None
    else:
        query = "SELECT u.Username, u.Salt, u.Email, u.FirstName, u.LastName, u.Password FROM tUsers u WHERE u.UserID = " + str(
            user.UserID) + ";"
        result = DBConn.query_return(query)
        if len(result) > 0:
            user.mapper(result)
        else:
            user.UserID = 0
        return user
def fill(password):
    if password.PasswordID == 0:
        return None
    else:
        query = "SELECT p.PasswordID, p.DateCreated, p.DateModified, p.UserID, pd.PasswordType, pd.PasswordName, pd.PasswordUser, pd.PasswordSite, pd.PasswordPassword, pd.PasswordNote " \
                "FROM tPassword p LEFT JOIN tPasswordDetail pd ON p.PasswordDetailID = pd.PasswordDetailID " \
                "WHERE p.PasswordID = " + password.PasswordID + ";"
        result = DBConn.query_return(query)
        if len(result) > 0:
            password.mapper(result)
        else:
            password.PasswordID = 0
        return password
def get_all_user_passwords(user_id, user_password):
    passwords = []
    query = "SELECT p.PasswordID, p.DateCreated, p.DateModified, p.UserID, pd.PasswordType, pd.PasswordName, pd.PasswordUser, pd.PasswordSite, pd.PasswordPassword, pd.PasswordNote, u.UserID " \
            "FROM tPassword p " \
            "LEFT JOIN tPasswordDetail pd ON p.PasswordDetailID = pd.PasswordDetailID " \
            "LEFT JOIN tUsers u ON u.UserID = p.UserID " \
            "WHERE u.UserID = " + user_id + " AND u.Password = '******';"
    result = DBConn.query_return(query)
    for row in result:
        password = Password()
        password.mapper(row)
        passwords.append(password)
    return passwords
def delete(password_id):
    password_detail_id = None
    query = "SELECT p.PasswordDetailID FROM tPassword p WHERE p.PasswordID = " + str(
        password_id) + ";"
    result = DBConn.query_return(query)
    for dictionary in result:
        password_detail_id = list(dictionary.values())[0]
    if password_detail_id is not None:
        query = "DELETE FROM tPassword p WHERE p.PasswordID = " + str(
            password_id) + ";"
        DBConn.query_update(query, False)
        query = "DELETE FROM tPasswordDetail pd WHERE pd.PasswordDetailID = " + str(
            password_detail_id) + ";"
        DBConn.query_update(query, False)
        return True
    else:
        return False