var VictorOps = {
    params: {},

    setParams: function (params) {
        if (typeof params !== 'object') {
            return;
        }

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

            if (typeof VictorOps.params.routing_key !== 'undefined'
                    && VictorOps.params.routing_key !== '{ALERT.SENDTO}'
                    && VictorOps.params.routing_key !== 'Default') {
                VictorOps.params.endpoint += VictorOps.params.routing_key;
            }
        }
    },

    setProxy: function (HTTPProxy) {
        VictorOps.HTTPProxy = HTTPProxy;
    },

    addFields: function (fields) {
        var data = {};

        if (typeof fields === 'object') {
            Object.keys(fields)
                .forEach(function(field) {
                    if (fields[field] === '') {
                        Zabbix.Log(4, '[ VictorOps Webhook ] Field "' + field +
                            '" can\'t be empty. The field ignored.');
                    }
                    else {
                        try {
                            var parts = field.split(':'),
                                prefix = parts[0].split('_');

                            if (typeof prefix[1] === 'undefined'
                                    || (prefix[1] === 'p' && params.event_value === '1'
                                        && (params.event_update_status === '0'
                                            || params.event_update_status === '{EVENT.UPDATE.STATUS}'))
                                    || (prefix[1] === 'r' && params.event_value === '0'
                                        && (params.event_update_status === '0'
                                            || params.event_update_status === '{EVENT.UPDATE.STATUS}'))
                                    || (prefix[1] === 'u' && params.event_update_status === '1')) {
                                data[field.substring(field.indexOf(':') + 1)] = fields[field];
                            }
                        }
                        catch (error) {
                            Zabbix.Log(4, '[ VictorOps Webhook ] Can\'t parse field "' + field +
                                '". The field ignored.');
                        }
                    }
                });
        }

        return data;
    },

    request: function (data) {
        if (typeof VictorOps.params !== 'object' || typeof VictorOps.params.endpoint === 'undefined'
                || VictorOps.params.endpoint === '' ) {
            throw 'Required parameter is not set: "vops_endpoint".';
        }

        var response,
            url = VictorOps.params.endpoint,
            request = new CurlHttpRequest();

        request.AddHeader('Content-Type: application/json');

        if (typeof VictorOps.HTTPProxy !== 'undefined' && VictorOps.HTTPProxy !== '') {
            request.SetProxy(VictorOps.HTTPProxy);
        }

        if (typeof data !== 'undefined') {
            data = JSON.stringify(data);
        }

        Zabbix.Log(4, '[ VictorOps Webhook ] Sending request: ' + url +
            ((typeof data === 'string') ? ('\n' + data) : ''));

        response = request.Post(url, data);

        Zabbix.Log(4, '[ VictorOps Webhook ] Received response with status code ' +
            request.Status() + '\n' + response);

        if (response !== null) {
            try {
                response = JSON.parse(response);
            }
            catch (error) {
                Zabbix.Log(4, '[ VictorOps Webhook ] Failed to parse response received from VictorOps');
                response = null;
            }
        }

        if (request.Status() < 200 || request.Status() >= 300) {
            var message = 'Request failed with status code ' + request.Status();

            if (response !== null && typeof response.messages !== 'undefined') {
                message += ': ' + JSON.stringify(response.messages);
            }

            throw message + '. Check debug log for more information.';
        }

        return response;
    }
};

try {
    var params = JSON.parse(value),
        fields = {},
        vops = {},
        required_params = ['event_source', 'event_value', 'priority_update'],
        severities = [
            {name: 'not_classified', color: '#97AAB3'},
            {name: 'information', color: '#7499FF'},
            {name: 'warning', color: '#FFC859'},
            {name: 'average', color: '#FFA059'},
            {name: 'high', color: '#E97659'},
            {name: 'disaster', color: '#E45959'},
            {name: 'resolved', color: '#009900'},
            {name: 'default', color: '#000000'}
        ];

    Object.keys(params)
        .forEach(function (key) {
            if (key.startsWith('vops_')) {
                vops[key.substring(5)] = params[key];
            }
            else if (key.startsWith('field')) {
                fields[key.substring(5)] = params[key];
            }
            else if (required_params.indexOf(key) !== -1 && params[key] === '') {
                throw 'Parameter "' + key + '" can\'t be empty.';
            }
        });

    if ([0, 1, 2, 3].indexOf(parseInt(params.event_source)) === -1) {
        throw 'Incorrect "event_source" parameter given: ' + params.event_source + '\nMust be 0-3.';
    }

    // Check {EVENT.VALUE} for trigger-based and internal events.
    if (params.event_value !== '0' && params.event_value !== '1'
            && (params.event_source === '0' || params.event_source === '3')) {
        throw 'Incorrect "event_value" parameter given: ' + params.event_value + '\nMust be 0 or 1.';
    }

    // Check {EVENT.UPDATE.STATUS} only for trigger-based events.
    if (params.event_update_status !== '0' && params.event_update_status !== '1' && params.event_source === '0') {
        throw 'Incorrect "event_update_status" parameter given: ' + params.event_update_status + '\nMust be 0 or 1.';
    }

    if ([0, 1, 2, 3, 4, 5].indexOf(parseInt(params.event_nseverity)) === -1) {
        params.event_nseverity = '7';
    }

    if (params.event_value === '0') {
        params.event_nseverity = '6';
    }

    if (params.event_update_status === '1') {
        fields[':message_type'] = params.priority_update;
    }
    else {
        fields[':message_type'] = params['priority_' + severities[params.event_nseverity].name]
            || 'INFO';
    }

    if (params.event_info && params.event_source === '0') {
        fields[':event_info'] = params.event_info;
    }

    VictorOps.setParams(vops);
    VictorOps.setProxy(params.HTTPProxy);
    VictorOps.request(VictorOps.addFields(fields));

    return 'OK';
}
catch (error) {
    Zabbix.Log(3, '[ VictorOps Webhook ] ERROR: ' + error);
    throw 'Sending failed: ' + error;
}