//
// begining of xlsx to json coversion
//

var jsonContent = [];

/* set up XMLHttpRequest */
var url = "./content/CitationSheet.xlsx";
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("GET", url, true);
xmlHttpRequest.responseType = "arraybuffer";
xmlHttpRequest.onload = function (e) {
    var arraybuffer = xmlHttpRequest.response;

    // convert data to binary string //
    var data = new Uint8Array(arraybuffer);
    var arr = new Array();
    for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
    var bstr = arr.join("");

    // Call XLSX //
    var workbook = XLSX.read(bstr, {
        type: "binary"
    });

    // DO SOMETHING WITH workbook HERE //
    var first_sheet_name = workbook.SheetNames[0];
    // Get worksheet //
    var worksheet = workbook.Sheets[first_sheet_name];

    var jsonOutput = XLSX.utils.sheet_to_json(worksheet);

    for (i = 0; i < jsonOutput.length; i++) {
        var item = {
            "item": {
                "title": "",
                "project": "",
                "authors": [],
                "imageLink": "",
                "keywords": "",
                "journal": {
                    "name": "journal",
                    "volume": "",
                    "year": "",
                    "pages": ""
                },
                "DOI": ""
            },
            "links": {
                "pdf": "",
                "url": "",
                "demo": ""
            }
        }
        var authors = jsonOutput[i].Authors.replace("and ", "").trim().split(",");

        item["item"].title = jsonOutput[i].Titel;
        item["item"].project = jsonOutput[i].Project;
        for (j = 0; j < authors.length; j++) {
            var author = {
                "firstName": "",
                "lastName": ""
            }
            if (authors[j].trim().split(" ").length == 1) {
                author["firstName"] = authors[j].trim().split(".")[0];
                author["lastName"] = authors[j].trim().split(".")[1];
                item["item"].authors.push(author);
            } else if (authors[j].trim().split(" ").length == 2) {
                author["firstName"] = authors[j].trim().split(" ")[0];
                author["lastName"] = authors[j].trim().split(" ")[1];
                item["item"].authors.push(author);
            } else if (authors[j].trim().split(" ").length == 3) {
                author["firstName"] = authors[j].trim().split(" ")[0];
                author["lastName"] = authors[j].trim().split(" ")[2];
                item["item"].authors.push(author);
            }

        }
        item["item"].imageLink = jsonOutput[i]["Path to demo image"]
        if (jsonOutput[i].Keywords != undefined) {
            item["item"].keywords = jsonOutput[i].Keywords.split(",");
        } else {
            item["item"].keywords = jsonOutput[i].Keywords;
        }
        item["item"].journal.name = jsonOutput[i].Journal;
        item["item"].journal.volume = jsonOutput[i].Volume;
        item["item"].journal.year = jsonOutput[i].Year;
        item["item"].journal.pages = jsonOutput[i].Pages;
        item["item"].DOI = jsonOutput[i].DOI;
        item["links"].pdf = jsonOutput[i].URL;
        item["links"].url = jsonOutput[i].URL;
        item["links"].demo = jsonOutput[i].URL;

        jsonContent.push(item);

    }
    // console.log(jsonContent);

}
xmlHttpRequest.send();

//
// ending of xlsx to json coversion
//
