# -*- coding: utf-8 -*-

"""

NDEx
~~~~
This module provides a wrapper around the input/output functions in :code:`pybel` for converting BEL graphs to CX
documents and the NDEx API wrapper provided by the :code:`ndex-python` for uploading/downloading CX documents from
ndexbio.org

The interface in :code:`pybel2ndex` is incredibly simple. It provides the user with two functions: one for upload, and 
one for download.

"""

import logging
import os

from ndex.client import Ndex
from requests.compat import urlsplit

from .cx import from_cx, to_cx

__all__ = [
    'to_ndex',
    'from_ndex',
]

log = logging.getLogger(__name__)

#: The name of the environment variable to search or the NDEx username
NDEX_USERNAME = 'NDEX_USERNAME'

#: The name of the environment variable to search or the NDEx password
NDEX_PASSWORD = 'NDEX_PASSWORD'


def build_ndex_client(username=None, password=None, debug=False):
    """Builds a NDEx client by checking environmental variables.

    It has been requested that the :code:`ndex-client` has this functionality built-in by this GitHub 
    `issue <https://github.com/ndexbio/ndex-python/issues/9>`_

    :param str username: NDEx username
    :param str password: NDEx password
    :param bool debug: If true, turn on NDEx client debugging
    :return: An NDEx client
    :rtype: ndex.client.Ndex
    """
    if username is None and NDEX_USERNAME in os.environ:
        username = os.environ[NDEX_USERNAME]
        log.info('got NDEx username from environment: %s', username)

    if password is None and NDEX_PASSWORD in os.environ:
        password = os.environ[NDEX_PASSWORD]
        log.info('got NDEx password from environment')

    return Ndex(username=username, password=password, debug=debug)


def cx_to_ndex(cx, username=None, password=None, debug=False):
    """Uploads a CX document to NDEx. Not necessarily specific to PyBEL.

    :param list cx: A CX JSON dictionary
    :param str username: NDEx username
    :param str password: NDEx password
    :param bool debug: If true, turn on NDEx client debugging
    :return: The UUID assigned to the network by NDEx
    :rtype: str
    """
    ndex = build_ndex_client(username=username, password=password, debug=debug)
    res = ndex.save_new_network(cx)

    url_parts = urlsplit(res).path
    network_id = url_parts.split('/')[-1]

    return network_id


def to_ndex(graph, username=None, password=None, debug=False):
    """Uploads a BEL graph to NDEx

    :param pybel.BELGraph graph: A BEL Graph
    :param str username: NDEx username
    :param str password: NDEx password
    :param bool debug: If true, turn on NDEx client debugging
    :return: The UUID assigned to the network by NDEx
    :rtype: str

    Example Usage:

    >>> import pybel
    >>> graph = pybel.from_url('http://resources.openbel.org/belframework/20150611/knowledge/small_corpus.bel')
    >>> pybel.to_ndex(graph)
    """
    cx = to_cx(graph)
    return cx_to_ndex(cx, username=username, password=password, debug=debug)


def from_ndex(network_id, username=None, password=None, debug=False):
    """Downloads a BEL Graph from NDEx

    .. warning:: This function only will work for CX documents that have been originally exported from PyBEL

    :param str network_id: The UUID assigned to the network by NDEx
    :param str username: NDEx username
    :param str password: NDEx password
    :param bool debug: If true, turn on NDEx client debugging
    :return: A BEL Graph
    :rtype: pybel.BELGraph

    Example Usage:

    >>> from pybel import from_ndex
    >>> network_id = '1709e6f3-04a1-11e7-aba2-0ac135e8bacf'
    >>> graph = from_ndex(network_id)
    """
    ndex = build_ndex_client(username, password, debug)
    res = ndex.get_network_as_cx_stream(network_id)
    cx = res.json()
    graph = from_cx(cx)
    return graph

@@ -42,13 +42,14 @@
 his 
-modul
+packag
 e pr
@@ -76,177 +76,125 @@
 und 
-the input/output functions in :code:%60pybel%60 for converting BEL graphs to CX%0Adocuments and the NDEx API wrapper provided by the :code:%60ndex-python%60 for
+:func:%60pybel.to_cx%60 and NDEx%0A%60client %3Chttps://github.com/ndexbio/ndex-python%3E%60_ to allow for easy
  upload
-ing/
+ and 
 download
 ing 
@@ -193,14 +193,15 @@
 load
-ing CX
+ of BEL
  doc
@@ -211,162 +211,230 @@
 nts 
-from%0Andexbio.org%0A%0AThe interface in :code:%60pybel2ndex%60 is incredibly simple. It provides the user with two functions: one for upload, and %0Aone for download
+to the%0A%60NDEx %3Chttp://www.ndexbio.org/%3E%60_ database.%0A%0AThe programmatic API also provides options for specifying username and password. By default, it checks the environment %0Avariables: %60%60NDEX_USERNAME%60%60 and %60%60NDEX_PASSWORD%60%60
 .%0A%0A%22

Update docs