# -*- coding: utf-8 -*-
import pytest
from gmaps import errors

from gmaps import Geocoding
from .testutils import retry

geocoding = Geocoding(sensor=False)


@retry
def test_geocode():
    results = geocoding.geocode(u"Wrocław, Hubska")
    assert results
    assert len(results) > 0


@retry
def test_geocode_override_sensor():
    results = geocoding.geocode(u"Wrocław, Hubska", sensor=True)
    assert results
    assert len(results) > 0


@retry
def test_geocode_components_filters():
    """Test if querying with same route but different component filtering
    returns different locations"""
    # both these cities has this street
    results1 = geocoding.geocode(u"Łubinowa",
                                 components={"locality": "Wrocław"})
    results2 = geocoding.geocode(u"Łubinowa",
                                 components={"locality": "Warszawa"})

    assert results1[0]['geometry']['location'] != results2[0]['geometry'][
        'location']


@retry
def test_geocode_components_without_address():
    """Test if querying explicitely set components returns same location like
    with string address"""
    components = {"route": "Łubinowa", "locality": "Wrocław"}
    address = ",".join(components.values())

    results_with_address = geocoding.geocode(components=components)
    results_without_address = geocoding.geocode(address)
    assert results_with_address[0]['geometry']['location'] == \
        results_without_address[0]['geometry']['location']


@retry
def test_geocode_no_results_exception():
    components = {"administrative_area": "TZ", "country": "FR"}
    with pytest.raises(errors.NoResults):
        geocoding.geocode(components)


@retry
def test_geocode_language():
    results = geocoding.geocode(u"Wrocław, Hubska", language='pl')
    assert 'Polska' in results[0]['formatted_address']


@retry
def test_geocode_region():
    results 
y
def test_geocode_bounds():
    results1 = geocoding.geocode("Winnetka", bounds=(
        (42.1282269, -87.71095989999999), (42.0886089, -87.7708363)))
    results2 = geocoding.geocode("Winnetka", bounds=(
        (34.172684, -118.604794), (34.236144, -118.500938)))
    assert results1[0]['formatted_address'] != results2[0]['formatted_address']


@retry
def test_reverse():
    results = geocoding.reverse(lat=51.213, lon=21.213)
    assert results
    assert len(results) > 0
    assert results[0]['formatted_address']


@retry
def test_reverse_override_sensor():
    results = geocoding.reverse(lat=51.213, lon=21.213, sensor=True)
    assert results
    assert len(results) > 0


@retry
def test_reverse_language():
    results = geocoding.reverse(lat=51.213, lon=21.213, language='pl')
    assert results
    # given lat lon are position somwhere in poland so test if there is
    # 'Polska' in formatted_address of first result
    assert 'Polska' in results[0]['formatted_address']


@pytest.mark.xfail
@retry
def test_exception_when_sensor_bad():
    with pytest.raises(errors.GmapException):
        geocoding.reverse(lat=51.213, lon=21.213, sensor="foo")

    with pytest.raises(errors.GmapException):
        geocoding.geocode(u"Wrocław, Hubska", sensor="foo")

= geocoding.geocode("Toledo", region="us")
    assert 'USA' in results[0]['formatted_address']

    results = geocoding.geocode("Toledo", region="es")
    assert 'Spain' in results[0]['formatted_address']


@retr