# Path: data_model.py
# class StopLocation(GeoModel):
#     stopID       = db.StringProperty()
#     intersection = db.StringProperty()
#     direction    = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
# 
# Path: api/v1/api_utils.py
# def validateDevKey(devKey):
# def conformStopID(stopID):
# def conformRouteID(routeID):
# def inthepast(time):
# def getLocalDatetime():
# def getLocalTimestamp():
# def computeCountdownMinutes(arrivalTime):
# def afterHours():
# def buildErrorResponse(error,description):
# def getDirectionLabel(directionID):
# def handle_500(request, response, exception):
# def recordDeveloperRequest(devKey,type,terms,ipaddr,error='success'):
# def serialize_entities(models):
# def deserialize_entities(data):
# def get_time_from_text(text_with_time, findPos=0):
# GETARRIVALS = "get arrivals"
# GETVEHICLES = "get vehicles"
# GETSTOPS = "get stops"
# GETROUTES = "get routes"
# GETNEARBYSTOPS = "get nearby stops"
# GETSTOPLOCATION = "get stop location"
# 
# Path: stats_and_maps/stats/stathat.py
# STATHAT_MEMCACHE_ERROR_COUNT = 'stathat_error_count'
# STATHAT_MEMCACHE_REQ_COUNT = 'stathat_req_count'
#         URL_BASE = 'https://api.stathat.com'
# class StatHat:
# class StatValueTaskHandler(webapp.RequestHandler):
# class StatFlushHandler(webapp.RequestHandler):
# class StatStopsCollectHandler(webapp.RequestHandler):
#         def http_post_async(self, path, data):
#         def http_post(self, path, data):
#         def post_value(self, user_key, stat_key, value, callback=None):
#         def post_count(self, user_key, stat_key, count, callback=None):
#         def ez_post_value(self, ezkey, stat_name, value):
#         def ez_post_count(self, ezkey, stat_name, count):
# def apiStatCount():
# def apiErrorCount():
# def apiTimeStat(stat_key,value):
# def noop():
#   def post(self):
#     def get(self):
#     def post(self):
# def main():


import os
import wsgiref.handlers
import logging
import webapp2 as webapp
import json
from data_model import StopLocation
from utils.geo import geotypes
from api.v1 import api_utils
from stats_and_maps.stats import stathat
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api.labs.taskqueue import Task
      destination = self.request.get('destination')
      logging.debug('getstops request parameters...  routeID %s destination %s' % (routeID,destination))

      if api_utils.afterHours() is True:
          # don't run these jobs during "off" hours
	      json_response = api_utils.buildErrorResponse('-1','The Metro service is not currently running')
      elif routeID is not '' and destination is '':
          json_response = routeRequest(routeID, None)
          api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETSTOPS,self.request.query_string,self.request.remote_addr);
      elif routeID is not '' and destination is not '':
          json_response = routeRequest(routeID, destination)
          api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETSTOPS,self.request.query_string,self.request.remote_addr);
      else:
          logging.error("API: invalid request")
          json_response = api_utils.buildErrorResponse('-1','Invalid Request parameters. Did you forget to include a routeID?')
          api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETSTOPS,self.request.query_string,self.request.remote_addr,'illegal query string combination');

      #logging.debug('API: json response %s' % json_response);
      # encapsulate response in json
      callback = self.request.get('callback')
      if callback is not '':
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.headers['Access-Control-Allow-Origin'] = '*'
          self.response.headers['Access-Control-Allow-Methods'] = 'GET'
          response = callback + '(' + json.dumps(json_response) + ');'
      else:
          self.response.headers['Content-Type'] = 'application/json'
          response = json.dumps(json_response)

      self.response.out.write(response)

# Path: data_model.py
# class StopLocation(GeoModel):
#     stopID       = db.StringProperty()
#     intersection = db.StringProperty()
#     direction    = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
# 
# Path: api/v1/api_utils.py
# def validateDevKey(devKey):
# def conformStopID(stopID):
# def conformRouteID(routeID):
# def inthepast(time):
# def getLocalDatetime():
# def getLocalTimestamp():
# def computeCountdownMinutes(arrivalTime):
# def afterHours():
# def buildErrorResponse(error,description):
# def getDirectionLabel(directionID):
# def handle_500(request, response, exception):
# def recordDeveloperRequest(devKey,type,terms,ipaddr,error='success'):
# def serialize_entities(models):
# def deserialize_entities(data):
# def get_time_from_text(text_with_time, findPos=0):
# GETARRIVALS = "get arrivals"
# GETVEHICLES = "get vehicles"
# GETSTOPS = "get stops"
# GETROUTES = "get routes"
# GETNEARBYSTOPS = "get nearby stops"
# GETSTOPLOCATION = "get stop location"
# 
# Path: stats_and_maps/stats/stathat.py
# STATHAT_MEMCACHE_ERROR_COUNT = 'stathat_error_count'
# STATHAT_MEMCACHE_REQ_COUNT = 'stathat_req_count'
#         URL_BASE = 'https://api.stathat.com'
# class StatHat:
# class StatValueTaskHandler(webapp.RequestHandler):
# class StatFlushHandler(webapp.RequestHandler):
# class StatStopsCollectHandler(webapp.RequestHandler):
#         def http_post_async(self, path, data):
#         def http_post(self, path, data):
#         def post_value(self, user_key, stat_key, value, callback=None):
#         def post_count(self, user_key, stat_key, count, callback=None):
#         def ez_post_value(self, ezkey, stat_name, value):
#         def ez_post_count(self, ezkey, stat_name, count):
# def apiStatCount():
# def apiErrorCount():
# def apiTimeStat(stat_key,value):
# def noop():
#   def post(self):
#     def get(self):
#     def post(self):
# def main():


# Path: api/v1/getstops.py
import os
import wsgiref.handlers
import logging
import webapp2 as webapp
import json
from data_model import StopLocation
from utils.geo import geotypes
from api.v1 import api_utils
from stats_and_maps.stats import stathat
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api.labs.taskqueue import Task

      destination = self.request.get('destination')
      logging.debug('getstops request parameters...  routeID %s destination %s' % (routeID,destination))

      if api_utils.afterHours() is True:
          # don't run these jobs during "off" hours
	      json_response = api_utils.buildErrorResponse('-1','The Metro service is not currently running')
      elif routeID is not '' and destination is '':
          json_response = routeRequest(routeID, None)
          api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETSTOPS,self.request.query_string,self.request.remote_addr);
      elif routeID is not '' and destination is not '':
          json_response = routeRequest(routeID, destination)
          api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETSTOPS,self.request.query_string,self.request.remote_addr);
      else:
          logging.error("API: invalid request")
          json_response = api_utils.buildErrorResponse('-1','Invalid Request parameters. Did you forget to include a routeID?')
          api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETSTOPS,self.request.query_string,self.request.remote_addr,'illegal query string combination');

      #logging.debug('API: json response %s' % json_response);
      # encapsulate response in json
      callback = self.request.get('callback')
      if callback is not '':
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.headers['Access-Control-Allow-Origin'] = '*'
          self.response.headers['Access-Control-Allow-Methods'] = 'GET'
          response = callback + '(' + json.dumps(json_response) + ');'
      else:
          self.response.headers['Content-Type'] = 'application/json'
          response = json.dumps(json_response)

      self.response.out.write(response)

      stathat.apiStatCount()