# Licensed under a 3-clause BSD style license - see LICENSE.rst

"""
This module contains...
"""

from __future__ import division, print_function

# Standard library
import re
import sys
import httplib
import urllib
import urllib2

# Third party
import numpy as np

# Astropy
from .builtin_systems import ICRSCoordinates
from .. import units as u

__all__ = ["get_icrs_coordinates"]

sesame_url = "http://cdsweb.u-strasbg.fr/cgi-bin/nph-sesame/{db}?{name}"
mirror_url = "http://vizier.cfa.harvard.edu/viz-bin/nph-sesame/{db}?{name}"
allowed_databases = ['ned', 'simbad', 'vizier', 'all']

def get_icrs_coordinates(name, database='all'):
    """ Retrieve an ICRSCoordinates object by using an online name resolving
        service to retrieve coordinates for the specified name.

        Parameters
        ----------
        name : str
            The name of the object to get coordinates for, e.g. m42.
        database : str (optional)
            Specify which database to search. Can be 'ned', 'simbad', 'vizier', or 'all.'
    """

    if database.lower() not in allowed_databases:
        raise ValueError("Invalid database name: {0}. Allowed databases: {1}".format(database, ",".join(allowed_databases)))

    # The web API just takes the first letter of the database name
    db = database.upper()[0]
    url = sesame_url.format(name=urllib.quote(name), db=db)
    try:
        # Retrieve ascii name resolve data from CDS
        resp = urllib2.urlopen(url)
    except urllib2.URLError:
        raise urllib2.URLError("Unable to connect to name resolve web server. Check your internet connection, and try again.")
    except httplib.BadStatusLine:
        raise urllib2.URLError("Server didn't return any data. This could mean the object was not found in the specified database. URL: {0}".format(url))

    resp_data = resp.read()

    pattr = re.compile(r"%J\s*([0-9\.]+)\s*([\+\-\.0-9]+)")
    matched = pattr.search(resp_data.decode('utf-8'))

    if matched == None:
        if db == "A":
            err = "Unable to find coordinates for name '{0}'".format(name)
        else:
            err = "Unable to find coordinates for name '{0}' in database {1}".format(name, database)

        raise ValueError(err)

    ra,dec = matched.groups()

    return ICRSCoordinates(ra, dec, unit=(u.degree, u.degree))
@@ -377,26 +377,26 @@
 ates%22%5D%0A%0A
-sesame_url
+SESAME_URL
  = %22http
@@ -454,18 +454,18 @@
 e%7D%22%0A
-mirror_url
+MIRROR_URL
  = %22
@@ -526,33 +526,33 @@
 %7Bname%7D%22%0A
-allowed_databases
+ALLOWED_DATABASES
  = %5B'ned
@@ -580,16 +580,52 @@
 , 'all'%5D
+%0ANAME_RESOLVE_TIMEOUT = 30 # seconds
 %0A%0Adef ge
@@ -1057,16 +1057,163 @@
 r 'all.'
+%0A%0A        Returns%0A        -------%0A        coord : SphericalCoordinatesBase%0A            An %60ICRSCoordinates%60 instance for the object name specified.
 %0A    %22%22%22
@@ -1245,33 +1245,33 @@
  not in 
-allowed_databases
+ALLOWED_DATABASES
 :%0A      
@@ -1372,25 +1372,25 @@
 oin(
-allowed_databases
+ALLOWED_DATABASES
 )))%0A
@@ -1500,18 +1500,18 @@
 l = 
-sesame_url
+SESAME_URL
 .for
@@ -1641,16 +1641,46 @@
 open(url
+, timeout=NAME_RESOLVE_TIMEOUT
 )%0A    ex

add timeout to urlopen, fix returns docstring