exports.id = 'csv_import';
exports.title = 'CsvImport';
exports.version = '1.0.0';
exports.group = 'Worksys';
exports.color = '#2134B0';
exports.input = 1;
exports.output = ["red", "white"];
exports.click = false;
exports.author = 'Daniel Segeš';
exports.icon = 'file-import';
exports.options = { edge: "undefined" };

exports.html = `<div class="padding">
	<div class="row">
		<div class="col-md-6">
			<div data-jc="textbox" data-jc-path="edge" data-jc-config="placeholder:undefined;required:true" class="m">CSV Import</div>
		</div>
	</div>
</div>`;

exports.readme = `# load csv to table db`;

//config
let delimiter = ";";
let uniqueColumn = "node";
let path = "flow/audit_test_panel.csv";
let startFrom = 1;
let table = "nodes";
let mapImport = {
	2: "node",
	4: "tbname",
	3: "line"
};

//10.0.0.62
delimiter = ";";
uniqueColumn = "node";
path = "flow/audit_rvo14_lampy.csv";
startFrom = 1;
table = "nodes";
mapImport = {
	1: "node",
	3: "tbname",
	2: "line"
};

//notification
delimiter = ";";
uniqueColumn = undefined;
path = "flow/notifikacie.csv";
startFrom = 1;
table = "notifications";
mapImport = {
	0: "key",
	1: "weight",
	2: "en",
	3: "sk"
};

const fs = require('fs');

exports.install = function(instance) {

  //console.log("csv import installed");

  instance.on("close", () => {

  })

  
  instance.on("data", (flowdata) => {

	instance.send(0, "start import");
	console.log("csv import", flowdata.data);

	//{table: "nodes", startFrom: 1, delimiter: ";", uniqueColumn: "node", path: "flow/audit_rvo14_lampy.csv", mapImport: {1: "node",	3: "tbname", 2: "line"}}


	if(typeof flowdata.data === 'object')
	{
		console.log("*******************", flowdata.data);

		if(!flowdata.data.hasOwnProperty("table"))
		{
			instance.send(0, "!!!!csv import - nedefinovana tabulka");
			return;
		}

		if(!flowdata.data.hasOwnProperty("uniqueColumn"))
		{
			//instance.send(0, "!!!!csv import - nedefinovane uniqueColumn");
			//return;
		}

		if(!flowdata.data.hasOwnProperty("path"))
		{
			instance.send(0, "!!!!csv import - nedefinovana cesta k suboru");
			return;
		}

		if(!flowdata.data.hasOwnProperty("mapImport"))
		{
			instance.send(0, "!!!!csv import - nedefinovany mapImport");
			return;
		}

		table = flowdata.data.table;
		uniqueColumn = flowdata.data.uniqueColumn;
		if(uniqueColumn === "") uniqueColumn = undefined;

		path = flowdata.data.path;
		mapImport = flowdata.data.mapImport;

		if(flowdata.data.hasOwnProperty("delimiter")) delimiter = flowdata.data.delimiter;
		if(flowdata.data.hasOwnProperty("startFrom")) startFrom = flowdata.data.startFrom;
	}


	var db = TABLE(table);
	db.clear();
	
	let keys = Object.keys(mapImport);

	try {
		const data = fs.readFileSync(path, 'utf8')

		let lines = data.split("\n");

		for(let i = startFrom; i < lines.length; i++)
		{
			let line = lines[i];
			if(line === "") continue;

			let data = line.split(delimiter);
			if(data.length == 0) continue;

			let insertData = {};

			keys.map(function(key){
				let k = mapImport[key];

				//console.log("importineg", i, key, k);

				if(data[key] != undefined) insertData[k] = data[key].trim();
				else{
					console.log("undefined", key, data);
				}
			});
			
			console.log("insertData", insertData);

			if(uniqueColumn != undefined)
			{
				db.insert(insertData, true).where(uniqueColumn, insertData[uniqueColumn]);
			}
			else
			{
				db.insert(insertData);
			}

			
		}

		console.log("csv import finished");
		instance.send(0, "csv import finished");

	  } catch (err) {
		console.error(err)
		instance.send(0, err);
	  }
  })

}


