# Path: prosoar/task/task.py
# class Task:
# 
#     def __init__(self):
#         self.__list = []
# 
#         # type: fai, triangle, outreturn, goal, racing, aat, mixed, touring
#         self.type = 'racing'
# 
#         self.distance = 0
# 
#         # aat_min_time (s)
#         self.aat_min_time = 3 * 3600
# 
#         # start_max_speed (m/s), start_max_height (m)
#         self.start_max_speed = 60
#         self.start_max_height = 0
# 
#         # start_max_height_ref (AGL, MSL)
#         self.start_max_height_ref = 'MSL'
# 
#         # finish_min_height (m)
#         self.finish_min_height = 0
#         self.finish_min_height_ref = 'AGL'
# 
#         # fai_finish rule
#         self.fai_finish = 0
# 
#         self.min_points = 2
#         self.max_points = 13
#         self.homogeneous_tps = 0
#         self.is_closed = 0
#         self.task_scored = 0
# 
#     def __len__(self):
#         return len(self.__list)
# 
#     def __iter__(self):
#         return iter(self.__list)
# 
#     def append(self, tp):
#         if not isinstance(tp, Turnpoint):
#             raise TypeError("Turnpoint expected")
# 
#         self.__list.append(tp)
# 
# Path: prosoar/task/turnpoint.py
# class Turnpoint(GeoPoint):
# 
#     def __init__(self):
#         self.name = ''
#         self.id = 0
#         self.comment = ''
#         self.altitude = 0
# 
#         self.sector = Sector()
# 
#     def __str__(self):
#         return '{}'.format(self.name)


from prosoar.task.task import Task
from prosoar.task.turnpoint import Turnpoint
import json
import urllib2


def task_distance(task, distance):
    task.distance = float(distance)


def parse_turnpoint(task, tp):
    turnpoint = Turnpoint()

    if not tp['name'] or not tp['lon'] or not tp['lat']:
        raise RuntimeError('Turnpoint not complete')

    turnpoint.name = tp['name']
    turnpoint.lon = float(tp['lon'])
    turnpoint.lat = float(tp['lat'])

    if turnpoint.name == 'Free turnpoint':
        turnpoint.altitude = get_altitude(turnpoint.lon, turnpoint.lat)
    else:
        turnpoint.altitude = float(tp['altitude'])

    turnpoint.comment = tp['comment']

    if tp['type']:
        parse_sector(turnpoint.sector, tp)

    task.append(turnpoint)


def parse_sector(sector, tp):

# Path: prosoar/task/task.py
# class Task:
# 
#     def __init__(self):
#         self.__list = []
# 
#         # type: fai, triangle, outreturn, goal, racing, aat, mixed, touring
#         self.type = 'racing'
# 
#         self.distance = 0
# 
#         # aat_min_time (s)
#         self.aat_min_time = 3 * 3600
# 
#         # start_max_speed (m/s), start_max_height (m)
#         self.start_max_speed = 60
#         self.start_max_height = 0
# 
#         # start_max_height_ref (AGL, MSL)
#         self.start_max_height_ref = 'MSL'
# 
#         # finish_min_height (m)
#         self.finish_min_height = 0
#         self.finish_min_height_ref = 'AGL'
# 
#         # fai_finish rule
#         self.fai_finish = 0
# 
#         self.min_points = 2
#         self.max_points = 13
#         self.homogeneous_tps = 0
#         self.is_closed = 0
#         self.task_scored = 0
# 
#     def __len__(self):
#         return len(self.__list)
# 
#     def __iter__(self):
#         return iter(self.__list)
# 
#     def append(self, tp):
#         if not isinstance(tp, Turnpoint):
#             raise TypeError("Turnpoint expected")
# 
#         self.__list.append(tp)
# 
# Path: prosoar/task/turnpoint.py
# class Turnpoint(GeoPoint):
# 
#     def __init__(self):
#         self.name = ''
#         self.id = 0
#         self.comment = ''
#         self.altitude = 0
# 
#         self.sector = Sector()
# 
#     def __str__(self):
#         return '{}'.format(self.name)


# Path: prosoar/task/json_reader.py
from prosoar.task.task import Task
from prosoar.task.turnpoint import Turnpoint
import json
import urllib2



def task_distance(task, distance):
    task.distance = float(distance)


def parse_turnpoint(task, tp):
    turnpoint = Turnpoint()

    if not tp['name'] or not tp['lon'] or not tp['lat']:
        raise RuntimeError('Turnpoint not complete')

    turnpoint.name = tp['name']
    turnpoint.lon = float(tp['lon'])
    turnpoint.lat = float(tp['lat'])

    if turnpoint.name == 'Free turnpoint':
        turnpoint.altitude = get_altitude(turnpoint.lon, turnpoint.lat)
    else:
        turnpoint.altitude = float(tp['altitude'])

    turnpoint.comment = tp['comment']

    if tp['type']:
        parse_sector(turnpoint.sector, tp)

    task.append(turnpoint)


def parse_sector(sector, tp):

    if tp['type'] == 'startline':