const fs = require("fs");
const {read_html, du_unizeug, fetch_page,merge_folders_courselist} = require("./nodelib/lib");
const tissparse = require("./nodelib/tissparse");
const YAML = require("yaml");


async function load_curriculum(link, targetfile) {
    html = await fetch_page(link,'div.ui-datatable-tablewrapper');
    folders=du_unizeug()
    console.log(`writing to ${targetfile}.html`)
    fs.writeFile(targetfile+".html", html, function (err) {
        if (err) return console.log(err);
    });
    courselist=merge_folders_courselist(tissparse.courselist(html),folders)
    console.log(`writing to ${targetfile}`)
    fs.writeFile(targetfile, YAML.stringify(courselist["tab2"]), function (err) {
        if (err) return console.log(err);
    });

}

const file = fs.readFileSync('./curricula.yaml', 'utf8')
curricula=YAML.parse(file)
for (key in curricula ){
    console.log(`loading link: ${curricula[key]['link']}`);
    console.log(`Target file data/${key}.yaml`);
    if (curricula[key]["link"]) {
        load_curriculum(curricula[key]["link"],`data/${key}.yaml`)
    }
}

fetch_page('https://tiss.tuwien.ac.at/curriculum/public/curriculum.xhtml?dswid=3493&dsrid=603&key=58908','div.ui-datatable-tablewrapper').then((html)=> {
    folders=du_unizeug()
    courselist=tissparse.courselist(html)
    courselist.forEach((value,index)=> {
        i=folders["lookup"][value["lvanr"]] // lookup the LVAnr and get an Array with indexes 
        if (i) value["folders"] = i.reduce((acc,item)=>{
            acc.push(folders["folders"][item]) // get one of the folders that are related to LVAnr
            return acc
        },[])
    })

    //console.log(YAML.stringify(courselist))
});