var OTRS = {
    params: {},
    entrypoint: 'nph-genericinterface.pl/Webservice/ZabbixTicketConnector/Ticket',

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

        OTRS.params = params;
        if (typeof OTRS.params.url === 'string') {
            if (!OTRS.params.url.endsWith('/')) {
                OTRS.params.url += '/';
            }
        }
    },

    addDynamicFields: function (data, fields) {
        if (typeof fields === 'object' && Object.keys(fields).length) {
            data.DynamicField = [];

            Object.keys(fields)
                .forEach(function(field) {
                    if (field !== undefined) {
                        if (fields[field].match(/^\d{4}[.-]\d{2}[.-]\d{2}$/)) {
                            fields[field] = fields[field].replace(/\./g, '-');
                        }

                        data.DynamicField.push({Name: field, Value: fields[field]});
                    }
                });
        }

        return data;
    },

    request: function (method, query, data) {
        ['url', 'auth_user', 'auth_password', 'queue',
            'customer', 'ticket_state', 'default_priority_id', 'time_unit'].forEach(function (field) {
            if (typeof OTRS.params !== 'object' || typeof OTRS.params[field] === 'undefined'
                || OTRS.params[field] === '' ) {
                throw 'Required param is not set: "' + field + '".';
            }
        });

        var response,
            request = new CurlHttpRequest(),
            url = OTRS.params.url + query +
                '?UserLogin=' + encodeURIComponent(OTRS.params.auth_user) +
                '&Password=' + encodeURIComponent(OTRS.params.auth_password);

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

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

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

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

        switch (method) {
            case 'get':
                response = request.Get(url, data);
                break;

            case 'post':
                response = request.Post(url, data);
                break;

            case 'put':
                response = request.Put(url, data);
                break;

            default:
                throw 'Unsupported HTTP request method: ' + method;
        }

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

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

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

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

        if (typeof response.Error !== 'undefined'
            && Object.keys(response.Error).length > 0) {
            message = 'Request failed: ' + JSON.stringify(response.Error);
        }


        return {
            status: request.Status(),
            response: response
        };
    },

    createTicket: function(subject, message, priority, fields) {
        var result,
            data = {
                Ticket: {
                    Title: subject,
                    Queue: OTRS.params.queue,
                    State: OTRS.params.ticket_state,
                    PriorityID: priority || OTRS.params.default_priority_id,
                    CustomerUser: OTRS.params.customer
                },
                Article: {
                    Subject: subject,
                    Body: message,
                    TimeUnit: OTRS.params.time_unit,
                    ContentType: 'text/plain; charset=utf8'
                }
            };

        result = OTRS.request('post', OTRS.entrypoint, OTRS.addDynamicFields(data, fields));

        if (typeof result.response !== 'object'
            || typeof result.response.TicketID === 'undefined'
            || result.status != 200) {
            throw 'Cannot create OTRS ticket. Check debug log for more information.';
        }

        return result.response.TicketID;
    },

    updateTicket: function(subject, message) {
        var result,
            data = {
                Article: {
                    Subject: subject,
                    Body: message || '',
                    TimeUnit: OTRS.params.time_unit,
                    ContentType: 'text/plain; charset=utf8'
                }
            };

        result = OTRS.request('put', OTRS.entrypoint + '/' + encodeURIComponent(OTRS.params.ticket_id), data);

        if (typeof result.response !== 'object'
            || typeof result.response.TicketID === 'undefined'
            || result.status != 200) {
            throw 'Cannot update OTRS ticket. Check debug log for more information.';
        }
    }
};

try {
    var params = JSON.parse(value),
        params_otrs = {},
        params_fields = {},
        params_update = {},
        result = {tags: {}},
        required_params = [
            'alert_subject', 'event_source',
            'event_value',  'event_update_status'
        ],
        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: null, color: '#000000'}
        ],
        priority;

    Object.keys(params)
        .forEach(function (key) {
            if (key.startsWith('otrs_')) {
                params_otrs[key.substring(5)] = params[key];
            }
            else if (key.startsWith('dynamicfield_')) {
                params_fields[key.substring(13)] = params[key];
            }
            else if (key.startsWith('event_update_')) {
                params_update[key.substring(13)] = params[key];
            }
            else if (required_params.indexOf(key) !== -1 && params[key].trim() === '') {
                throw 'Parameter "' + key + '" cannot 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_source === '0' && params.event_update_status !== '0' && params.event_update_status !== '1') {
        throw 'Incorrect "event_update_status" parameter given: ' + params.event_update_status + '\nMust be 0 or 1.';
    }

    if (params.event_source !== '0' && params.event_value === '0') {
        throw 'Recovery operations are supported only for trigger-based actions.';
    }

    if (params.event_source === '0'
        && ((params.event_value === '1' && params.event_update_status === '1')
            || (params.event_value === '0'
                && (params.event_update_status === '0' || params.event_update_status === '1')))
        && (isNaN(parseInt(params.otrs_ticket_id)) || parseInt(params.otrs_ticket_id) < 1 )) {
        throw 'Incorrect "otrs_ticket_id" parameter given: ' + params.otrs_ticket_id +
            '\nMust be positive integer.';
    }

    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';
    }

    priority = params['severity_' + severities[params.event_nseverity].name];
    priority = priority && priority.trim() || severities[7].name;

    OTRS.setParams(params_otrs);
    OTRS.HTTPProxy = params.HTTPProxy;

    // Create ticket for non trigger-based events.
    if (params.event_source !== '0'
        && params.event_value !== '0') {
        OTRS.createTicket(params.alert_subject, params.alert_message, priority);
    }
    // Create ticket for trigger-based events.
    else if (params.event_value === '1' && params_update.status === '0') {
        var ticket_id = OTRS.createTicket(params.alert_subject,
            params.alert_subject + '\n' + params.alert_message + '\n' +
            params.zabbix_url + (params.zabbix_url.endsWith('/') ? '' : '/') +
            'tr_events.php?triggerid=' + params.trigger_id + '&eventid=' + params.event_id + '\n',
            priority,
            params_fields);

        result.tags.__zbx_otrs_ticket_id = ticket_id;
        result.tags.__zbx_otrs_ticketlink = params.otrs_url +
            (params.otrs_url.endsWith('/') ? '' : '/') + 'index.pl?Action=AgentTicketZoom;TicketID=' + ticket_id;
    }
    // Update created ticket for trigger-based event.
    else {
        OTRS.updateTicket(params.alert_subject, params.alert_message);
    }

    return JSON.stringify(result);
}
catch (error) {
    Zabbix.Log(3, '[ OTRS Webhook ] ERROR: ' + error);
    throw 'Sending failed: ' + error;
}