from typing import Optional
from typing import Any
from typing import OrderedDict


def collection_gen(resources: list, topic_uri: str, uri: str, members: bool=False) -> Optional[Any]:
    """
    Generate an OrderedDict for a IIIF Presentation API collection from
    a list of manifest Ordered Dicts (with @type, @id, and label),
    setting the @id of the Collection based on the annotation 'creator'.

    :param resources: List of IIIF Presentation API Ordered Dicts
    :param topic_uri: id of the topic
    :param uri: base URI where the collection can be retrieved
    :param members: Boolean to set whether to use members or manifests. Members
    would allow Collections to be part of the Collection.

    :return: OrderedDict for IIIF Collection
    """
    if resources and topic_uri and uri:
        at_id = uri  # + 'topic/?topic=' + topic_uri
        coll = OrderedDict()
        coll['@context'] = 'http://iiif.io/api/presentation/2/context.json'
        coll['@id'] = at_id
        coll['@type'] = 'sc:Collection'
        coll['label'] = ' '.join(['IDA Topic Collection for', topic_uri.split('/')[-1]])
        if members:
            coll['members'] = [r for r in resources]
        else:
            coll['manifests'] = [r for r in resources]
        return coll
    else:
        return None