const axios = require('axios');
const pronote = require('pronote-api');
const config = require('../includes/app.json');

const url = config.pronote.url;
const username = config.pronote.user;
const password = config.pronote.password;

async function getTimetable(offset) {
    const timetable = lessons;

    let nextClass = timetable.filter(i => new Date(i.from.toDateString()).getTime() === new Date(new Date(new Date().toDateString()).getTime() + 86400000 * offset).getTime()).filter(i => !i.isCancelled)[0];

    return nextClass ? nextClass["from"] : null;
}

global.wakeUpTime = null;
global.wakeUpTimeDiff = null;
global.sleepTime = null;
global.sleepTimeDiff = null;
global.sleepClockR = null;
global.sleepClockC = null;
global.wakeUpClockC = null;
global.wakeUpClockR = null;
global.firstClassTime = null;
global.firstClassClockC = null;
global.firstClassClockR = null;

async function nightWarning() {
    if (global.sleepTimeDiff) {
        await notification("🌙 Good evening!", "You are going to sleep " + global.sleepTimeDiff + ", " + global.sleepClockC + " BST (" + global.sleepClockR + " CEST) so you can wake up " + global.wakeUpTimeDiff + ", " + global.wakeUpClockC + " BST (" + global.wakeUpClockR + " CEST). You will be reminded when it's time to go to sleep.", 3);
    } else {
        await notification("🌙 Good evening!", "You don't need to wake up at a specific time tomorrow, enjoy!", 3);
    }
}

async function nightReminder() {
    if (global.sleepTimeDiff) {
        await notification("🌙 Now let's sleep!", "You should get ready to sleep now so you can wake up " + global.wakeUpTimeDiff + ", " + global.wakeUpClockC + " BST (" + global.wakeUpClockR + " CEST).", 3);
    }
}

function digits(input) {
    if (input < 10) {
        return "0" + input;
    } else {
        return input;
    }
}

global.tzOffset = Math.abs(new Date().getTimezoneOffset() / 60);

function formatHours(hours) {
    return hours >= 24 ? hours - 24 : hours;
}

async function updateSleepTime(offset) {
    let classTime = await getTimetable(offset);
    let classTime2 = await getTimetable(offset + 1);
    let tzOffset = global.tzOffset;

    if (classTime) {
        global.wakeUpTime = new Date(classTime.getTime() - 5100000);

        if (wakeUpTime.getHours() > 8 || (wakeUpTime.getHours() === 8 && wakeUpTime.getMinutes() > 0)) {
            global.wakeUpTime = new Date(wakeUpTime.toISOString().split("T")[0] + "T08:00:00+0" + (tzOffset) + ":00");
        }

        if (classTime.toTimeString().split(" ")[0] === "08:00:00") {
            global.wakeUpTime = new Date(wakeUpTime.toISOString().split("T")[0] + "T06:30:00+0" + (tzOffset) + ":00");
        }

        if (classTime.toTimeString().split(" ")[0] === "09:00:00") {
            global.wakeUpTime = new Date(wakeUpTime.toISOString().split("T")[0] + "T07:00:00+0" + (tzOffset) + ":00");
        }

        if (classTime.toTimeString().split(" ")[0] === "10:00:00") {
            global.wakeUpTime = new Date(wakeUpTime.toISOString().split("T")[0] + "T07:00:00+0" + (tzOffset) + ":00");
        }
        global.wakeUpClockR = formatHours(wakeUpTime.getUTCHours() + tzOffset) + ":" + digits(wakeUpTime.getUTCMinutes());
        global.wakeUpClockC = formatHours(wakeUpTime.getUTCHours() + (tzOffset - 1)) + ":" + digits(wakeUpTime.getUTCMinutes());

        global.firstClassTime = classTime;
        global.firstClassClockC = formatHours(firstClassTime.getUTCHours() + (tzOffset - 1)) + ":" + digits(firstClassTime.getUTCMinutes());
        global.firstClassClockR = formatHours(firstClassTime.getUTCHours() + tzOffset) + ":" + digits(firstClassTime.getUTCMinutes());
    } else {
        global.wakeUpTime = null;
        global.wakeUpClockR = null;
        global.wakeUpClockC = null;

        global.firstClassTime = null;
        global.firstClassClockC = null;
        global.firstClassClockR = null;
    }

    if (classTime2) {
        global.wakeUpTime2 = new Date(classTime2.getTime() - 5100000);

        if (wakeUpTime2.getHours() > 8 || (wakeUpTime2.getHours() === 8 && wakeUpTime2.getMinutes() > 0)) {
            global.wakeUpTime2 = new Date(wakeUpTime2.toISOString().split("T")[0] + "T08:00:00+0" + (tzOffset) + ":00");
        }

        if (classTime2.toTimeString().split(" ")[0] === "08:00:00") {
            global.wakeUpTime2 = new Date(wakeUpTime2.toISOString().split("T")[0] + "T06:30:00+0" + (tzOffset) + ":00");
        }

        if (classTime2.toTimeString().split(" ")[0] === "09:00:00") {
            global.wakeUpTime2 = new Date(wakeUpTime2.toISOString().split("T")[0] + "T07:00:00+0" + (tzOffset) + ":00");
        }

        if (classTime2.toTimeString().split(" ")[0] === "10:00:00") {
            global.wakeUpTime2 = new Date(wakeUpTime2.toISOString().split("T")[0] + "T07:00:00+0" + (tzOffset) + ":00");
        }

        global.sleepTime = new Date(global.wakeUpTime2.getTime() - 28800000);

        global.sleepClockR = formatHours(sleepTime.getUTCHours() + tzOffset) + ":" + digits(sleepTime.getUTCMinutes());
        global.sleepClockC = formatHours(sleepTime.getUTCHours() + (tzOffset - 1)) + ":" + digits(sleepTime.getUTCMinutes());
    } else {
        global.sleepTime = null;

        global.sleepClockR = null;
        global.sleepClockC = null;
    }
}

