const fs = require("fs");
const YAML = require("yaml");
const child_process = require("child_process");
const puppeteer = require('puppeteer');
async function read_html(filename){
    return  fs.readFileSync(filename,"utf8");
}


 function parse_du_line(text) {
    res={}
    
    r=text.split("\t")
    if (r.length <2) return [text]; // valid line mus have a column size and folder
    foldername=r[1]
    res["size"]=r[0]
    res["folderpath"]=foldername
    res["folder"]=foldername.split("/")[foldername.split("/").length-1]
    //console.log([foldername.split("/")[foldername.split("/").length-1], foldername.split("/"),foldername.split("/").length])
    r1=foldername.replace(/(\d{3})[\._]?([A\d]{3})/i,"$1.$2")
    r1=r1.match(/(\d{3})\.([A\d]{3})/i)
    if (!r1) return res;
    res["lvanr"] = r1[0]
    return res
}

function du_unizeug(){
buf = child_process.execSync("du /mnt/save/daten/Unizeug/ -d 1 -h",{"maxBuffer": 1024*1024*48}).toString()
    tab=[];

    buf.split("\n").forEach(value => {
    text=parse_du_line(value)
    tab.push(text)
    })
    lookup=tab.reduce((acc,item,index)=>{
        if (item["lvanr"]) {
            if (acc[item["lvanr"]]) acc[item["lvanr"]].push(index);
            else acc[item["lvanr"]] =[index];
    }
    return acc;
    }, {});
    return {"folders": tab, "lookup": lookup}

}


async function fetch_page(url, selector) {
  const browser = await puppeteer.launch({args: ['--no-sandbox']});
  const page = await browser.newPage();
  await page.goto(url, {
    waitUntil: 'networkidle2'
});
    
    //console.log("Waiting for selector: "+selector)
  await page.waitForSelector(selector,{ timeout: 7000 }).catch((err)=>{console.log(err);  page.screenshot({path: 'exampleerr.png'}); await browser.close();})
  await page.screenshot({path: 'example.png'});
  let body = await page.evaluate((selector)=>{ 
        let b = $(selector)[0].innerHTML;
        return b
    },selector).catch((err)=>{console.log(err); await browser.close()});
  //console.log(await page.text()); 
  await browser.close();
  //console.log(body)
  return body
}


function merge_folders_courselist(courselist,folders) {
 Object.entries(courselist["tab2"]).forEach(([index0,value0])=> {
    value0["courses"].forEach((value)=> {          
    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
    },[])
    },value0)
})
return courselist;

}
module.exports = {read_html,du_unizeug, fetch_page,merge_folders_courselist}