var RbmqMessage = require('./RbmqMessage').RbmqMessage;

var RbmqIncomingMessage = function RbmqIncomingMessage(body, headers, deliveryInfo, m) {
    var data = deliveryInfo;
    data.body = body;

    this._m = m;
    this._completed = false;

    RbmqMessage.call(this, data);
};

RbmqIncomingMessage.prototype = Object.create(RbmqMessage.prototype);

RbmqIncomingMessage.prototype.acknowledge = function() {
    console.log('RbmqIncomingMessage/acknowledge', this.getMessageId());

    if (this._completed) {
        throw new Error('Message already acknowledged or rejected');
    }

    this._m.acknowledge();
    this._completed = true;
};

RbmqIncomingMessage.prototype.reject = function(requeue) {
    console.log('RbmqIncomingMessage/reject', this.getMessageId(), requeue);

    if (this._completed) {
        throw new Error('Message already acknowledged or rejected');
    }

    this._m.reject(requeue);
    this._completed = true;
};

exports.RbmqIncomingMessage = RbmqIncomingMessage;
