import login_generator_lib


def ask_for_amount_of_logins():
    while True:
        amount = input("how many logins do you want to generate? ")
        if amount.isdigit():
            amount = int(amount)
            break
        print("please input a number...")
    return amount


def ask_for_complex_or_not():
    while True:
        complexity = input("do you want complex passwords (y/n)? ").lower()
        if complexity.startswith("y"):
            complexity = True
            break
        elif complexity.startswith("n"):
            complexity = False
            break
        else:
            print("please answer with y(es) of n(o)...")
    return complexity


def ask_for_password_length():
    while True:
        length = input("how long should the password be? ")
        if length.isdigit():
            length = int(length)
            break
        print("please input a number...")
    return length


def ask_for_save_to_file():
    while True:
        save = input("do you want to save the logins (y/n)? ").lower()
        if save.startswith("y"):
            save = True
            break
        elif save.startswith("n"):
            save = False
            break
        else:
            print("please answer with y(es) of n(o)...")
    return save


def generate_logins(amount, length, complexity):
    logins = []
    for number in range(0, amount):
        login = login_generator_lib.generate_login(length, complexity)
        logins.append(login)
    return logins


def save_to_file(data):
    filepath = "logins.txt"
    with open(filepath, "w") as fp:
        for login in data:
            username = login["username"]
            password = login["password"]
            fp.write("{} {}\n".format(username, password))


def show_logins(logins):
    for login in logins:
        username = login["username"]
        password = login["password"]
        print("generated login is: {} with {} as password".format(username, password))


def main():
    amount_of_logins = ask_for_amount_of_logins()
    complexity = ask_for_complex_or_not()
    length = ask_for_password_length()
    save = ask_for_save_to_file()
    logins = generate_logins(amount_of_logins, length, complexity)
    show_logins(logins)
    if save:
        save_to_file(logins)


if __name__ == "__main__":
    main()
