"use strict";

/**
 * @module
 */

/**
 * Parses a string into the format of a requests then provides accessor methods.
 * @example
 * Fully implemented
 * 
 * Request format:
 * 
 * HEAD;
 * {headers}
 * BODY;
 * {data}
 * 
 * Notes:
 * 
 * HEAD; and BODY; must be on a newline with no whitespace on the same line.
 * Whitespace is ignored when defining headers (data between HEAD & BODY).
 * 
 * @example
 * Proposed future use.
 * 
 * CdpRequest (comments after //):
 * 
 * HEAD;
 * command: AuthenticateAgent;			// String command name that identifies this request
 * wrapping: encrypted(compressed());		// Shows that it was first compressed, then encrypted
 * requestFormat: json;				// Request format of the body section is in json
 * responseFormat: json;				// Response format of the body section is to be sent in json
 * BODY;
 * Hello World!
 * 
 * @example
 * Available headers & values:
 * 
 * command: {string};				// Required
 * requestFormat: json|string;			// Defaults to json (will automatically try to parse the body and return an object instead of a string)
 * responseFormat: json|string;			// Defaults to json (will automatically try to parse the body and return an object instead of a string)
 * 
 * @constructor
 */
module.exports = function() {
	this._head = {};
	this._body = {};
};

/**
 * Set the head section of the request
 * @param {object} head section
 */
module.exports.prototype.setHead = function(head) {
	this._head = head;
};
module.exports.prototype.setBody = function(body) {
	this._body = body;
};
module.exports.prototype.getCommand = function() {
	return this._head.cmd;
};
module.exports.prototype.getBody = function() {
	return this._body;
};
module.exports.prototype.toString = function() {
	return JSON.stringify({head: this._head, body:this._body});
};
module.exports.fromString = function(str) {
	var request = new module.exports();
	var parsedJson = JSON.parse(str);
	request.setHead(parsedJson["head"]);
	request.setBody(parsedJson["body"]);
	return request;
};