

var util = require("util");
var buffer = require('./buffer');
exports.Type = Type;
exports.TypeNull = TypeNull;
exports.TypeError = TypeError;
exports.DecodeType = function(buf_reader){
	var typename = buf_reader.ReadStringAscii();
	if(create_map[typename]){
		var type = new create_map[typename]();
		type.typename = typename;
		type.DecodeBody(buf_reader);
		return type;
	}else{
		console.log("no typename creater typename:"+typename);
		throw new Error();
	}
	return null;
}
var create_map = {};
exports.RegistType = function(typename,Class){
	create_map[typename] = Class;
}

function Type(name){
	this.typename_ = name;
}
Type.prototype.GetTypeName = function(){
	return this.typename_;
}
Type.prototype.Length =  function(){
	return Buffer.byteLength(this.typename_) + 1 + this.BodyLength();
}
Type.prototype.Encode =  function(buf_writer){
	
	buf_writer.WriteStringAscii(this.typename_);
	this.EncodeBody(buf_writer);
}

/*Type.prototype.BodyLength = function(){
	return 0;
}


Type.prototype.DecodeBody = function(buf){
}

Type.prototype.EncodeBody = function(buf){
}*/


function TypeNull(){
	Type.call(this,	"adcloud.rpc.TRpcNull");

}
util.inherits(TypeNull, Type);
TypeNull.prototype.BodyLength = function(){
	return 0;
}
TypeNull.prototype.DecodeBody = function(buf){

}

TypeNull.prototype.EncodeBody = function(buf){

}


function TypeError(err){
	Type.call(this,	"adcloud.rpc.TRpcError");
	this.error_ = err;
}
util.inherits(TypeError, Type);

TypeError.prototype.BodyLength = function(){
	return Buffer.byteLength(this.error_) + 1;
}
TypeError.prototype.DecodeBody = function(buf_reader){
	this.error_ = buf_reader.ReadStringAscii();
}

TypeError.prototype.EncodeBody = function(buf_writer){
	buf_writer.WriteStringAscii(this.error_);
}

exports.RegistType("adcloud.rpc.TRpcError",TypeError);
exports.RegistType("adcloud.rpc.TRpcNull",TypeNull);