exports.id = 'slack_filter';
exports.title = 'Slack Filter';
exports.group = 'Citysys';
exports.color = '#30E193';
exports.input = 2;
exports.output = 1;
exports.author = 'Jakub Klena';
exports.icon = 'plug';
exports.version = '1.0.8';
exports.options = { 'name': '', 'types': '["emergency", "critical", "error", "alert"]', 'message_includes': '["is responding again"]', 'tag_on_include': '[{"user_id":"U072JE5JUQG", "includes":["Electrometer", "Twilight sensor"]}]', 'slack_channel': '' };

exports.html = `<div class="padding">
	<div class="row">
		<div class="col-md-12">
			<div data-jc="textbox" data-jc-path="slack_channel" data-jc-config="required:false">@(Slack channel to receive the alerts)</div>
		</div>
		<div class="col-md-12">
			<div data-jc="textbox" data-jc-path="types" data-jc-config="required:false">@(Watch these types, comma separated names)</div>
		</div>
		<div class="col-md-12">
			<div data-jc="textbox" data-jc-path="message_includes" data-jc-config="required:false">@(Watch messages that include any of the following strings)</div>
		</div>
		<div class="col-md-12">
			<div data-jc="textbox" data-jc-path="tag_on_include" data-jc-config="required:false">@(Tag people if message includes something)</div>
		</div>
	</div>
</div>`;

exports.readme = `# Slack Filter`;

exports.install = function(instance) {
	var running = false;
	instance["savedSlackMessages"] = [];
	var timer = null;

	instance.on('0', function(response) {
		if (!running) return;
		let value = response.data;
		if (typeof value !== 'object') return;

		let can = false
		var k = Object.keys(value);
		var interested = JSON.parse(instance.options.types);
		var msg_incl = JSON.parse(instance.options.message_includes);
		var tags = JSON.parse(instance.options.tag_on_include);

		if (k.length <= 0) return;
		if (value[k[0]].length <= 0) return;
		if (!Object.prototype.hasOwnProperty.call(value[k[0]][0], 'values')) return;
		if (!Object.prototype.hasOwnProperty.call(value[k[0]][0]['values'], '_event')) return;
		if (!Object.prototype.hasOwnProperty.call(value[k[0]][0]['values']['_event'], 'type')) return;
		if (!Object.prototype.hasOwnProperty.call(value[k[0]][0]['values']['_event'], 'source')) return;
		if (!Object.prototype.hasOwnProperty.call(value[k[0]][0]['values']['_event']['source'], 'func')) return;
		if (!Object.prototype.hasOwnProperty.call(value[k[0]][0]['values']['_event'], 'message')) return;
		if (!Object.prototype.hasOwnProperty.call(value[k[0]][0]['values']['_event'], 'message_data')) return;

		let icon = ':totaljs:';
		let type = value[k[0]][0]['values']['_event']['type'];
		let source = value[k[0]][0]['values']['_event']['source']['func'];
		let message = value[k[0]][0]['values']['_event']['message']['en'];
		let message_data = value[k[0]][0]['values']['_event']['message_data'];
		let tag = '';

		switch (type) {
			case 'debug':
				icon = ':beetle:';
				break;
			case 'info':
				icon = ':speech_balloon:';
				break;
			case 'notice':
				icon = ':speech_balloon:';
				break;
			case 'warning':
				icon = ':exclamation:';
				break;
			case 'alert':
				icon = ':warning:';
				break;
			case 'error':
				icon = ':no_entry:';
				break;
			case 'emergency':
				icon = ':fire:';
				break;
			case 'critical':
				icon = ':fire:';
				break;
		}

		// Check if this message includes one of the strings we are watching for
		for (const msg of msg_incl) {
			if (message.includes(msg)) {
				if (msg == 'is responding again') icon = ':large_green_circle:';
				can = true;
				break;
			}
		}
		// Check if message is one of the types we are watching for
		if (interested.includes(type)) {
			can = true;
		}

		if (!can) return;


		// Check for each person tags based on what the message includes
		for (const person of tags) {
			for (const msg of person.includes) {
				if (message.includes(msg)) {
					tag += '<@' + person.user_id + '> ';
					break; // Break out from this person checks as they are already tagged now
				}
			}
		}
		// Now that all people are tagged add new line symbol
		if (tag != '') tag += '\n';

		let send_data = tag + instance.options.name + ' ' + type.toUpperCase() + '\n*Source*: ' + source + '\n*Message*: ' + message;
		if (message_data) {
			send_data += '\nData: ' + message_data;
		}

		let ignore_msg = false
		if (message.includes('Configuration of dimming profile to node no')) {
			for (let i = 0; i < FLOW["savedSlackMessages"].length; i++) {
				if (FLOW["savedSlackMessages"][i].message == message) {
					ignore_msg = true;
					break;
				}
			}
			if (!ignore_msg) {
				FLOW["savedSlackMessages"].push({ message, 'dateandtime': Date.now() });
				if (timer === null) {
					timer = setTimeout(checkSavedMessages, 60 * 60000);
				}
			}
		}

		if (!ignore_msg) {
			instance.send2({ 'msg': send_data, 'bot_name': instance.options.name + ' ' + type.toUpperCase(), 'bot_icon': icon, 'channel': instance.options.slack_channel });
		}
	});

	function checkSavedMessages() {
		var d = Date.now();
		d = d - 86400000; // older then 24hr
		var a = [];
		//Remove msgs older then 24hr
		for (let i = 0; i < FLOW["savedSlackMessages"].length; i++) {
			if (FLOW["savedSlackMessages"][i].dateandtime > d) {
				a.push(FLOW["savedSlackMessages"][i]);
			}
		}
		FLOW["savedSlackMessages"] = a;

		if (FLOW["savedSlackMessages"].length > 0) {
			timer = setTimeout(checkSavedMessages, 60 * 60000);
		} else {
			timer = null;
		}
	}

	instance.reconfigure = function() {
		try {
			if (!FLOW["savedSlackMessages"]) {
				FLOW["savedSlackMessages"] = [];
			}

			instance.options.name = FLOW.GLOBALS.settings.rvo_name;
			if (instance.options.name) {
				instance.status('Running');
				running = true;
			} else {
				instance.status('Please run options again', 'red');
				running = false;
			}
		} catch (e) {
			instance.error('Citysys connector: ' + e.message);
		}
	};

	instance.on('options', instance.reconfigure);

	instance.on("1", _ => {
		instance.reconfigure();
	})
};
