#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import unittest
import requests_mock
from mock import Mock
from urlparse import urlparse, parse_qs

from cartodb_services.here import HereMapsRoutingIsoline
from cartodb_services.here.exceptions import BadGeocodingParams
from cartodb_services.here.exceptions import NoGeocodingParams
from cartodb_services.here.exceptions import MalformedResult

requests_mock.Mocker.TEST_PREFIX = 'test_'


@requests_mock.Mocker()
class HereMapsRoutingIsolineTestCase(unittest.TestCase):
    EMPTY_RESPONSE = """{
      "response": {
        "metaInfo": {
          "timestamp": "2016-02-10T10:42:21Z",
          "mapVersion": "8.30.61.107",
          "moduleVersion": "7.2.65.0-1222",
          "interfaceVersion": "2.6.20"
        },
        "center": {
          "latitude": 33,
          "longitude": 0.9999999
        },
        "isoline": [
          {
            "range": 1000,
            "component": [
              {
                "id": 0,
                "shape": []
              }
            ]
          }
        ],
        "start": {
          "linkId": "+1025046831",
          "mappedPosition": {
            "latitude": 32.968725,
            "longitude": 0.9993629
          },
          "originalPosition": {
            "latitude": 33,
            "longitude": 0.9999999
          }
        }
      }
    }"""

    ERROR_RESPONSE = """{
      "_type": "ns2:RoutingServiceErrorType",
      "type": "ApplicationError",
      "subtype": "InitIsolineSearchFailed",
      "details": "Error is NGEO_ERROR_UNKNOWN",
      "additionalData": [
        {
          "key": "error_code",
          "value": "NGEO_ERROR_UNKNOWN"
        }
      ],
      "metaInfo": {
        "timestamp": "2016-02-10T10:39:35Z",
        "mapVersion": "8.30.61.107",
        "moduleVersion": "7.2.65.0-1222",
        "interfaceVersion": "2.6.20"
      }
    }"""

    GOOD_RESPONSE = """{
      "response": {
        "metaInfo": {
          "timestamp": "2016-02-10T10:42:21Z",
          "mapVersion": "8.30.61.107",
          "moduleVersion": "7.2.65.0-1222",
          "interfaceVersion": "2.6.20"
        },
        "center": {
          "latitude": 33,
          "longitude": 0.9999999
        },
        "isoline": [
          {