#!/usr/bin/env python

"""
Access a Galaxy instance and get data for LibraryDatasetDatasetAssociations
and LibraryFolders.

Methods to list a library's contents - not unlike hdas_1, 2, 3.
Libraries, however, are somewhat more complex in that they can contain both
LDDAs and LibraryFolders (which themselves can contain both).

For the purposes of this workshop - we won't explore the 'nesting' aspect
of Libraries and will focus on simplicity.
"""
import os
import sys
import pprint

import setup
import common

# same as libraries - but we're getting contents
RESOURCE_URL = '/api/libraries'

# ----------------------------------------------------------------------------- functions
def get_lddas( library_id ):
    """
    Return a list of dictionaries that describe all lddas from a specific library.
    :param library_id: the id of the library which we want to query.
    """
    apikey = setup.get_apikey()
    container_url = setup.get_base_url() + RESOURCE_URL
    full_url = container_url + '/' + library_id + '/contents'
    return common.get( apikey, full_url )

def get_ldda( library_id, ldda_id ):
    """
    Return a dictionary that the gives details of a specific ldda.
    :param library_id: the id of the library which we want to query.
    :param ldda_id: the id of the LDDA which we want to query.
    """
    apikey = setup.get_apikey()
    container_url = setup.get_base_url() + RESOURCE_URL
    full_url = container_url + '/' + library_id + '/contents/' + ldda_id
    return common.get( apikey, full_url )

def copy_hda_to_ldda( library_id, library_folder_id, hda_id, message='' ):
    """
    Copy an HDA into a LDDA (that the current user has add permissions on)
    in the given library and library folder.
    :param library_id: the id of the library which we want to query.
    :param library_folder_id: the id of the folder to copy into.
    :param hda_id: the id of the HDA we want to copy.
    :param message: an optional message to add to the new LDDA.
    """
    apikey = setup.get_apikey()
    container_url = setup.get_base_url() + RESOURCE_URL
    full_url = container_url + '/' + library_id + '/contents'

    post_data = {
        'folder_id'     : library_folder_id,
        'create_type'   : 'file',
        'from_hda_id'   : hda_id,
    }
    if message:
        post_data[ 'ldda_message' ] = message

    return common.post( apikey, full_url, post_data )


# ----------------------------------------------------------------------------- main
if __name__ == '__main__':
    if   len( sys.argv ) < 2:
        # no args: help
        print 'USAGE: lddas_1.py <library id> [<ldda id>] [<id of hda to copy>]'
        sys.exit( 1 )

    elif len( sys.argv ) == 2:
        # 1 arg: get all lddas in a library
        library_id = sys.argv[1]
        returned = get_lddas( library_id )
        pprint.pprint( returned, indent=2 )

    elif len( sys.argv ) == 3:
        # 2 args: get a specific LDDA from a library
        library_id = sys.argv[1]
        ldda_id = sys.argv[2]
        returned = get_ldda( library_id, ldda_id )
        pprint.pprint( returned, indent=2 )

    elif len( sys.argv ) >= 4:
        # 3 args: copy an HDA into a specific folder in a library
        ( library_id, library_folder_id, hda_id ) = sys.argv[1:4]
        returned = copy_hda_to_ldda( library_id, library_folder_id, hda_id )
