import logging
import time


def stopwatch(func):
    """ Stopwatch Decorator.

    Use this decorator on any function to measure how long it took
    for the function to complete. This is in seconds, but may have fractions of a second
    if the system clock provides more precision.

    Notes:
        This is _logged_, not printed to the Terminal

    Examples:
        1. How long does it take to add an item to the cart?
        @stopwatch
        def add_item_to_cart(py):
            py.get('#add-item').click()
            py.get('#added-notification').should().be_visible()

        2. How long does it take to edit an item's available stock via the API
           and see it change in the UI?
        @stopwatch
        def update_available_stock(py, item, quantity):
            payload = {'item': item, 'qty': quantity}
            api.items.update(payload)
            py.get(f'#available-stock-{item}').should().have_text(quantity)
    """
    def wrapper(*args, **kwargs):
        log = logging.getLogger('driver')
        start_time = time.time()
        func(*args, **kwargs)
        stop_time = time.time()
        func_name = func.__name__
        log.info(f'STOPWATCH - {func_name} took {stop_time - start_time} seconds')
    return wrapper