var Eda = {
    params: {},

    setParams: function (params) {
        var required_params = [
                'send_to',
                'event_source',
                'event_value',
                'event_date',
                'event_time'],
            integer_fields = [
                'event_id',
                'event_nseverity',
                'event_object',
                'event_source',
                'event_value',
                'host_id',
                'trigger_id'];

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

        Eda.params = params;

        integer_fields.forEach(function (key) {
                if (typeof Eda.params[key] !== 'undefined') {
                    if (isNaN(Eda.params[key])) {
                        throw 'Parameter "' + key + '" must be integer. Given value: ' + Eda.params[key];
                    }
                    Eda.params[key] = parseInt(Eda.params[key]);
                }
            });

        // Check type of event. Possible values: 0 - Trigger
        if (params.event_source != 0) {
            throw ('Incorrect "event_source" parameter given: ' + params.event_source 
                + '\nOnly trigger-based events are supported');
        }

        // Check trigger update and trigger resolve events
        if (params.event_source == 0 && params.event_value != 1) {
            throw ('Incorrect "event_value" parameter given: "' + params.event_value 
                + '".\nOnly trigger-based events of problem are supported');
        }
        
        // Check endpoint
        Eda.params.endpoint = (Eda.params.endpoint.startsWith('/'))
                ? Eda.params.endpoint : '/' + Eda.params.endpoint;
        // Prepare groups
        Eda.params.host_groups = (typeof Eda.params['host_groups'] !== 'undefined')
                ? this.prepareHostgroups(Eda.params.host_groups): []
        // Prepare tags
        Eda.params.event_tags = (typeof Eda.params['event_tags'] !== 'undefined')
                ? this.transformTags(Eda.params.event_tags): {}
        // Prepare timestamp
        Eda.params.event_datetime_timestamp = this.getTimestamp(
            Eda.params.event_date, Eda.params.event_time)
    },

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

    prepareHostgroups: function (hostgroups) {
        var host_groups = []; 
        hostgroups.split(',').forEach(function (group) {
            group = group.trim();
            if (group.length > 0) {
                host_groups.push(group);
            }
        });
        return host_groups;
    },

    getTimestamp: function (date, time) {
        try {
            return Date.parse(date.split('.').join('-') + 'T' + time + '.000Z') / 1000 + new Date().getTimezoneOffset() * 60;
        } catch (_) {}
    
        throw ('Failed to parse event time.');
    },

    transformTags: function (event_tags) {
        var tags = {};
        if (event_tags) {
            try {
                JSON.parse(event_tags).forEach(function (object) {
                    var tag = object['tag'];
                    tags[tag] = tags[tag] || [];
                    tags[tag].push(object['value']);
                });
            } catch (error) {
                throw 'Event tags format is invalid.';
            }
        }
    
        return tags;
    },

    sendMessage: function () {
        var response, request = new HttpRequest();
        var url = Eda.params['send_to'] + Eda.params['endpoint'];
        var data = JSON.stringify(Eda.params);
        Zabbix.log(4, '[ Event-Driven Ansible webhook ] URL: ' + url + ' data: ' + data);
        
        if (typeof Eda.HTTPProxy !== 'undefined' && Eda.HTTPProxy !== '') {
            request.setProxy(Eda.HTTPProxy);
        }
        request.addHeader('Content-Type: application/json');
        response = request.post(url, data);
        
        Zabbix.log(4, '[ Event-Driven Ansible webhook ] HTTP code: ' + request.getStatus()
            + ' response: ' + response);

        if (request.getStatus() !== 200) {
            throw 'Request failed with status code ' + request.getStatus() + ': ' + response;
        }
    }
};


try {
    var params = JSON.parse(value);
    
    Eda.setParams(params);
    Eda.setProxy(params.HTTPProxy);
    Eda.sendMessage();
    return true;
}
catch (error) {
    Zabbix.log(3, '[ Event-Driven Ansible webhook ] notification failed: ' + error);
    throw 'Sending failed: ' + error;
}