def revert_annotation(gc, annotation_id=None, annotation=None, version=None,
    revert_to_nonempty_elements=False, only_revert_if_empty=True):
    """Revert an annotation to a previous version.

    Parameters
    ----------
    gc : girder_client.GirderClient
        authenticated girder client
    annotation_id : str
        girder id of annotation
    annotation : dict
        overrides annotation_id if given
    version : int
        versoin number for annotation. If None, and
        not revert_to_nonempty_elements
        the default behavior of the endpoint is evoked, which reverts the
        annotation if it was deleted and if not, reverts to the last version.
    revert_to_nonempty_elements : bool
        if true, reverts to the most recent version of the annotation
        with non-empty elements.
    only_revert_if_empty : bool
        if true, only reverts annotation if it contains an empty element list

    Returns
    -------
    dict
        server response

    """
    if annotation is not None:
        annotation_id = annotation['_id']
    elif annotation_id is None:
        raise Exception(
            'You must provide either the annotation or its girder id.')
    history = gc.get('/annotation/%s/history' % annotation_id)
    if only_revert_if_empty and len(history[0]['groups']) > 0:
        return dict()
    if len(history) < 2:
        return dict()
    if version is None and revert_to_nonempty_elements:
        for ver in history:
            if len(ver['groups']) > 0:
                version = ver['_version']
                break
    ver = '' if version is None else '?version=%d' % version
    return gc.put('/annotation/%s/history/revert%s' % (annotation_id, ver))
