var JsonRpcResponse = require('../jsonrpc/JsonRpcResponse.js').JsonRpcResponse;
var Response = require('../net/Response.js').Response;
var Types = require('../common/Types.js');

var OpalResponse = exports.OpalResponse = function (params) {
    Response.call(this, params);
};

OpalResponse.prototype = Object.create(Response.prototype);

OpalResponse.prototype.setBody = OpalResponse.prototype._setBody = function (body) {
    var isValid = false;

    if (Types.isInstanceOf(body, JsonRpcResponse) || Types.isInstanceOf(body, Response)) {
        isValid = true;
    } else if (Types.isString(body)) {
        try {
            body = new JsonRpcResponse(body);
            isValid = true;
        } catch (e) {}
    }

    if (!isValid) {
        throw OpalResponse.Exceptions.WRONG_BODY;
    }

    this._body = body;
    
    return this;
};

OpalResponse.Exceptions = {}
OpalResponse.Exceptions.WRONG_BODY = 'Body has to be either a proper json-rpc string or object';