async function display(offset) {
    await updateSleepTime(offset);
    console.log(new Date(new Date().getTime() + 86400000 * offset).toDateString())
    console.log("    First:   " + firstClassTime + " (" + firstClassClockC + ", " + firstClassClockR + ")");
    console.log("    Wake up: " + wakeUpTime + " (" + wakeUpClockC + ", " + wakeUpClockR + ")");
    console.log("    Sleep:   " + sleepTime + " (" + sleepClockC + ", " + sleepClockR + ")");
}

(async () => {
    global.session = await pronote.login(url, username, password);
    global.lessons = await session.timetable(new Date(new Date() - (86400000 * 3)), new Date(new Date().getTime() + (86400000 * 7)));

    await updateSleepTime(0);
    await display(-2);
    console.log("-------------------");
    await display(-1);
    console.log("-------------------");
    await display(0);
    console.log("-------------------");
    await display(1);
    console.log("-------------------");
    await display(2);
    console.log("-------------------");
    await display(3);
    console.log("-------------------");
    await display(4);
    console.log("-------------------");
    await display(5);
    console.log("-------------------");
    await display(6);
    console.log("-------------------");
    await display(7);

    let json = {};

    for (let i = -2; i < 8; i++) {
        let date = new Date(new Date().getTime() + 86400000 * i).toISOString().split("T")[0];

        json[date] = {
            firstClass: {
                timestamp: null,
                cloudburst: null,
                raindrops: null
            },
            wakeUp: {
                timestamp: null,
                cloudburst: null,
                raindrops: null
            },
            sleep: {
                timestamp: null,
                cloudburst: null,
                raindrops: null
            }
        };

        let time = await updateSleepTime(i);

        json[date].firstClass.timestamp = firstClassTime ? firstClassTime.getTime() : null;
        json[date].firstClass.cloudburst = firstClassClockC ?? null;
        json[date].firstClass.raindrops = firstClassClockR ?? null;

        json[date].sleep.timestamp = sleepTime ? sleepTime.getTime() : null;
        json[date].sleep.cloudburst = sleepClockC ?? null;
        json[date].sleep.raindrops = sleepClockR ?? null;

        json[date].wakeUp.timestamp = wakeUpTime ? wakeUpTime.getTime() : null;
        json[date].wakeUp.cloudburst = wakeUpClockC ?? null;
        json[date].wakeUp.raindrops = wakeUpClockR ?? null;
    }

    console.log(json);
    require('fs').writeFileSync("../includes/data/school.json", JSON.stringify(json));
})()