var params = JSON.parse(value);

var request = new HttpRequest();

request.addHeader('X-Cisco-Meraki-API-Key:' + params.token);

var response,
    error_msg = '',
    networks = [],
    uplinks = [];

function getHttpData(url) {
    response = request.get(url);
    Zabbix.log(4, '[ Meraki API ] [ ' + url + ' ] Received response with status code ' + request.getStatus() + ': ' + response);

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

    if (request.getStatus() !== 200) {
        if (response.errors) {
            throw response.errors.join(', ');
        } else {
            throw 'Failed to receive data: invalid response status code.';
        }
    }

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

    return response;
};

try {

    if (params.token === '{' + '$MERAKI.TOKEN}') {
        throw 'Please change {' + '$MERAKI.TOKEN} macro with the proper value.';
    }

    if (params.url.indexOf('http://') === -1 && params.url.indexOf('https://') === -1) {
        params.url = 'https://' + params.url;
    }

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

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

    networks = getHttpData(params.url + 'organizations/' + encodeURIComponent(params.organizationId) + '/networks');

    responseUplinks = getHttpData(params.url + 'organizations/' + encodeURIComponent(params.organizationId) + '/appliance/uplink/statuses');

    if (typeof responseUplinks !== 'undefined' && Array.isArray(responseUplinks)) {
        for (var i in responseUplinks) {
            if ('networkId' in responseUplinks[i] && typeof networks !== 'undefined' && Array.isArray(networks)) {
                network = networks.filter(function (x) { return x.id == responseUplinks[i].networkId; });
            }

            if (typeof responseUplinks[i].uplinks !== 'undefined' && Array.isArray(responseUplinks[i].uplinks)) {
                for (var p in responseUplinks[i].uplinks) {
                    if (typeof network[0].name !== 'undefined') {
                        responseUplinks[i].uplinks[p].networkName = network[0].name;
                    }
                    if (typeof network[0].timeZone !== 'undefined') {
                        responseUplinks[i].uplinks[p].timeZone = network[0].timeZone;
                    }
                    if ('highAvailability' in responseUplinks[i] && 'role' in responseUplinks[i].highAvailability) {
                        responseUplinks[i].uplinks[p].role = responseUplinks[i].highAvailability.role;
                    }
                    if ('serial' in responseUplinks[i]) {
                        responseUplinks[i].uplinks[p].serial = responseUplinks[i].serial;
                    }

                    uplinks.push(responseUplinks[i].uplinks[p]);
                }
            }
        }
    }


} catch (error) {
    error_msg = error;
};

return JSON.stringify({
    'uplinks': uplinks,
    'networks': networks,
    'error': error_msg.toString()
});