from flask import Flask
from flask import render_template, send_file, request, redirect, url_for
import typst
import json
from io import BytesIO
import random

from pprint import pprint

app = Flask(__name__)

compiler = typst.Compiler(font_paths=["fonts/"])

@app.route("/")
def index():
    cours_base = [
        # ALGEBRE
        {
            "ue": "algebre",
            "name": "Algèbre",
            "color": "#ff80b3",
            "cm": [
                {
                    "prof": "Jean-Benoît Bost",
                    "day": 0, "start": "08:15", "end": "10:15", "room": "Amphi Yoccoz",
                },
                {
                    "prof": "Jean-Benoît Bost",
                    "day": 3, "start": "14:00", "end": "16:00", "room": "Amphi Yoccoz",
                },
            ],
            "td": [
                {
                    "group": 1,
                    "prof": "Kévin Destagnol",
                    "slots": [
                        {"day": 0, "start": "10:30", "end": "13:00", "room": "0A2"},
                        {"day": 3, "start": "16:15", "end": "18:45", "room": "0A2"},
                    ],
                },
                {
                    "group": 2,
                    "prof": "Pierre Lorenzon",
                    "slots": [
                        {"day": 0, "start": "10:30", "end": "13:00", "room": "0A7"},
                        {"day": 3, "start": "16:15", "end": "18:45", "room": "0E1"},
                    ],
                },
            ],
        },
        # ANALYSE
        {
            "ue": "analyse",
            "name": "Analyse",
            "color": "#80ffcc",
            "cm": [
                {
                    "prof": "Blanche Buet",
                    "day": 0, "start": "14:00", "end": "16:00", "room": "OA2",
                },
                {
                    "prof": "Blanche Buet",
                    "day": 2, "start": "08:15", "end": "10:15", "room": "OA1",
                },
            ],
            "td": [
                {
                    "group": 1,
                    "prof": "Frédéric Valet",
                    "slots": [
                        {"day": 0, "start": "16:15", "end": "18:45", "room": "0A2"},
                        {"day": 2, "start": "10:30", "end": "13:00", "room": "0A1"},
                    ],
                },
                {
                    "group": 2,
                    "prof": "Sebastian Bechtel",
                    "slots": [
                        {"day": 0, "start": "16:15", "end": "18:45", "room": "0A7"},
                        {"day": 2, "start": "10:30", "end": "13:00", "room": "0D1"},
                    ],
                },
            ],
        },
        # PROBAS
        {
            "ue": "probas",
            "name": "Probabilités",
            "color": "#ffcc99",
            "cm": [
                {
                    "prof": "Pierre-Loïc Méliot",
                    "day": 1, "start": "08:15", "end": "10:15", "room": "Amphi Yoccoz",
                },
                {
                    "prof": "Pierre-Loïc Méliot",
                    "day": 3, "start": "08:15", "end": "10:15", "room": "Amphi Yoccoz",
                },
            ],
            "td": [
                {
                    "group": 1,
                    "prof": "Sophie Lemaire",
                    "slots": [
                        {"day": 1, "start": "10:30", "end": "13:00", "room": "0D1"},
                        {"day": 3, "start": "10:30", "end": "13:00", "room": "0E1"},
                    ],
                },
                {
                    "group": 2,
                    "prof": "A. Legrand",
                    "slots": [
                        {"day": 1, "start": "10:30", "end": "13:00", "room": "Amphi Yoccoz"},
                        {"day": 3, "start": "10:30", "end": "13:00", "room": "0A2"},
                    ],
                },
            ],
        },
    ]


    cours_autres = [
    {
        "ue": "crypto",
        "name": "Cryptographie",
        "color": "#e6b3ff",
        "atoms": [
            {
                "label": "CM",
                "prof": "Stéphane Fischler",
                "day": 1,
                "start": "14:00",
                "end": "16:00",
                "room": "0A2",
            },
            {
                "label": "TP",
                "prof": "Chimène Fischler",
                "day": 4,
                "start": "10:30",
                "end": "12:30",
                "room": "0E10",
            },
        ],
    },
    {
        "ue": "geo_alea",
        "name": "Géométrie aléatoire",
        "color": "#ccb3ff",
        "atoms": [
            {
                "label": "CM",
                "prof": "N. Curien",
                "day": 1,
                "start": "16:15",
                "end": "18:15",
                "room": "1A7",
            },
            {
                "label": "TP",
                "prof": "A. Legrand",
                "day": 4,
                "start": "08:15",
                "end": "10:15",
                "room": "0D10",
            },
        ],
        },
    {
        "ue": "edp_num",
        "name": "EDP numériques",
        "color": "#ccebff",
        "atoms": [
            {
                "label": "CM",
                "prof": "B. Maury",
                "day": 1,
                "start": "16:15",
                "end": "18:15",
                "room": "0A5",
            },
            {
                "label": "TP",
                "prof": "B. Graille",
                "day": 4,
                "start": "08:15",
                "end": "10:15",
                "room": "0E10",
            },
        ],
    },
    {
        "ue": "topalg",
        "name": "Topologie algébrisée",
        "color": "#a366ff",
        "atoms": [
            {
                "label": "CI",
                "prof": "Patrick Massot",
                "day": 2,
                "start": "14:00",
                "end": "18:15",
                "room": "0A1",
            },
        ],
    },
    ]

    return render_template('index.html', cours_base=cours_base, cours_autres=cours_autres)


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

@app.route('/edt.pdf', methods=["POST"])
def generate_pdf():
    data = json.loads(request.data)
    pprint(data)
    
    sys_inputs = {"data": json.dumps(data)}

    pdf_file = BytesIO(compiler.compile(input="templates/edt.typ", sys_inputs=sys_inputs))

    return send_file(pdf_file, download_name="edt.pdf")

@app.route('/edt.svg', methods=["POST"])
def generate_svg():
    data = json.loads(request.data)
    pprint(data)
    
    sys_inputs = {"data": json.dumps(data)}

    pdf_file = BytesIO(compiler.compile(input="templates/edt.typ", sys_inputs=sys_inputs, format="svg"))

    return send_file(pdf_file, download_name="edt.svg")

