function tohours(mns) { // convert a value in minutes to a 24-hour timestamp, e.g. 330 -> 0530
    let hour = Math.floor(mns / 60);
    let minute = mns % 60;
    return hour * 100 + minute;
}

function tomins(hrs) { // convert a value in 24-hour to minutes, the inverse of tohours
    let hour = Math.floor(hrs / 100);
    let minute = hrs % 100;
    return hour * 60 + minute;
}

const daystart = tomins(1000);

function zpad(thing, zeroes) { // left-pad a string with zeroes
    thing = "" + thing;
    while (thing.length < zeroes) {
        thing = "0" + thing;
    }
    return thing;
}

function tonicehrs(hrs) { // convert a value in 24-hour to a string (800 -> "08:00", 1645 -> "16:45")
    let hour = Math.floor(hrs / 100);
    if (hour > 12) {
        hour -= 12;
    }
    let minute = hrs % 100;
    return zpad(hour, 2) + ":" + zpad(minute, 2);
}

function tablesetup() {
    for (var i = 0; i < 11 * 4; i ++) {
        let row = document.createElement("tr");
        let time = tonicehrs(tohours(daystart + i * 15))
        row.innerHTML = `<td>${time}<span>${time}</span></td><td></td><td></td><td></td><td></td><td></td>`;
        document.querySelector("#schedule > tbody").appendChild(row);
    }
}


function setClassTo(el, c, map) {
    el.querySelector(".cname").innerText = c.name;
    el.querySelector(".ctype").innerText = c.type;
    el.querySelector(".bloc").href = map[c.building];
    el.querySelector(".building").innerText = c.building;
    el.querySelector(".room").innerText = c.room;
    el.querySelector(".start").innerText = tonicehrs(c.start_time);
    el.querySelector(".end").innerText = tonicehrs(c.end_time);
}


window.addEventListener("load", async () => {
    let data = await (await fetch("data.json")).json();
    document.getElementById("year").innerHTML = data.year;
    document.getElementById("semester").innerHTML = data.semester;
    document.getElementById("credits").innerHTML = data.credits;
    tablesetup();
    let tbody = document.querySelector("#schedule > tbody")
    let curday = new Date().getDay();
    let firstClass = undefined;
    let nextClass = undefined;
    let lastClass = undefined;
    let now = new Date();
    let curTime = now.getMinutes() + now.getHours() * 100;
    let curTimeSnap = Math.round((tomins(curTime) - daystart) / 15);
    tbody.children[curTimeSnap].children[curday].classList.add("youarehere");
    for (let cls of data.week) {
        for (let day of cls.days) {
            if (day == curday) {
                if (firstClass == undefined || cls.start_time < firstClass.start_time) {
                    firstClass = cls;
                }
                if (lastClass == undefined || cls.end_time > lastClass.end_time) {
                    lastClass = cls;
                }
                if (cls.start_time > curTime) {
                    if (nextClass == undefined || cls.start_time < nextClass.start_time) {
                        nextClass = cls;
                    }
                }
            }
            let start = Math.floor((tomins(cls.start_time) - daystart) / 15);
            let el = tbody.children[start].children[day];
            el.classList.add("class");
            let rows = Math.floor((tomins(cls.end_time) - tomins(cls.start_time)) / 15);
            el.rowSpan = rows;
            for (var i = start + 1; i < start + rows; i ++) { // cull the tds that this td is overlapping
                tbody.children[i].removeChild(tbody.children[i].children[day]);
            }
            el.classList.add(cls.type);
            el.innerHTML = `<span class="coursename">${cls.name}</span><span class="coursetype">${cls.type}</span> at <a href="${data.maps[cls.building]}" about="blank">${cls.building}</a> ${cls.room}`
        }
    }
    if (firstClass) {
        setClassTo(document.getElementById("firstclass"), firstClass, data.maps);
    }
    if (nextClass) {
        setClassTo(document.getElementById("nextclass"), nextClass, data.maps);
    }
    if (lastClass) {
        setClassTo(document.getElementById("lastclass"), lastClass, data.maps);
    }
});