from flask import Flask, render_template, url_for, jsonify, send_file
from datetime import datetime
from pathlib import Path

from src.iris_api import Timetable

def compile_javascript(base_path = "static"):
    js_file_ptrs = list(Path(base_path).glob("*.js"))
    js_files = []

    print(js_file_ptrs)

    for file in js_file_ptrs:
        print(file.name)
        fname = f"{file.name}"
        js_files.append(url_for('static', filename=fname))

    return js_files

eva = 8002377

app = Flask(__name__)

@app.route("/favicon.ico")
def favicon():
    return send_file("static/images/favicon.ico")

@app.route("/")
def main_page():
    # print(compile_javascript())

    return render_template(
        'base.html', 
        title="testin", 
        station="Gröbenzell",
        current_time_str=datetime.now().strftime(r"%H:%M:%S"),
        js_files=compile_javascript()
    )

# TODO: add a text entry field to change the EVA
# @app.route("/setup")
# def setup():
#     return ""

@app.route("/update", methods=['POST'])
def update():
    tt = Timetable(eva)

    tt.get_stops()
    
    table_data = tt.str_stops()
    # print(table_data[0])


    result = {'status': 'success'}
    # result.update(data)
    result["table_data"] = table_data
    return jsonify(result)