var Veeam = {
    params: {},

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

        Veeam.params = params;
        if (typeof Veeam.params.api_endpoint === 'string' && !Veeam.params.api_endpoint.endsWith('/')) {
            Veeam.params.api_endpoint += '/';
        }
    },

    request: function (url) {
        var response, request = new HttpRequest();
        if (typeof Veeam.params.http_proxy !== 'undefined' && Veeam.params.http_proxy !== '') {
            request.setProxy(Veeam.params.http_proxy);
        }
        request.addHeader('Accept: application/json');
        request.addHeader('Authorization: Basic ' + btoa(Veeam.params.user + ':' + Veeam.params.password));
        request.post(Veeam.params.api_endpoint + 'api/sessionMngr');
        response = request.get(url);
        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.';
        }
    },

    getMetricsData: function() {
        var data = {};
        endpoints = {
            'backupFiles': 'api/backupFiles',
        };
        reports_summary = Veeam.request(Veeam.params.api_endpoint + 'api/reports/summary')
        if (typeof reports_summary !== 'object' || reports_summary.hasOwnProperty('Links') === false)
            throw 'Failed response parse. Check debug log for more information.';
        reports_summary.Links.forEach(function (field)  {
                data[field.Name] = Veeam.request(field.Href)
            })
        Object.keys(endpoints).forEach(function (key) {
            data[key] = Veeam.request(Veeam.params.api_endpoint + endpoints[key]);
            if (typeof data[key] !== 'object' || data[key].hasOwnProperty('Refs') === false)
                throw 'Failed response parse. Check debug log for more information.';
            data[key].Refs.forEach(function (field) {
                data[field.Name] = Veeam.request(field.Href)
            })
        
        })
        return data;
    }

};  
try {
    Veeam.setParams(JSON.parse(value));
    return JSON.stringify(Veeam.getMetricsData());
}
catch (error) {
    error += (String(error).endsWith('.')) ? '' : '.';
    Zabbix.log(3, '[ VEEAM MANAGER ] ERROR: ' + error);
    return JSON.stringify({'error': error});
}