const puppeteer = require('puppeteer');
const cheerio = require('cheerio');
const fs = require("fs");
const {read_html, du_unizeug} = require("./lib");
const tissparse = require("./tissparse");
const YAML = require("yaml");


async function fetch_courselist() {
  const browser = await puppeteer.launch({args: ['--no-sandbox']});
  const page = await browser.newPage();
  await page.goto('https://tiss.tuwien.ac.at/curriculum/public/curriculum.xhtml?dswid=3493&dsrid=603&key=58908');
  await page.waitForSelector('div.ui-datatable-tablewrapper')
  await page.screenshot({path: 'example.png'});
  let body = await page.evaluate(()=>{ 
        let b = $('div.ui-datatable-tablewrapper')[0].innerHTML
        return b 
    });
  //console.log(await page.text()); 
  await browser.close();
  return body
}

async function write_html(html) {
    fs.writeFile("temp.html",html, (err)=>{
        if(err) console.log(err);
        console.log("successfully written temp.html")
    })
}
//fetch_courselist().then(write_html)

read_html("temp.html").then((html)=> {
    folders=du_unizeug()
    courselist=tissparse.courselist(html)
    courselist.forEach((value,index)=> {
        i=folders["lookup"][value["lvanr"]]
        if (i) value["folders"] = i.reduce((acc,item)=>{
            acc.push(folders["folders"][item])
            return acc
        },[])
    })
    
    console.log(YAML.stringify(courselist))
});
    
//child_process.exec("find /mnt/save/daten/Unizeug/ -type d -maxdepth 1", (err,buf)=>{
//    console.log(buf.split("\n"))
//});


const parse_courselist = ((html) => { return html;})