#  Copyright (c) 2001-2019, Kisio Digital and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
#     the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Kisio Digital (http://www.kisiodigital.com/).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# IRC #navitia on freenode
# https://groups.google.com/d/forum/navitia
# www.navitia.io
from __future__ import absolute_import, print_function, unicode_literals, division
import requests, pybreaker, logging
from jormungandr.street_network.parking.abstract_parking_module import AbstractParkingModule


class Augeas(AbstractParkingModule):
    def __init__(self, service_url, max_park_duration=1200):
        self.service_url = service_url
        self.max_park_duration = max_park_duration
        # TODO: put into a config when the POC is validated
        self.breaker = pybreaker.CircuitBreaker(fail_max=500, reset_timeout=60)
        self.logger = logging.getLogger(__name__)

    def _breaker_wrapper(self, fun, *args, **kwargs):
        try:
            return self.breaker.call(fun, *args, **kwargs)
        except Exception as e:
            self.logger.exception('error occurred when requesting Augeas: {}'.format(str(e)))
            raise

    def _request_in_batch(self, coords):
        data = {
            "n": 1,
            "walking_speed": 1.11,
            "max_park_duration": self.max_park_duration,
            "coords": [[c.lon, c.lat] for c in coords],
        }
        url = requests.compat.urljoin(self.service_url, '/v0/park_duration')
        r = requests.post(url=url, json=data)
        return r.json().get('durations')

    def _request(self, coord):
        params = {
            'lon': coord.lon,
            'lat': coord.lat,
            'n': 1,
            "walking_speed": 1.11,
            'max_park_duration': self.max_park_duration,
        }
        url = requests.compat.urljoin(self.service_url, '/v0/park_duration')
        r = requests.get(url=url, params=params)
        durations = r.json().get('durations')
        if not durations:
            return self.max_park_duration
        return durations[0].get('duration', self.max_park_duration)

    def get_parking_duration(self, coord):
        return self._breaker_wrapper(self._request, coord=coord)

    def get_leave_parking_duration(self, coord):
        return self._breaker_wrapper(self._request, coord=coord)

    def get_parking_duration_in_batch(self, coords):
        return self._breaker_wrapper(self._request_in_batch, coords=coords)

    def get_leave_duration_in_batch(self, coords):
        return self._breaker_wrapper(self._request_in_batch, coords=coords)

@@ -1309,17 +1309,23 @@
 requests
-,
+%0Aimport
  pybreak
@@ -1326,17 +1326,23 @@
 ybreaker
-,
+%0Aimport
  logging
@@ -1342,16 +1342,47 @@
 logging%0A
+from datetime import timedelta%0A
 from jor
@@ -1467,16 +1467,93 @@
 Module%0A%0A
+DEFAULT_MAX_PARKING_DURATION = int(timedelta(seconds=1200).total_seconds())%0A%0A
 %0Aclass A
@@ -1636,20 +1636,44 @@
 uration=
-1200
+DEFAULT_MAX_PARKING_DURATION
 ):%0A     
@@ -1817,16 +1817,75 @@
 lidated%0A
+        one_minute = timedelta(seconds=60).total_seconds()%0A
         
@@ -1937,10 +1937,9 @@
 max=
-50
+1
 0, r
@@ -1951,19 +1951,98 @@
 timeout=
-60)
+one_minute)%0A        self.request_timeout = timedelta(seconds=0.1).total_seconds()%0A
 %0A       
@@ -2712,16 +2712,46 @@
 son=data
+, timeout=self.request_timeout
 )%0A      
@@ -3142,16 +3142,46 @@
 s=params
+, timeout=self.request_timeout
 )%0A      

fix after review