import click


def get_new_count_from_user(field_type, limit=0):
    """Prompt the user for a new episode/chapter/volume count and return it

    :param field_type: A string,
    :param limit: An integer, the number of episodes/chapters/volumes in a series
    :return: An integer, the new episode count or None if the user cancelled
    """
    if limit < 0:
        raise ValueError("Limit must be greater than or equal to 0")

    while True:
        count = click.prompt("Enter the new {} count (leave blank to skip)".format(field_type), default=-1,
                             show_default=False)

        if 0 <= count:
            # if there is no limit or there is a valid count
            if limit == 0 or count <= limit:
                return count
        # if the user cancelled
        elif count == -1:
            return

        if limit != 0:
            click.echo("You must enter a value between 0 and {}.".format(limit))
        else:
            click.echo("You must enter a value greater than or equal to 0.")