var coder = require("./coder");
var util = require("util");
exports.MAppCloseClient = MAppCloseClient;

exports.MAppClientIn = MAppClientIn;
exports.MAppClientOut = MAppClientOut;
exports.MAppScopeOp = MAppScopeOp;
exports.MAppScopeData = MAppScopeData;
exports.MAppClientData = MAppClientData;
exports.MAppBroadCast = MAppBroadCast;
exports.MAppFollowOtherApp = MAppFollowOtherApp;
exports.MAppUnFollowOtherApp = MAppUnFollowOtherApp;
exports.MAppOtherAppData = MAppOtherAppData;
exports.MAppEnter = MAppEnter;
exports.MAppLeave = MAppLeave;

coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CLOSE,MAppCloseClient);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_IN,MAppClientIn);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_OUT,MAppClientOut);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_SCOPE,MAppScopeOp);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_SCOPE_DATA,MAppScopeData);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_MSG,MAppClientData);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_BROADCAST,MAppBroadCast);

coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_APP_OTHER_APP_DATA,MAppOtherAppData);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_APP_UNFOLLOW_OTHER_APP,MAppUnFollowOtherApp);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_APP_FOLLOW_OTHER_APP,MAppFollowOtherApp);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_APP_ENTER,MAppEnter);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_APP_LEAVE,MAppLeave);

function MAppBace(tag,cid){
	coder.Message.call(this,tag);
	this.client_id = cid;
}
util.inherits(MAppBace, coder.Message);
MAppBace.prototype.BodyLength = function(){
	return 4;
}

MAppBace.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.client_id);
}

MAppBace.prototype.DecodeBody = function(buf_reader){
	this.client_id = buf_reader.ReadUInt32();
}


function MAppCloseClient(cid) {
	MAppBace.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CLOSE,cid);
}
util.inherits(MAppCloseClient,MAppBace);

function MAppClientIn(cid) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_IN);
	this.client_id = cid;
	this.addr = "";
	this.plat = "";
	this.buf_ = null;
}
util.inherits(MAppClientIn, coder.Message);
MAppClientIn.prototype.BodyLength = function(){
	var size = 4 
	+  Buffer.byteLength(this.addr)  + 1
	+  Buffer.byteLength(this.plat)  + 1;
	if(this.buf_){
		size += this.buf_.length;
	}
	return size;
}

MAppClientIn.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.client_id);
	buf_writer.WriteStringAscii(this.addr);
	buf_writer.WriteStringAscii(this.plat);
	if(this.buf_)
		buf_writer.WriteBuffer(this.buf_);
}

MAppClientIn.prototype.DecodeBody = function(buf_reader){
	this.client_id = buf_reader.ReadUInt32();
	this.addr = buf_reader.ReadStringAscii();
	this.plat = buf_reader.ReadStringAscii();
	if(buf_reader.DataSize()){
		this.buf_ = buf_reader.GetDataBuffer();
	}
}
function MAppClientOut(cid) {
	MAppBace.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_OUT,cid);
}
util.inherits(MAppClientOut,MAppBace);




function MAppScopeOp(cid,badd,scope) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_SCOPE);
	this.client_id = cid;
	this.badd = badd;
	this.scope = scope;
}
util.inherits(MAppScopeOp, coder.Message);

MAppScopeOp.prototype.BodyLength = function(){
	var size =4 + 1 + Buffer.byteLength(this.scope) + 1;
	return size;
}

MAppScopeOp.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.client_id);
	buf_writer.WriteBool(this.badd);
	buf_writer.WriteStringAscii(this.scope);
}

MAppScopeOp.prototype.DecodeBody = function(buf_reader){
	this.client_id = buf_reader.ReadUInt32();
	this.client_id = buf_reader.ReadBool();
	this.scope = buf_reader.ReadStringAscii();
}

function MAppScopeData(scope,buf) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_SCOPE_DATA);
	this.scope = scope;
	this.buf_ = buf;
}
util.inherits(MAppScopeData, coder.Message);

