const debug = require("debug")("edupage:log");
const error = require("debug")("edupage:error");
const RawData = require("../lib/RawData");
const Edupage = require("./Edupage");
const {FatalError, ParseError} = require("./exceptions");
const Lesson = require("./Lesson");
debug.log = console.log.bind(console);
class Timetable extends RawData {
constructor(data = {}, date = null, edupage = null) {
super(data);
this.edupage = edupage;
this.date = new Date(date);
this.lessons = [];
this.week = data.tt_week;
if(this.edupage) Timetable.prototype.init.call(this);
}
init(edupage = null) {
if(edupage) this.edupage = edupage;
this.lessons = this._data.plan.filter(e => e.type == "lesson").map(data => new Lesson(data, this.edupage));
}
static parse(html) {
const match = html.match(/classbook\.fill\([\s\S]*?,\s?([\s\S]+?)(?:,\s?\[[\s\S]*?\])?\);/mi);
if(!match || !match[1]) return FatalError.throw(new ParseError("Failed to parse timetable data from html"), {html, match});
try {
return JSON.parse(match[1]);
} catch(e) {
return FatalError.throw(new ParseError("Failed to parse JSON from timetable html"), {html, match});
}
}
}
module.exports = Timetable;