var params = JSON.parse(value),
    fields = ['username', 'password', 'base_url'],
    methods = [
        'system',
        'controllers',
        'controller-statistics',
        'frus',
        'disk-groups',
        'disk-group-statistics',
        'disks',
        'enclosures',
        'fans',
        'pools',
        'ports',
        'power-supplies',
        'volumes',
        'volume-statistics'
    ],
    data_tmp = {},
    result_tmp = {},
    session_key,
    data = {};

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

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

var response, request = new HttpRequest();
request.addHeader('datatype: xml');

auth_string = md5(params.username + '_' + params.password);
response = request.get(params.base_url + 'api/login/' + auth_string);

if (request.getStatus() < 200 || request.getStatus() >= 300) {
    throw 'Authentication request has failed with status code ' + request.getStatus() + ': ' + response;
}

if (response !== null) {
    try {
        session_key = XML.query(response, '/RESPONSE/OBJECT/PROPERTY[@name="response"]/text()');
        return_code = XML.query(response, '/RESPONSE/OBJECT/PROPERTY[@name="return-code"]/text()');
    }
    catch (error) {
        throw 'Failed to parse authentication response received from device API.';
    }
}

if (return_code != '1') {
    throw 'Authentication failed.'
}
else if (session_key === '') {
    throw 'Failed to retrieve session key from authentication response.';
}

request.clearHeader();
request.addHeader('sessionKey: ' + session_key);
request.addHeader('datatype: api-embed');

data.errors = [];

methods.forEach(function (method) {
    response = request.get(params.base_url + 'api/show/' + method);
    method_error = '';

    if (request.getStatus() < 200 || request.getStatus() >= 300) {
        method_error = 'Method: ' + method + '. Request has failed with status code ' + request.getStatus() + ': ' + response;
        data.errors.push(method_error);
        return;
    }

    if (response !== null) {
        try {
            result_tmp = JSON.parse(XML.toJson(response));
            data[method] = [];

            result_tmp.RESPONSE.OBJECT.forEach(function (object) {
                var data_tmp = {};

                if (object['@basetype'] !== 'status' && object['@basetype'] !== 'enclosure-sku') {
                    object.PROPERTY.forEach(function (property) {
                        name = property['@name'];
                        value = property['#text'] || '';
                        data_tmp[name] = value;
                    });
    
                    if (method == 'controller-statistics') {
                        data_tmp['durable-id'] = data_tmp['durable-id'].toLowerCase();
                    }

                    data[method].push(data_tmp);
                }
            });
        }
        catch (error) {
            method_error = 'Method: ' + method + '. Failed to parse response received from device API.';
        }
    }
    else {
        method_error = 'Method: ' + method + '. No data received by request.';
    }

    if (method_error.length > 0) {
        data.errors.push(method_error);
    }
});

if (data.errors.length == 0) {
    data.errors = '';
}

response = request.get(params.base_url + 'api/exit');

if (request.getStatus() < 200 || request.getStatus() >= 300) {
    throw 'Logout request failed with status code ' + request.getStatus() + ': ' + response;
}

return JSON.stringify(data);