# -*- coding: utf-8 -*-

from database.database_access import get_dao
from gtfslib.model import Route, StopTime, Shape
from gtfsplugins import decret_2015_1610
from database.database_access import get_dao
from services.check_urban import check_urban_category

def get_route(agency_id, routeId):
	dao = get_dao(agency_id)
	parsedRoute = dict()
	listPoints = list()

	route = dao.routes(fltr=Route.route_id == routeId)
	print(route)
	parsedRoute["id"] = route[0].route_id
	parsedRoute["name"] = route[0].route_long_name
	parsedRoute["category"] = check_urban_category(route[0].trips)
	# All trips have same trip_id so we may use only the first : route.trips[0]
	_get_route_shapepoints(dao, route[0].trips[0].shape_id, listPoints)
	_get_route_stops(dao, route[0].trips[0].trip_id, listPoints)
	parsedRoute['points'] = listPoints
			
	return parsedRoute


'''	Private methods '''

def _get_route_shapepoints(dao, shapeId, listPoints):
	countPoints = 0

	shape = dao.shape(shapeId)
	
	if shape is not None:
		for point in shape.points:
			countPoints += 1
			parsedPoint = dict()
			parsedPoint['id'] = str(point.feed_id)+str(point.shape_id)+str(point.shape_pt_sequence)
			parsedPoint['name'] = "no_name"
			parsedPoint['is_stop'] = False
			parsedPoint['location'] = {'lat': point.shape_pt_lat, 'lng': point.shape_pt_lon}
			listPoints.append(parsedPoint)

	print('Nb shape points = '+str(countPoints))
	return

def _get_route_stops(dao, tripId, listPoints):
	countStops = 0

	stoptimes = dao.stoptimes(fltr=StopTime.trip_id == tripId)

	for stoptime in stoptimes:
		stop = dao.stop(stoptime.stop_id)

		if stop is not None:
			countStops += 1
			parsedStop = dict()
			parsedStop['id'] = stop.stop_id
			parsedStop['name'] = stop.stop_name
			parsedStop['is_stop'] = True
			parsedStop['location'] = {'lat': stop.stop_lat, 'lng': stop.stop_lon}
			if not _check_already_in(listPoints, parsedStop):
				listPoints.append(parsedStop)

	print('Nb stop points = '+str(countStops))
	return 


def _same_position(pointA, pointB):
	return pointA['location'].get('lat') == pointB['location'].get('lat') and pointA['location'].get('lng') == pointB['location'].get('lng')

def _set_is_stop(pointToModifiy, stop):
	pointToModifiy['is_stop'] = True
	pointToModifiy['name'] = stop['name']
	pointToModifiy['id'] = stop['id']

def _check_already_in(points, aPoint):
	for point in points:
		if _same_position(point, aPoint):
			_set_is_stop(point, aPoint)
			return True
	return False
@@ -22,53 +22,8 @@
 *-%0A%0A
-from database.database_access import get_dao%0A
 from
@@ -76,142 +76,64 @@
 rom 
-gtfsplugins import decret_2015_1610%0Afrom database.database_access import get_dao%0Afrom services.check_urban import check_urban_category
+database.database_access import get_dao, get_urban_by_id
 %0A%0Ade
@@ -160,17 +160,18 @@
 d, route
-I
+_i
 d):%0A%09dao
@@ -284,19 +284,23 @@
 == route
-Id)
+_id)%5B0%5D
 %0A%09print(
@@ -304,16 +304,28 @@
 nt(route
+, flush=True
 )%0A%09parse
@@ -340,27 +340,24 @@
 id%22%5D = route
-%5B0%5D
 .route_id%0A%09p
@@ -382,19 +382,16 @@
  = route
-%5B0%5D
 .route_l
@@ -430,43 +430,49 @@
 %5D = 
-check
+get
 _urban_
-category(route%5B0%5D.trips
+by_id(agency_id, route.route_id
 )%0A%09#
@@ -572,35 +572,32 @@
 oints(dao, route
-%5B0%5D
 .trips%5B0%5D.shape_
@@ -640,19 +640,16 @@
 o, route
-%5B0%5D
 .trips%5B0

use precalculated category