MAppScopeData.prototype.BodyLength = function(){
	var size =  1 + Buffer.byteLength(this.scope) + this.buf_.length;
	return size;
}

MAppScopeData.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteStringAscii(this.scope);
	if(this.buf_){
		buf_writer.WriteBuffer(this.buf_);
	}
}

MAppScopeData.prototype.DecodeBody = function(buf_reader){
	this.scope = buf_reader.ReadStringAscii();
	this.buf_ = buf_reader.GetDataBuffer();
	buf_reader.Grow(this.buf_.length);
}
function MAppBroadCast(buf) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_BROADCAST);
	this.buf_ = buf;
}
util.inherits(MAppBroadCast, coder.Message);

MAppBroadCast.prototype.BodyLength = function(){
	var size =  this.buf_.length;
	return size;
}

MAppBroadCast.prototype.EncodeBody= function(buf_writer){
	if(this.buf_){
		buf_writer.WriteBuffer(this.buf_);
	}
}
MAppBroadCast.prototype.DecodeBody = function(buf_reader){
	this.buf_ = buf_reader.GetDataBuffer();
	buf_reader.Grow(this.buf_.length);
}




function MAppClientData(cid,buf){
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_MSG);
	this.client_id = cid;
	this.buf_ = buf;
}
util.inherits(MAppClientData, coder.Message);
MAppClientData.prototype.BodyLength = function(){
	var size = 4 + this.buf_.length;
	return size;
}

MAppClientData.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.client_id);
	if(this.buf_){
		buf_writer.WriteBuffer(this.buf_);
	}
}

MAppClientData.prototype.DecodeBody = function(buf_reader){
	this.client_id = buf_reader.ReadUInt32();
	this.buf_ = buf_reader.GetDataBuffer();
	buf_reader.Grow(this.buf_.length);
}




function MAppOtherAppData(appid,buf) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_APP_OTHER_APP_DATA);
	this.appid = appid;
	this.buf_ = buf;
}
util.inherits(MAppOtherAppData, coder.Message);

MAppOtherAppData.prototype.BodyLength = function(){
	var size =  1 + Buffer.byteLength(this.appid) + this.buf_.length;
	return size;
}

MAppOtherAppData.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteStringAscii(this.appid);
	if(this.buf_){
		buf_writer.WriteBuffer(this.buf_);
	}
}

MAppOtherAppData.prototype.DecodeBody = function(buf_reader){
	this.appid = buf_reader.ReadStringAscii();
	var buf = buf_reader.GetDataBuffer();
	if(buf.length  == 0){
		return;
	}
	this.buf_ = buf;
	buf_reader.Grow(buf.length);
}

function MAppIdBase(tag,appid){
	coder.Message.call(this,tag);
	this.appid = appid;
}
util.inherits(MAppIdBase, coder.Message);
MAppIdBase.prototype.BodyLength = function(){
	var size = Buffer.byteLength(this.appid)  + 1;
	return size;
}

MAppIdBase.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteStringAscii(this.appid);
}

MAppIdBase.prototype.DecodeBody = function(buf_reader){
	this.appid = buf_reader.ReadStringAscii();
}

function MAppFollowOtherApp(appid) {
	MAppIdBase.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_APP_FOLLOW_OTHER_APP,appid);
	this.appid = appid;
}
util.inherits(MAppFollowOtherApp, MAppIdBase);


function MAppUnFollowOtherApp(appid) {
	MAppIdBase.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_APP_UNFOLLOW_OTHER_APP,appid);
}
util.inherits(MAppUnFollowOtherApp, MAppIdBase);

function MAppEnter(appid) {
	MAppIdBase.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_APP_ENTER,appid);
}
util.inherits(MAppEnter, MAppIdBase);

function MAppLeave(appid) {
	MAppIdBase.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_APP_LEAVE,appid);
}
util.inherits(MAppLeave, MAppIdBase);
