import requests
import json


def get_collection_uuid(uri, token, collection_name):
    """Returns the DSA collection uuid from the provided `collection_name`

    Args:
        uri (string): DSA host:port e.g. localhost:8080
        token (string): DSA token from /token/current HistomicsUI API
        collection_name (string): name of the collection in DSA

    Returns:
        string: DSA collection uuid. None if nothing matches the collection name.
    """
    get_collection_id_url = f"http://{uri}/api/v1/collection?text={collection_name}&limit=5&sort=name&sortdir=1"
    headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'Girder-Token': f'{token}'}
    get_collection_id_url_response = requests.get(get_collection_id_url, headers=headers)
    get_collection_id_response = json.loads(get_collection_id_url_response.text)

    collection_id_dicts = get_collection_id_response

    for collection_id_dict in collection_id_dicts:
        print("collection_id_dict", collection_id_dict)
        if collection_id_dict['name'] == collection_name:
            collection_id = collection_id_dict['_id']
            print(f"Collection {collection_name} found with id: {collection_id}")
            return collection_id

    print(f"Collection {collection_name} not found")
    return None