#
# Copyright 2014 Google Inc. All rights reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#

"""Performs requests to the Google Maps Geocoding API."""
from googlemaps import convert


def geocode(client, address=None, components=None, bounds=None, region=None,
            language=None):
    """
    Geocoding is the process of converting addresses
    (like ``"1600 Amphitheatre Parkway, Mountain View, CA"``) into geographic
    coordinates (like latitude 37.423021 and longitude -122.083739), which you
    can use to place markers or position the map.

    :param address: The address to geocode.
    :type address: string

    :param components: A component filter for which you wish to obtain a
        geocode, for example: ``{'administrative_area': 'TX','country': 'US'}``
    :type components: dict

    :param bounds: The bounding box of the viewport within which to bias geocode
        results more prominently.
    :type bounds: string or dict with northeast and southwest keys.

    :param region: The region code, specified as a ccTLD ("top-level domain")
        two-character value.
    :type region: string

    :param language: The language in which to return results.
    :type langauge: string

    :rtype: list of geocoding results.
    """

    params = {}

    if address:
        params["address"] = address

    if components:
        params["components"] = convert.components(components)

    if bounds:
        params["bounds"] = convert.bounds(bounds)

    if region:
        params["region"] = region

    if language:
        params["language"] = language

    return client._get("/maps/api/geocode/json", params)["results"]


def reverse_geocode(client, latlng, result_type=None,
                    location_type=None, language=None):
    """
    Reverse geocoding is the process of converting geographic coordinates into a
    human-readable address.

    :param latlng: The latitude/longitude value for which you wish to obtain the
        closest, human-readable address.
    :type latlng: string, dict, list, or tuple
    
    :param place_id: The place_id for which you wish to obtain the
        closest, human-readable address.
    :type place_id: string

    :param result_type: One or more address types to restrict results to.
    :type result_type: string or list of strings

    :param location_type: One or more location types to restrict results to.
    :type location_type: list of strings

    :param language: The language in which to return results.
    :type langauge: string

    :rtype: list of reverse geocoding results.
    """

    if (type(latlng) == str) & (latlng[0] in ("-0123456789")):
    	params = {"place_id": latlng}
    else:
	    params = {"latlng": convert.latlng(latlng)}

    if result_type:
        params["result_type"] = convert.join_list("|", result_type)

    if location_type:
        params["location_type"] = convert.join_list("|", location_type)

    if language:
        params["language"] = language

    return client._get("/maps/api/geocode/json", params)["results"]

@@ -2219,28 +2219,8 @@
 one,
-%0A                   
  loc
@@ -2235,16 +2235,36 @@
 pe=None,
+%0A                   
  languag
@@ -2438,16 +2438,28 @@
 e value 
+or place_id 
 for whic
@@ -2460,32 +2460,40 @@
 r which you wish
+%0A       
  to obtain the%0A 
@@ -2486,32 +2486,24 @@
 o obtain the
-%0A       
  closest, hu
@@ -2580,144 +2580,8 @@
    %0A
-    :param place_id: The place_id for which you wish to obtain the%0A        closest, human-readable address.%0A    :type place_id: string%0A%0A
     
@@ -2990,14 +2990,25 @@
 ng) 
-== str
+in (str, unicode)
 ) & 
@@ -3021,15 +3021,20 @@
 g%5B0%5D
+ not
  in (%22-
++
 0123
@@ -3048,17 +3048,20 @@
 )):%0A    
-%09
+    
 params =
@@ -3096,9 +3096,12 @@
 se:%0A
-%09
+    
     

Support reverse_geocode by place_id