# Path: roadmaptools/calculate_curvature.py
# def get_length(coords):
#     length = 0
#     for i in range(0, len(coords) - 1):
#         point1 = get_node(coords[i])
#         point2 = get_node(coords[i + 1])
#         length += get_distance_between_coords(point1, point2)
#     return length
# 
# Path: roadmaptools/printer.py
# def print_info(info, file=sys.stdout, end="\n"):
#     print("[{0}]: {1}".format(datetime.now().strftime('%H:%M:%S'), info), file=file, end=end)
# 
# Path: roadmaptools/init.py



from typing import Tuple
from roadmaptools.calculate_curvature import get_length
from geojson import Feature
from roadmaptools.printer import print_info
from roadmaptools.init import config
import geojson
import codecs
import sys
import argparse
import time

MPH = 1.60934  # miles to km

# https://wiki.openstreetmap.org/wiki/OSM_tags_for_routing/Maxspeed
SPEED_CODE_DICT = {'CZ': {'default': 50, 'urban': 50, 'living_street': 20, 'pedestrian': 20,
                          'motorway': 80, 'motorway_link': 80, 'trunk': 80, 'trunk_link': 80},

                   'US': {'default': 30, 'living_street': 20, 'residential': 25, 'primary': 45,
                          'motorway': 55, 'motorway_link': 55, 'trunk': 5, 'trunk_link': 55,
                          'secondary': 55, 'tertiary': 55, 'unclassified': 55}}

DEFAULT_UNIT = "kmh"
UNIT_MAP = {'US': "mph", "CZ": "kmh"}

TO_METERS_PER_SECOND = {"kmh": 3.6, "mph": 2.2369362920544}


# length is computed here too!!!
def estimate_posted_speed(input_filename: str, output_filename: str):
    print_info('Estimating travel speed')
    start_time = time.time()

    input_stream = codecs.open(input_filename, encoding='utf8')
    output_stream = open(output_filename, 'w')

    print_info("Loading file from: {}".format(input_filename))
    geojson_file = load_geojson(input_stream)

    print_info("Computing speed")
    geojson_out = get_geojson_with_speeds(geojson_file)


# Path: roadmaptools/calculate_curvature.py
# def get_length(coords):
#     length = 0
#     for i in range(0, len(coords) - 1):
#         point1 = get_node(coords[i])
#         point2 = get_node(coords[i + 1])
#         length += get_distance_between_coords(point1, point2)
#     return length
# 
# Path: roadmaptools/printer.py
# def print_info(info, file=sys.stdout, end="\n"):
#     print("[{0}]: {1}".format(datetime.now().strftime('%H:%M:%S'), info), file=file, end=end)
# 
# Path: roadmaptools/init.py



# Path: roadmaptools/estimate_speed_from_osm.py
from typing import Tuple
from roadmaptools.calculate_curvature import get_length
from geojson import Feature
from roadmaptools.printer import print_info
from roadmaptools.init import config
import geojson
import codecs
import sys
import argparse
import time

MPH = 1.60934  # miles to km

# https://wiki.openstreetmap.org/wiki/OSM_tags_for_routing/Maxspeed
SPEED_CODE_DICT = {'CZ': {'default': 50, 'urban': 50, 'living_street': 20, 'pedestrian': 20,
                          'motorway': 80, 'motorway_link': 80, 'trunk': 80, 'trunk_link': 80},

                   'US': {'default': 30, 'living_street': 20, 'residential': 25, 'primary': 45,
                          'motorway': 55, 'motorway_link': 55, 'trunk': 5, 'trunk_link': 55,
                          'secondary': 55, 'tertiary': 55, 'unclassified': 55}}

DEFAULT_UNIT = "kmh"
UNIT_MAP = {'US': "mph", "CZ": "kmh"}

TO_METERS_PER_SECOND = {"kmh": 3.6, "mph": 2.2369362920544}


# length is computed here too!!!
def estimate_posted_speed(input_filename: str, output_filename: str):
    print_info('Estimating travel speed')
    start_time = time.time()

    input_stream = codecs.open(input_filename, encoding='utf8')
    output_stream = open(output_filename, 'w')

    print_info("Loading file from: {}".format(input_filename))
    geojson_file = load_geojson(input_stream)

    print_info("Computing speed")
    geojson_out = get_geojson_with_speeds(geojson_file)


    print_info("Saving file to: {}".format(output_filename))
