'use strict';

const util = require('./common-util.js');
const db = require('../db-module.js');
const trayModule = require('../tray-module.js');

module.exports = {
	getAllSettings: response => {
		db.settingDatastore.find({_id: {$nin:
			['3', '4', '5', '6']
			}}, (err, docs) => {
			let ret = '{"aaData":[';
			docs.forEach(setting => {
				ret += '["' + setting._id + '","' +
				setting.key + '","' +
				setting.value + '","' +
				util.editButton('Setting', setting._id, setting.type) + '"],';
			});
			response.send(util.correggiRispostaPerDataTable(ret));
		});
	},

	editSettingBooleano: (event, id) => {
		db.settingDatastore.findOne({_id: id}, (err, setting) => {
			event.sender.send('edit-setting-booleano-reply', setting);
		});
	},

	saveSettingBooleano: (event, id, value) => {
		db.settingDatastore.update({_id: id},
			{$set: {value: value}},
			{returnUpdatedDocs: false, multi: false}, (err, numAffected, affectedDocuments) => {
				event.sender.send('save-setting-booleano-reply');
				if (id === '9' || id === '10') {
					trayModule.applySettings();
				}
			}
		);
	}
}

@@ -1085,13 +1085,191 @@
 %09%7D%0A%09%09);%0A
+%09%7D,%0A%0A%09readSettingByKey: (key) =%3E %7B%0A%09%09return new Promise(function(resolve, reject) %7B%0A%09%09%09db.settingDatastore.findOne(%7Bkey: key%7D, (err, value) =%3E %7B%0A%09%09%09%09resolve(value);%0A%09%09%09%7D);%0A%09%09%7D);%0A
 %09%7D%0A%7D%0A

add readSettingByKey with promise