var Redmine = {
    params: {},

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

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

    addCustomFields: function (data, fields) {
        if (typeof fields === 'object' && Object.keys(fields).length) {
            var schema = Redmine.getSchema();

            if (schema) {
                data.issue.custom_fields = [];

                Object.keys(fields)
                    .forEach(function(field) {
                        for (var idx in schema.custom_fields) {
                            if (schema.custom_fields[idx].id != field) {
                                continue;
                            }

                            var field_value;

                            switch (schema.custom_fields[idx].field_format) {
                                case 'string':
                                    field_value = fields[field];
                                    break;

                                case 'int':
                                    field_value = parseInt(fields[field]);
                                    break;

                                case 'float':
                                    field_value = parseFloat(fields[field]);
                                    break;

                                case 'date':
                                    if (fields[field].match(/^\d{4}[.-]\d{2}[.-]\d{2}$/)) {
                                        field_value = fields[field].replace(/\./g, '-');
                                    }
                                    break;
                            }

                            if (field_value !== undefined) {
                                data.issue.custom_fields.push({id: field, value: field_value});
                            }
                        }
                    });
            }
            else {
                Zabbix.Log(4, '[ Redmine Webhook ] Failed to retrieve field schema.');
            }
        }

        return data;
    },

    request: function (method, query, data) {
        ['url', 'access_key'].forEach(function (field) {
            if (typeof Redmine.params !== 'object' || typeof Redmine.params[field] === 'undefined'
                || Redmine.params[field] === '' ) {
                throw 'Required param is not set: "' + field + '".';
            }
        });

        var response,
            url = Redmine.params.url + query,
            request = new CurlHttpRequest();

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

        request.AddHeader('Content-Type: application/json');
        request.AddHeader('X-Redmine-API-Key: ' + Redmine.params.access_key);

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

        Zabbix.Log(4, '[ Redmine 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, '[ Redmine Webhook ] Received response with status code ' + request.Status() + ': ' + response);

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

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

            if (response !== null && typeof response.errors !== 'undefined'
                && Object.keys(response.errors).length > 0) {
                message += ': ' + JSON.stringify(response.errors);
            }
            else if (response !== null && typeof response.errorMessages !== 'undefined'
                && Object.keys(response.errorMessages).length > 0) {
                message += ': ' + JSON.stringify(response.errorMessages);
            }

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

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

    getProjectID: function(name) {
        var result = Redmine.request('get', 'projects.json'),
            project_id;

        if (result.response) {
            var projects = result.response.projects || [];

            for (var i in projects) {
                if (projects[i].name === name) {
                    project_id = projects[i].id;
                    break;
                }
            }
        }
        else {
            Zabbix.Log(4, '[ Redmine Webhook ] Failed to retrieve project data.');
        }

        if (typeof project_id === 'undefined') {
            throw 'Cannot find project with name: ' + name;
        }

        return project_id;
    },

    getSchema: function() {
        var result = Redmine.request('get', 'custom_fields.json');

        return result.response;
    },

    createIssue: function(subject, description, priority, fields) {
        var project_id = /^\d+$/.test(Redmine.params.project)
                ? Redmine.params.project
                : Redmine.getProjectID(Redmine.params.project),
            data = {
                issue: {
                    project_id: project_id,
                    tracker_id: Redmine.params.tracker_id,
                    subject: subject,
                    description: description
                }
            },
            result;

        if (priority) {
            data.issue.priority_id = priority;
        }

        result = Redmine.request('post', 'issues.json', Redmine.addCustomFields(data, fields));

        if (typeof result.response !== 'object'
            || typeof result.response.issue.id === 'undefined'
            || result.status != 201) {
            throw 'Cannot create Redmine issue. Check debug log for more information.';
        }

        return result.response.issue.id;
    },

    updateIssue: function(note, fields) {
        var data = {
            issue: {
                notes: note || ''
            }
        };

        Redmine.request('put', 'issues/' + Redmine.params.issue_key + '.json', Redmine.addCustomFields(data, fields));
    }
};

try {
    var params = JSON.parse(value),
        params_redmine = {},
        params_fields = {},
        params_update = {},
        result = {tags: {}},
        required_params = [
            'alert_subject', 'tracker_id', 'project',
            '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('redmine_')) {
                params_redmine[key.substring(8)] = params[key];
            }
            else if (key.startsWith('customfield_')) {
                params_fields[key.substring(12)] = 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.redmine_issue_key)) || parseInt(params.redmine_issue_key) < 1 )) {
        throw 'Incorrect "redmine_issue_key" parameter given: ' + params.redmine_issue_key +
            '\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;

    Redmine.setParams(params_redmine);
    Redmine.HTTPProxy = params.HTTPProxy;

    // Create issue for non trigger-based events.
    if (params.event_source !== '0'
        && params.event_value !== '0') {
        Redmine.createIssue(params.alert_subject, params.alert_message, priority);
    }
    // Create issue for trigger-based events.
    else if (params.event_value === '1' && params_update.status === '0') {
        var issue_id = Redmine.createIssue(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_redmine_issue_id = issue_id;
        result.tags.__zbx_redmine_issuelink = params.redmine_url +
            (params.redmine_url.endsWith('/') ? '' : '/') + 'issues/' + issue_id;
    }
    // Update created issue for trigger-based event.
    else {
        Redmine.updateIssue(params.alert_subject + '\n' + params.alert_message, params_fields);
    }

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