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

import os
import datetime
from lxml import etree
from skylines import files
from tg import config


def helper_path(helper):
    return os.path.join(config['skylines.analysis.path'], helper)


def import_datetime_attribute(node, name):
    if node is None or not name in node.attrib:
        return None
    try:
        return datetime.datetime.strptime(node.attrib[name], '%Y-%m-%dT%H:%M:%SZ')
    except ValueError:
        return None


def find_contest(root, name):
    for contest in root.findall('contest'):
        if contest.attrib['name'] == name:
            return contest
    return None


def find_trace(contest, name):
    for contest in contest.findall('trace'):
        if contest.attrib['name'] == name:
            return contest
    return None


def analyse_flight(flight):
    path = files.filename_to_path(flight.filename)

    with os.popen(helper_path('AnalyseFlight') + ' "' + path + '"') as f:
        try:
            doc = etree.parse(f)
        except etree.Error:
            return False

    root = doc.getroot()

    times = root.find('times')
    flight.takeoff_time = import_datetime_attribute(times, "takeoff")
    flight.landing_time = import_datetime_attribute(times, "landing")

    contest = find_contest(root, 'olc_plus')
    if contest is not None:
        trace = find_trace(contest, 'classic')
        if trace is not None:
            flight.olc_classic_distance = int(float(trace.attrib['distance']))
        else:
            flight.olc_classic_distance = None

        trace = find_trace(contest, 'triangle')
        if trace is not None:
            flight.olc_triangle_distance = int(float(trace.attrib['distance']))
        else:
            flight.olc_triangle_distance = None

        trace = find_trace(contest, 'plus')
        if trace is not None:
            flight.olc_plus_score = int(float(trace.attrib['score']))
        else:
            flight.olc_plus_score = None

    return True


def flight_path(flight, max_points = 1000):
    path = files.filename_to_path(flight.filename)
    f = os.popen(helper_path('FlightPath') + ' --max-points=' + str(max_points) + ' "' + path + '"')

    path = []
    for line in f:
        line = line.split()
        path.append((int(line[0]), float(line[1]), float(line[2]), int(line[3])))
    return path

@@ -116,16 +116,56 @@
  config%0A
+from skylines.model.geo import Location%0A
 %0A%0Adef he
@@ -251,16 +251,315 @@
 lper)%0A%0A%0A
+def import_location_attribute(node):%0A    if node is None:%0A        return None%0A%0A    try:%0A        latitude = float(node.attrib%5B'latitude'%5D)%0A        longitude = float(node.attrib%5B'longitude'%5D)%0A        return Location(latitude=latitude, longitude=longitude)%0A    except ValueError:%0A        return None%0A%0A%0A
 def impo
@@ -1576,16 +1576,248 @@
 ding%22)%0A%0A
+    locations = root.find('locations')%0A    if locations:%0A        flight.takeoff_location = import_location_attribute(locations.find('takeoff'))%0A        flight.landing_location = import_location_attribute(locations.find('landing'))%0A%0A
     cont

Read takeoff and landing location from AnalyseFlight output