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

function getHttpData(url) {
	request.addHeader('Accept: application/json');
	request.addHeader('Authorization: Bearer ' + params.token);
	response = request.get(url);
	Zabbix.log(4, '[ FortiGate ] [ ' + url + ' ] Received response with status code ' + request.getStatus() + ': ' + response);

	if (request.getStatus() !== 200) {
		throw 'Failed to receive data: invalid response status code. Check debug log for more information.';
	}

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

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

	if ((typeof response.status !== 'undefined' && response.status != 'success') || typeof response.results === 'undefined') {
		var uri = '/' + response.path + '/' + response.name + ((typeof response.action !== 'undefined') ? '?action=' + response.action : "");
		throw 'Failed to parse response received from ' + uri + '. Check debug log for more information.';
	}

	return response;
};

try {
	['scheme', 'api.token', 'api.fqdn', 'api.port'].forEach(function (field) {
		var macro = '{$FGATE.' + field.toUpperCase() + '}';
		var key = field.split('.').pop();
		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 (typeof params.http_proxy !== 'undefined' && params.http_proxy !== '{' + '$FGATE.HTTP.PROXY}' && params.http_proxy !== '') {
		request.setProxy(params.http_proxy);
	}

	api_url = params.scheme + '://' + params.fqdn + ':' + params.port;

	var netif_data = getHttpData(
		api_url + '/api/v2/monitor/system/interface'
	);

	var netif_list = getHttpData(
		api_url + '/api/v2/cmdb/system/interface'
	);

	result.data = netif_list.results.map(function (v) {
		var item = {};
		['q_origin_key', 'name', 'mode', 'type', 'description'].forEach(function (k) {
			item[k] = v[k];
		});
		if (typeof item.q_origin_key !== 'undefined' && typeof netif_data.results[String(item.q_origin_key)] !== 'undefined') {
			Object.assign(item, netif_data.results[String(item.q_origin_key)]);
		}
		if (typeof item.alias !== 'undefined') {
			item.alias = item.alias.slice(1,-1);
		}
		else {
			item.alias = '';
		}
		if (typeof item.link === 'undefined') {
			item.link = false;
		}
		if (typeof item.id === 'undefined') {
			item.id = item.q_origin_key;
		}
		return item;
	});
} catch (error) {
	error_msg = error.toString();
	error_msg += error_msg.endsWith('.') ? '' : '.';
	Zabbix.log(3, '[ FortiGate ] ERROR: ' + error_msg);
};

result.error = error_msg;

return JSON.stringify(result);