var params = JSON.parse(value),
	request = new HttpRequest(),
	result = { "data": [], "error": "" },
	error_msg = "";

function getHttpData(url, token, cookie) {
	request.addHeader('Cookie: ' + cookie);
	request.addHeader('X-XSRF-TOKEN: ' + token);
	response = request.get(url);
	Zabbix.log(4, '[ Cisco SD-WAN ] [ ' + url + ' ] Received response with status code ' + request.getStatus() + ': ' + response);

	if (request.getStatus() !== 200) {
		throw 'Failed to receive data: invalid response status code.';
	}

	if (response !== null) {
		try {
			response = JSON.parse(response);
		}
		catch (error) {
			throw 'Failed to parse response received from Cisco SD-WAN API. Check debug log for more information.';
		}
	}

	if (typeof (response) !== 'object' || response === null) {
		throw 'Cannot process response data: received data is not an object.';
	}

	return response;
};

try {
	['auth.token', 'auth.cookie', 'api.url', 'device_id'].forEach(function (field) {
		var macro = '{$SDWAN.' + field.toUpperCase() + '}';
		var key = field.split('.').slice(-1);
		if (params[key] === macro || params[key] === '') {
			throw 'Please change ' + macro + ' macro to the proper value.';
		}
		if (typeof params[key] === 'undefined') {
			throw 'Please set \'' + key + '\' script input parameter to ' + macro + ' macro value.';
		}
	});

	if (params.url.indexOf('http://') === -1 && params.url.indexOf('https://') === -1) {
		params.url = 'https://' + params.url;
	}

	if (!params.url.endsWith('/')) {
		params.url += '/';
	}

	if (typeof params.http_proxy !== 'undefined' && params.http_proxy !== '{' + '$SDWAN.HTTP_PROXY}' && params.http_proxy !== '') {
		request.setProxy(params.http_proxy);
	}

	var interface_list = getHttpData(
		params.url + 'dataservice/device/interface?deviceId=' + encodeURIComponent(params.device_id),
		params.token,
		params.cookie
	);

	if (Array.isArray(interface_list.data) && interface_list.data.length > 0) {
		var interfaces = {};
		interface_list.data.forEach(function (if_item) {
			var if_key = if_item['vdevice-name'] + '_' + if_item.ifname + '_' + if_item.ifindex;
			interfaces[if_key] = (typeof interfaces[if_key] !== 'undefined') ? interfaces[if_key] : {};
			if (typeof if_item['af-type'] !== 'undefined') {
				if (if_item['af-type'].includes('ipv6')) {
					Object.keys(if_item).sort().forEach(function (key) {
						if (key.match(/([tr]x-)/g)) {
							interfaces[if_key]['ipv6-' + key] = if_item[key];
						}
					});
				}
				else {
					interfaces[if_key] = if_item;
				}
			}
			else {
				interfaces[if_key] = if_item;
			}
		});
		Object.keys(interfaces).forEach(function (key) {
			result.data.push(interfaces[key]);
		});
	}
	else {
		throw 'Failed to parse response received from Cisco SD-WAN API. Check debug log for more information.';
	}
} catch (error) {
	error_msg = error.toString();
	error_msg += (error_msg.endsWith('.')) ? '' : '.';
	Zabbix.log(3, '[ Cisco SD-WAN ] ERROR: ' + error_msg);
};

result.error = error_msg;

return JSON.stringify(result);