from flask import Flask, request, render_template
from flask_restful import Resource, Api, reqparse, url_for
from datetime import date, timedelta
import configparser
import csv
import datetime
import timeago
import ast
import glob
import json, operator
import requests
import sqlite3
api_app = Flask(__name__)
api = Api(api_app)

# TODO this is duplicated from kiss_and_db.py, can I avoid that?
import constants

def read_config():
    config = configparser.ConfigParser()
    config.read('config.ini')
    return config

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

def get_db_connection():
    conn = sqlite3.connect('database.db')
    conn.row_factory = dict_factory
    return conn

def select_all_frames(conn):
    """
    Query all rows in the frames table
    :param conn: the Connection object
    :return:
    """
    cur = conn.cursor()
    cur.execute("SELECT * FROM frames")
    rows = cur.fetchall()
    return rows

def select_all_stations(conn):
    """
    Query all rows in the stations table
    :param conn: the Connection object
    :return:
    """
    cur = conn.cursor()
    cur.execute("SELECT * FROM stations ORDER BY last_heard_unix DESC")
    rows = cur.fetchall()
    return rows

def unique_stations(conn):
    """
    Query all rows in the frames table
    :param conn: the Connection object
    :return:
    """
    cur = conn.cursor()
    cur.execute('SELECT *, MAX(id), COUNT(id) FROM frames GROUP BY "from" ORDER BY MAX(id) DESC')
    rows = cur.fetchall()
    return rows

def select_frames(conn, n, url_params):

    # Should pass this a dict of fields and values (request.args)
    # TODO clean data before sending to DB
    # Filter out any keys that don't match db fields
    # From https://stackoverflow.com/a/20256491
    dictfilt = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
    field_where = dictfilt(url_params, constants.db_frames_fields)
    # Then loop through fields to create query parts
    # From https://stackoverflow.com/a/73512269/2152245
    field_where_str = ' AND '.join([f'"{k}" LIKE \'{v}\'' for k,v in field_where.items()])

    cur = conn.cursor()
    # Workaround to deal with missing value in WHERE
    field_where_query = "" if field_where_str == "" else "WHERE "+field_where_str
    sql = 'SELECT * FROM frames {field_where_query} ORDER BY created DESC LIMIT {n}'.format(field_where_query=field_where_query, n=n)
    print(sql)
    cur.execute(sql)
    rows = cur.fetchall()
    return rows

@api_app.route('/')
def index():

    # Get list of recent packets using API
    # TODO use relative path
    frames = json.loads(requests.get("https://digi.w1cdn.net/aprs_api/packets").text)['data']
    for frame in frames:
        if frame['created'] != None:
            frame['time_ago'] = timeago.format(frame['created_unix'], datetime.datetime.now())


    # Play with function to create station list
    #stations = select_all_stations(get_db_connection())
    #print(url_for("static", filename="test.txt", _external=True))
    # this should work: stations = json.loads(requests.get(url_for("stations", _external=True)).text)['data']
    stations = json.loads(requests.get("https://digi.w1cdn.net/aprs_api/stations").text)['data']
    # Convert unix time to datetime on the fly because I'm lazy right now
    for station in stations:
        if station['last_heard_unix'] != None:
            station['last_heard'] = datetime.datetime.utcfromtimestamp(station['last_heard_unix'])
            station['time_ago'] = timeago.format(station['last_heard_unix'], datetime.datetime.now())


    return render_template('index.html',
                            station_call = config['Settings']['station_call'],
                            station_lat = config['Settings']['station_lat'],
                            station_lon = config['Settings']['station_lon'],
                            frames = frames,
                            stations = stations)

class Packets(Resource):
    def get(self):
        # Handle arguments that may or may not exist
        try:
            n = int(request.args.get('n'))
        except:
            n = 10

        conn = get_db_connection()
        # Limit to number of records requested
        data = select_frames(conn, n = n, url_params = request.args.to_dict())
        # Sort by created date, descending (https://stackoverflow.com/a/45266808)
        #data.sort(key=operator.itemgetter('created'), reverse=True)
        return {'data':data}, 200  # return data and 200 OK code

class Stations(Resource):
    def get(self):
        # Handle arguments that may or may not exist
        try:
            n = int(request.args.get('n'))
        except:
            n = 10

        conn = get_db_connection()
        # Limit to number of records requested
        data = select_all_stations(conn)
        # Sort by created date, descending (https://stackoverflow.com/a/45266808)
        #data.sort(key=operator.itemgetter('created'), reverse=True)
        return {'data':data}, 200  # return data and 200 OK code

# Read config
config = read_config()
log_folder = config['Settings']['log_folder']

# Start subprocess to watch KISS connection
import subprocess
#proc = subprocess.Popen(["python3","kiss_and_db.py"])
# Combine under one process https://stackoverflow.com/a/13143013/2152245
proc = subprocess.Popen("exec " + "python3 kiss_and_db.py", stdout=subprocess.PIPE, shell=True)
print("kiss_and_db.py as subprocess pid "+str(proc.pid))

# The packets endpoint
api.add_resource(Packets, '/packets')
# The stations endpoint
api.add_resource(Stations, '/stations')

if __name__ == '__main__':
    api_app.run(debug=True, host='0.0.0.0', port=5001)  # run our Flask app