import geojson
from pkg_resources import resource_stream

from flask import render_template, request, jsonify, redirect
from application import app, mongo
from application.models import *

from bson.objectid import ObjectId

from hamtools.geolog import Log
from hamtools.ctydat import CtyDat
from hamtools.qrz import NotFound, Session as QrzSession


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/views/<view>')
def durandal_view(view):
    return render_template('views/' + view)


class DummyQRZ(object):
    @staticmethod
    def qrz(callsign):
        if callsign.lower() == 'n1ywb':
            return dict(lon=-72.486441, lat=44.196575)
        raise NotFound("Unimplemented")

ctydatflo = resource_stream('hamtools', "ctydat/cty.dat")
ctydat = CtyDat(ctydatflo)

def georeferencelog(logfile, key):
    CABRILLO_HEADER = 'START-OF-LOG'

    line = logfile.read(len(CABRILLO_HEADER))
    logfile.seek(0)

    if line == CABRILLO_HEADER:
        app.logger.info("Opened Cabrillo format log %r" % logfile)
        qsolog = Log.from_cabrillo(logfile)
    else:
        app.logger.info("Opened ADIF format log %r" % logfile)
        qsolog = Log.from_adif(logfile)

    sess = None
    if key:
        sess = QrzSession(key=key)
    qsolog.georeference(sess, ctydat)

    points, lines = qsolog.geojson()
    d = dict(points=points, lines=lines)
    return d


@app.route('/api/upload/post', methods=['POST'])
def upload_log():
    # app.logger.debug(request.files['file'].read())
    key = request.form['key']
    state = request.form['state']
    r = georeferencelog(request.files['file'], key)
    operator = None # georeferencelog() needs to return this
    filename = None # it's in the file obj somewhere
    r = mongo.db.logs.insert_one({
        'operator': operator,
        'filename': filename,
        'geojson': r
    })
    return jsonify({'_id': str(r.inserted_id)})


@app.route('/api/log/<_id>', methods=['GET'])
def download_geolog(_id):
    r = mongo.db.logs.find_one({'_id': ObjectId(_id)})
    return jsonify(r['geojson'])


@@ -216,16 +216,50 @@
 ObjectId
+%0Afrom bson.errors import InvalidId
 %0A%0Afrom h
@@ -2023,16 +2023,109 @@
 g(_id):%0A
+    try:%0A        oid = ObjectId(_id)%0A    except InvalidId:%0A        return %22invalid oid%22, 400%0A
     r = 
@@ -2159,24 +2159,68 @@
 d': 
-ObjectId(_
+o
 id
-)
 %7D)%0A
+    if r is None:%0A        return %22log not found%22, 404%0A
     

improve api errors