
const request_path = '/api/integration_management/v2/status';
const app_id = '5619386d-de56-49ec-8508-f5c399d3b9bd';

function checkParams(params) {
	['api_url', 'api_token', 'tenant_uuid'].forEach(function (field) {
		if (typeof params !== 'object' || typeof params[field] === 'undefined' || params[field] === '') {
			throw 'Required param is not set: ' + field + '.';
		}
	});
	return params;
}

function getHttpData(url, register, params, api_token, tenant_uuid) {
	const request = new HttpRequest();

	request.addHeader('Content-Type: application/json');
	request.addHeader('Authorization: Bearer ' + api_token);
	request.addHeader('X-Application-Secret: ' + app_id);

	if (params['http_proxy']) {
		request.setProxy(params['http_proxy']);
		Zabbix.log(4, '[ ACRONIS CYBER PROTECT CLOUD API ] Using http proxy: ' + params['http_proxy']);
	}

	if (!register) {
		const response = request.get(url + '?tenantID=' + encodeURIComponent(tenant_uuid));
		Zabbix.log(4, '[ ACRONIS CYBER PROTECT CLOUD API ] [ ' + url + ' ] Received response with status code ' + request.getStatus() + ': ' + response);

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

		return response;

	} else {

		const request_body = {
			"application_id": app_id,
			"module": {
				"name": "Zabbix integration with Acronis Cyber Protect Cloud",
				"version": "1.0.0"
			},
			"vendor_system": {
				"name": "Product integrated with Acronis",
				"version": "23.07"
			},
			"events": [
				{
					"category": "activation",
					"action": "activated integration",
					"activation_event": true
				}
			]
		};

		const response = request.post(url + '?tenantID=' + encodeURIComponent(tenant_uuid), JSON.stringify(request_body));
		Zabbix.log(4, '[ ACRONIS CYBER PROTECT CLOUD API ] [ ' + url + ' ] Received response with status code ' + request.getStatus() + ': ' + response);

		if (request.getStatus() !== 204) {
			if (typeof (response.error && response.error.message) !== 'undefined') {
				throw response.error.message;
			} else {
				throw 'Failed to receive data: invalid response status code. Check debug log for more information.';
			}
		}

		return 1;
	}
}

function parseGetStatusData(http_response) {
	const data = JSON.parse(http_response);
	if (data['items']) {
		data['items'].forEach(function (app) {
			if (app['application_id'] && app['application_id'] === app_id) {
				return app['active'];
			}
		});
		return false;
	} else {
		throw 'Unexpected data structure in HTTP response.';
	}
}

const params = checkParams(JSON.parse(value)),
	url = params.api_url,
	api_token = params.api_token,
	tenant_uuid = params.tenant_uuid;

const current_activation_status = parseGetStatusData(getHttpData(url + request_path, false, params, api_token, tenant_uuid));

if (!current_activation_status) {
	return getHttpData(url + request_path, true, params, api_token, tenant_uuid);
} else {
	return 0;
}