
var Ribbon = {
	params: {},

	setParams: function (params) {
		['url', 'username', 'password'].forEach(function (field) {
			if (typeof params !== 'object' || typeof params[field] === 'undefined' || params[field] === '') {
				throw 'Required param is not set: ' + field + '.';
			}
		});

		Ribbon.params = params;

		if (typeof Ribbon.params.url === 'string') {
			if (Ribbon.params.url.endsWith('/')) {
				Ribbon.params.url = Ribbon.params.url.slice(0,-1);
			}
		}

	},

	getRequest: function (url) {
		var response, request = new HttpRequest();

		if (typeof Ribbon.params.http_proxy !== 'undefined' && Ribbon.params.http_proxy !== '') {
			request.setProxy(Ribbon.params.http_proxy);
		};

		request.addHeader('Content-Type: application/yang-data+json');
		request.addHeader('Authorization: Basic ' + btoa(Ribbon.params.username + ':' + Ribbon.params.password));

		Zabbix.log(4, '[ Ribbon SBC SWe core ] Sending request: ' + url);

		response = request.get(Ribbon.params.url + url);

		Zabbix.log(4, '[ Ribbon SBC SWe core ] Received response with status code ' + request.getStatus() + ': ' + response);

		if (request.getStatus() !== 200 || response === null) {
			throw 'Request failed with status code ' + request.getStatus() + ': ' + response;
		}
		try {
			return JSON.parse(response);
		}
		catch (error) {
			throw 'Failed to parse response received from API. Check debug log for more information.';
		}
	},

	getField: function (data, path) {
		const steps = path.split('.');

		for (var i = 0; i < steps.length; i++) {
			var step = steps[i];
			if (typeof data !== 'object' || typeof data[step] === 'undefined') {
				throw 'Required field was not found: ' + path;
			}
			data = data[step];
		}

		return data;
	},

	filterByServerName: function (data, param_name, key_field) {
		var result = {};

		for (var key in data) {
			if (typeof data[key] === 'object' && data[key] !== null && typeof data[key][param_name] !== 'undefined') {
				if (data[key][param_name] === Ribbon.params.server_name) {
					result[data[key][key_field]] = data[key];
				};
			}
		};

		return result;
	},

	getMetricsData: function (endpoints) {
		var data = {};

		Object.keys(endpoints).forEach(function (key) {
			data[key] = Ribbon.getField(Ribbon.getRequest(endpoints[key]['url']), endpoints[key]['path']);
		});

		return data;
	}
};



try {
	Ribbon.setParams(JSON.parse(value));

	endpoints = {
		'serverAdmin': { 'url': '/restconf/data/sonusSystem:system/serverAdmin', 'path': 'sonusSystem:serverAdmin' },
		'admin': { 'url': '/restconf/data/sonusSystem:system/admin', 'path': 'sonusSystem:admin' },
		'syncStatus': { 'url': '/restconf/data/sonusSystem:system/syncStatus', 'path': 'sonusSystem:syncStatus' },
		'CongestionStatistics': {
			'url': '/restconf/data/sonusSystem:system/sonusOrcaSystem:systemCongestionCurrentStatistics',
			'path': 'sonusOrcaSystem:systemCongestionCurrentStatistics'
		},
		'CongestionStatus': { 'url': '/restconf/data/sonusSystem:system/sonusOrcaSystem:systemCongestionStatus', 'path': 'sonusOrcaSystem:systemCongestionStatus' },
		'callCountStatus': { 'url': '/restconf/data/sonusGlobal:global/sonusActiveCall:callCountStatus', 'path': 'sonusActiveCall:callCountStatus' },
		'callCountCurrentStatistics': { 'url': '/restconf/data/sonusGlobal:global/callCountCurrentStatistics', 'path': 'sonusGlobal:callCountCurrentStatistics' },
		'licenseInfo': { 'url': '/restconf/data/sonusSystem:system/licenseInfo', 'path': 'sonusSystem:licenseInfo' },
		'dnsGroup': { 'url': '/restconf/data/sonusAddressContext:addressContext=default/sonusDns:dnsGroup=', 'path': 'sonusDns:dnsGroup' },
		'globalTrunkGroupStatus': { 'url': '/restconf/data/sonusGlobal:global/sonusGlobalTrunkGroup:globalTrunkGroupStatus=', 'path': 'sonusGlobalTrunkGroup:globalTrunkGroupStatus' }
	};


	return JSON.stringify(Ribbon.getMetricsData(endpoints));

}
catch (error) {
	Zabbix.log(3, '[ Ribbon SBC SWe core ] ERROR: ' + error);
	error_msg = 'Failed to get data from API:' + error;

	return JSON.stringify({ 'error': error_msg });
}