# Path: prosoar/igc/igc_parser.py
# class SimpleParser(BaseParser):
# 
#     def parse(self, data):
#         self.__fixes = []
#         BaseParser.parse(self, data)
#         return self.__fixes
# 
#     def handle_fix(self, fix):
#         self.__fixes.append(fix)
# 
# Path: prosoar/igc/analyse_flight.py
# def analyse_flight(filename):
#     root = _analyse_flight(filename)
# 
#     flight = {}
# 
#     if 'contest' in root:
#         child = root['contest']
#         flight[child.get("name")] = parse_contest(child)
# 
#     if 'times' in root:
#         child = root['times']
#         flight['takeoff'] = child.get("takeoff")
#         flight['landing'] = child.get("landing")
# 
#     return flight
# 
# Path: prosoar/igc/trace.py
# def for_openlayers(data):
#     """
#     convert latlon from WGS84 to epsg:3785 (google spherical mercator)
#     then simplify the trace with douglas-peucker's algorithm
#     in the end, each fix contains of [x, y, lod]
#     """
# 
#     fixes = []
# 
#     proj_wgs84 = Proj(proj='latlong', datum='WGS84')  # init = 'epsg:4326')
#     proj_smerc = Proj(init='epsg:3785')
# 
#     for fix in data:
#         x, y = transform(proj_wgs84, proj_smerc,
#                          fix.latlon.lon, fix.latlon.lat)
#         fixes.append([int(x), int(y)])
# 
#     fixes = douglas_peucker(fixes)
# 
#     fixes = compress(fixes)
# 
#     return fixes


from flask import Blueprint, request, jsonify, current_app
from prosoar.igc.igc_parser import SimpleParser
from prosoar.igc.analyse_flight import analyse_flight
from prosoar.igc.trace import for_openlayers
import os
import tempfile
bp = Blueprint('igc', __name__)


@bp.route('/upload', methods=['POST'])
def upload():

    uploaded_file = save_uploaded_file()

    if not uploaded_file:
        return jsonify({'success': False})

    flight = {}

    parser = SimpleParser()
    flight['trace'] = for_openlayers(
        parser.parse(file(uploaded_file["tempname"])))
    flight['info'] = analyse_flight(uploaded_file["tempname"])
    flight['filename'] = uploaded_file["filename"]

    os.remove(uploaded_file["tempname"])

    if flight['trace'] is not None:
        return jsonify({'success': True, 'flight': flight})
    else:
        return jsonify({'success': False})


# save uploaded file to temp file
def save_uploaded_file():
    file = request.files['igcfile-upload-file']

# Path: prosoar/igc/igc_parser.py
# class SimpleParser(BaseParser):
# 
#     def parse(self, data):
#         self.__fixes = []
#         BaseParser.parse(self, data)
#         return self.__fixes
# 
#     def handle_fix(self, fix):
#         self.__fixes.append(fix)
# 
# Path: prosoar/igc/analyse_flight.py
# def analyse_flight(filename):
#     root = _analyse_flight(filename)
# 
#     flight = {}
# 
#     if 'contest' in root:
#         child = root['contest']
#         flight[child.get("name")] = parse_contest(child)
# 
#     if 'times' in root:
#         child = root['times']
#         flight['takeoff'] = child.get("takeoff")
#         flight['landing'] = child.get("landing")
# 
#     return flight
# 
# Path: prosoar/igc/trace.py
# def for_openlayers(data):
#     """
#     convert latlon from WGS84 to epsg:3785 (google spherical mercator)
#     then simplify the trace with douglas-peucker's algorithm
#     in the end, each fix contains of [x, y, lod]
#     """
# 
#     fixes = []
# 
#     proj_wgs84 = Proj(proj='latlong', datum='WGS84')  # init = 'epsg:4326')
#     proj_smerc = Proj(init='epsg:3785')
# 
#     for fix in data:
#         x, y = transform(proj_wgs84, proj_smerc,
#                          fix.latlon.lon, fix.latlon.lat)
#         fixes.append([int(x), int(y)])
# 
#     fixes = douglas_peucker(fixes)
# 
#     fixes = compress(fixes)
# 
#     return fixes


# Path: prosoar/views/igc.py
from flask import Blueprint, request, jsonify, current_app
from prosoar.igc.igc_parser import SimpleParser
from prosoar.igc.analyse_flight import analyse_flight
from prosoar.igc.trace import for_openlayers
import os
import tempfile

bp = Blueprint('igc', __name__)


@bp.route('/upload', methods=['POST'])
def upload():

    uploaded_file = save_uploaded_file()

    if not uploaded_file:
        return jsonify({'success': False})

    flight = {}

    parser = SimpleParser()
    flight['trace'] = for_openlayers(
        parser.parse(file(uploaded_file["tempname"])))
    flight['info'] = analyse_flight(uploaded_file["tempname"])
    flight['filename'] = uploaded_file["filename"]

    os.remove(uploaded_file["tempname"])

    if flight['trace'] is not None:
        return jsonify({'success': True, 'flight': flight})
    else:
        return jsonify({'success': False})


# save uploaded file to temp file
def save_uploaded_file():
    file = request.files['igcfile-upload-file']

    if not file: