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

coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_OPEN,ChannelMessageOpen);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_OPENBACK,ChannelMessageOpenBack);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_REOPEN,ChannelMessageReOpen);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_REOPEN_BACK,ChannelMessageReOpenBack);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_MESSAGE,ChannelMessageData);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_MESSAGE_BACK,ChannelMessageDataBack);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_CLOSE,ChannelMessageClose);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_KEEP_ALIVE,ChannelMessageKeepAlive);
exports.ChannelMessageOpen = ChannelMessageOpen;
exports.ChannelMessageOpenBack = ChannelMessageOpenBack;
exports.ChannelMessageReOpen = ChannelMessageReOpen;
exports.ChannelMessageReOpenBack = ChannelMessageReOpenBack;
exports.ChannelMessageData = ChannelMessageData;
exports.ChannelMessageDataBack = ChannelMessageDataBack;
exports.ChannelMessageClose = ChannelMessageClose;
exports.ChannelMessageKeepAlive = ChannelMessageKeepAlive;

function ChannelMessageOpen(id,pwd,version) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_OPEN);
	this.id = id;
	if(!pwd) pwd = "";
	this.pwd = pwd;
	this.plat = "nodejs";
	this.version = version;
}
util.inherits(ChannelMessageOpen, coder.Message);

ChannelMessageOpen.prototype.BodyLength = function(){
	var size = Buffer.byteLength(this.id)  + 1 
	+ Buffer.byteLength(this.pwd)  + 1  
	+ Buffer.byteLength(this.plat)  + 1 
	+ Buffer.byteLength(this.version)  + 1 ;
	return size;
}

ChannelMessageOpen.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteStringAscii(this.id);
	buf_writer.WriteStringAscii(this.pwd);
	buf_writer.WriteStringAscii(this.plat);
	buf_writer.WriteStringAscii(this.version);
}

ChannelMessageOpen.prototype.DecodeBody = function(buf_reader){
	this.id = buf_reader.ReadStringAscii();
	this.pwd = buf_reader.ReadStringAscii();
	this.plat = buf_reader.ReadStringAscii();
	this.version = buf_reader.ReadStringAscii();
}

function ChannelMessageOpenBack() {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_OPENBACK);
	this.session = 0;
	this.code = 0;
	this.desc = "";
}
util.inherits(ChannelMessageOpenBack, coder.Message);

ChannelMessageOpenBack.prototype.BodyLength = function(){
	return 5 +  Buffer.byteLength(this.desc)  + 1 ;;
}

ChannelMessageOpenBack.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt8(this.code);
	buf_writer.WriteUInt32(this.session);
	buf_writer.WriteStringAscii(this.desc);
}

ChannelMessageOpenBack.prototype.DecodeBody = function(buf_reader){
	this.code = buf_reader.ReadUInt8();
	this.session = buf_reader.ReadUInt32();
	this.desc = buf_reader.ReadStringAscii();
}

function ChannelMessageReOpen(session,term,id) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_REOPEN);
	this.session = session;
	this.term = term;
	this.id = id
}
util.inherits(ChannelMessageReOpen, coder.Message);

ChannelMessageReOpen.prototype.BodyLength = function(){
	return 8 + Buffer.byteLength(this.id)  + 1 ;;
}

ChannelMessageReOpen.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.term);
	buf_writer.WriteUInt32(this.session);
	buf_writer.WriteStringAscii(this.id);
}

ChannelMessageReOpen.prototype.DecodeBody = function(buf_reader){
	this.term = buf_reader.ReadUInt32();
	this.session = buf_reader.ReadUInt32();
	this.id = buf_reader.ReadStringAscii();
}


function ChannelMessageReOpenBack() {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_REOPEN_BACK);
	this.term = 0;
	this.code = 0;
}
util.inherits(ChannelMessageReOpenBack, coder.Message);

ChannelMessageReOpenBack.prototype.BodyLength = function(){
	return 5;
}

ChannelMessageReOpenBack.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt8(this.code);
	buf_writer.WriteUInt32(this.term);
}

ChannelMessageReOpenBack.prototype.DecodeBody = function(buf_reader){
	this.code = buf_reader.ReadUInt8();
	this.term = buf_reader.ReadUInt32();
}

function ChannelMessageData(term,arr) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_MESSAGE);
	this.term = term;
	if(!arr){
		this.arr = [];
	}else{
		this.arr = arr;
	}
	
}
util.inherits(ChannelMessageData, coder.Message);

ChannelMessageData.prototype.BodyLength = function(){
	var size = 4;
	for(var i in this.arr){
		size += this.arr[i].Length();
	}
	return size;
}

ChannelMessageData.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.term);
	for(var i in this.arr){
		this.arr[i].Encode(buf_writer);
	}
}

ChannelMessageData.prototype.DecodeBody = function(buf_reader){
	this.term = buf_reader.ReadUInt32();
	while(buf_reader.DataSize() > 0){
		var msg = coder.DecodeMessage(buf_reader);
		if(!msg) break;
		this.arr.push(msg);
	}
}

function ChannelMessageDataBack(term) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_MESSAGE_BACK);
	this.term = 0;
	if(term){
		this.term = term;
	}
}
util.inherits(ChannelMessageDataBack, coder.Message);

ChannelMessageDataBack.prototype.BodyLength = function(){
	return 4;
}

ChannelMessageDataBack.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.term);
}

ChannelMessageDataBack.prototype.DecodeBody = function(buf_reader){
	this.term = buf_reader.ReadUInt32();
}

function ChannelMessageClose(){
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_CLOSE);
}
util.inherits(ChannelMessageClose, coder.Message);

function ChannelMessageKeepAlive(time,ping){
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CHANNEL_KEEP_ALIVE);
	if(!time) time = "";
	if(!ping) ping = 0;
	this.time = String(time);
	this.ping = ping;
}
util.inherits(ChannelMessageKeepAlive, coder.Message);

ChannelMessageKeepAlive.prototype.BodyLength = function(){
	return Buffer.byteLength(this.id)  + 1 +4;
}

ChannelMessageKeepAlive.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.ping);
	buf_writer.WriteStringAscii(this.time);
}

ChannelMessageKeepAlive.prototype.DecodeBody = function(buf_reader){
	this.ping = buf_reader.ReadUInt32();
	this.time = buf_reader.ReadStringAscii();
}