let process = require('process');
let path = require('path');
let nodeXlsx = require('node-xlsx');
let jsonBeautifully = require('json-beautifully');
let fs = require('fs');

let cwd = process.cwd();
console.log(process.cwd());
let excelArray = [
    {
        fullPath: "excel/全局表.xls",
        name: "全局表.xls",
        sheet: "all",
    },
    {
        fullPath: "excel/关卡.xls",
        name: "关卡.xls",
        sheet: "smalllevel",
    },
    {
        fullPath: "excel/关卡.xls",
        name: "关卡.xls",
        sheet: "nest",
    },
    {
        fullPath: "excel/关卡.xls",
        name: "关卡.xls",
        sheet: "monster",
    },
    {
        fullPath: "excel/关卡.xls",
        name: "关卡.xls",
        sheet: "bullet",
    },
];

jsonSavePath = path.join(cwd, '../preview-templates/json');

excelArray = excelArray.map((v)=>{
    v.fullPath = path.join(cwd, v.fullPath);
    return v;
});

// console.log(excelArray);

// return;

function _getJsonSaveData(excelData, itemSheet) {
    let title = excelData[0];
    let desc = excelData[1];
    let ret = null;
    let useFormat1 = false;
    if (useFormat1) {
        let saveData1 = [];// 格式1:对应的为数组
        for (let i = 2; i < excelData.length; i++) {
            let lineData = excelData[i];
            // if (lineData.length < title.length) {
            //     continue;
            // } else if (lineData.length > title.length) {
            //     continue;
            // }
            if(!lineData) continue;
            let saveLineData = {};
            for (let j = 0; j < title.length; j++) {
                let key = title[j];
                let value = lineData[j];
                if (value === undefined) {
                    value = "";
                }
                // this._addLog("" + value);
                saveLineData[key] = value;
            }
            saveData1.push(saveLineData);
        }
        ret = saveData1;
    } else {
        let saveData2 = {};// 格式2:id作为索引
        for (let i = 2; i < excelData.length; i++) {
            let lineData = excelData[i];
            // if (lineData.length < title.length) {
            //     continue;
            // } else if (lineData.length > title.length) {
            //     continue;
            // }
            if(!lineData || lineData.length == 0) continue;
            let saveLineData = {};
            for (let j = 1; j < title.length; j++) {
                let key = title[j];
                let value = lineData[j];
                if (value === undefined) {
                    value = "";
                }
                // this._addLog("" + value);
                saveLineData[key] = value;
            }
            saveData2[lineData[0].toString()] = saveLineData;
        }
        ret = saveData2;
    }
    return ret;
}

function _addLog(str){
    console.log(str);
}

let jsonAllSaveData = {};// 保存的json数据
for (let k in excelArray) {
    let itemSheet = excelArray[k];

    let excelData = nodeXlsx.parse(itemSheet.fullPath);
    let sheetData = null;
    for (let j in excelData) {
        if (excelData[j].name === itemSheet.sheet) {
            sheetData = excelData[j].data;
        }
    }
    if (sheetData) {
        if (sheetData.length > 2) {
            // 保存为json
            let jsonSaveData = _getJsonSaveData(sheetData, itemSheet);
            {
                let saveStr = JSON.stringify(jsonSaveData);

                saveStr = jsonBeautifully(saveStr);
                
                let saveFileFullPath = path.join(jsonSavePath, itemSheet.sheet + ".json");
                fs.writeFileSync(saveFileFullPath, saveStr);
                _addLog("[Json]:" + saveFileFullPath);
            }
        } else {
            _addLog("行数低于2行,无效sheet:" + itemSheet.sheet);
        }
    } else {
        _addLog("未发现数据");
    }
}