
var util = require("util");
var events = require("events");
var app_msg = require("./app_message");
var msg_tag = require("./coder").TAG;
var channel = require("./channel")
var ver = require("./version");
const STATE_INITED = 1;
const STATE_REGISTING = 4;
const STATE_REGISTED = 5;
const STATE_CLOSED = 6;

exports.MasterClient = MasterClient;
function MasterClient( appid,pwd ){
	events.EventEmitter.call(this);
	this.queue_svr_ = null;
	this.appid_ = appid;
	this.state_ = STATE_INITED;
	this.channel_client_ = null;
	if(pwd == null) pwd = "";
	this.pwd_ = pwd;
	this.timer_ = null;
}

util.inherits(MasterClient, events.EventEmitter);

MasterClient.prototype.Connect = function(ip,port){
	if(this.state_ != STATE_INITED){
		console.log("state.error.state=",this.state_);
		return ;
	}
	this.channel_client_  = new channel.ChannelClient(this.appid_,ip,port,this.pwd_,ver.APP_VERSION);
	this.state_ = STATE_REGISTING;
	
	this.channel_client_.on("open",this._OnQueueOpen.bind(this));
	this.channel_client_.on("message",this._OnQueueMessage.bind(this));
	this.channel_client_.on("close",this._OnQueueClose.bind(this));
	this.channel_client_.Start();
}
MasterClient.prototype.Close = function(){
	this.state_ = STATE_CLOSED;
	clearInterval(this.timer_);
	if(this.channel_client_ ){
		this.channel_client_.Stop();
		this.channel_client_ = null;
	}
}
MasterClient.prototype._OnQueueOpen = function(){
	this.state_ = STATE_REGISTED;
	this.emit("open");
	this.timer_ = setTimeout(this.channel_client_.StartEmitMessage.bind(this.channel_client_),1000);
}
MasterClient.prototype._OnQueueMessage = function( msg ){
	var tag = msg.GetTag();
	if(tag == msg_tag.ADCLOUD_MESSAGE_TAG_IN){
		this.emit("client_in",msg.client_id,msg.addr,msg.plat,msg.buf_);
	}else if(tag == msg_tag.ADCLOUD_MESSAGE_TAG_MSG){
		this.emit("client_data",msg.client_id,msg.buf_);
	}else if(tag == msg_tag.ADCLOUD_MESSAGE_TAG_OUT){
		this.emit("client_out",msg.client_id);
	}else if(tag == msg_tag.ADCLOUD_MESSAGE_TAG_APP_OTHER_APP_DATA){
		this.emit("app_data",msg.appid,msg.buf_);
	}else if(tag == msg_tag.ADCLOUD_MESSAGE_TAG_APP_ENTER){
		this.emit("app_enter",msg.appid);
	}else if(tag == msg_tag.ADCLOUD_MESSAGE_TAG_APP_LEAVE){
		this.emit("app_leave",msg.appid);
	}else{
		console.log("unknow.tag.",tag);
	}
}
MasterClient.prototype._OnQueueClose = function(err){
	this.Close();
	this.emit("close",err);
}
MasterClient.prototype.AddScope = function(scope,id){
	if(this.state_ != STATE_REGISTED){
		return false;
	}
	var msg = new app_msg.MAppScopeOp(id,true,scope);
	return this.channel_client_.PushMessage(msg);
}
MasterClient.prototype.DelScope = function(scope,id){
	if(this.state_ != STATE_REGISTED){
		return false;
	}
	var msg = new app_msg.MAppScopeOp(id,false,scope);
	return this.channel_client_.PushMessage(msg);
}
MasterClient.prototype.SendToScope = function(scope,data){
	if(this.state_ != STATE_REGISTED){
		return false;
	}
	var buf;
	if( Buffer.isBuffer(data) ){
		buf = data;
	}else{
		if(typeof data != "string"){
			data = JSON.stringify(data);
		}
		buf = new Buffer(Buffer.byteLength(data));
		buf.write(data);
	} 
	var msg = new app_msg.MAppScopeData(scope,buf);
	return this.channel_client_.PushMessage(msg);
}
MasterClient.prototype.CloseClient = function(id){
	if(this.state_ != STATE_REGISTED){
		return false;
	}
	var msg = new app_msg.MAppCloseClient(id);
	return this.channel_client_.PushMessage(msg);
}
MasterClient.prototype.SendToOtherApp = function(appid,data){
	if(this.state_ != STATE_REGISTED){
		return false;
	}
	var buf;
	if( Buffer.isBuffer(data) ){
		buf = data;
	}else{
		if(typeof data != "string"){
			data = JSON.stringify(data);
		}
		buf = new Buffer(Buffer.byteLength(data));
		buf.write(data);
	} 

	var msg = new app_msg.MAppOtherAppData(appid,buf);
	return this.channel_client_.PushMessage(msg);
}

MasterClient.prototype.SendToClient = function(id,data){
	if(this.state_ != STATE_REGISTED){
		return false;
	}
	var buf;
	if( Buffer.isBuffer(data) ){
		buf = data;
	}else{
		if(typeof data != "string"){
			data = JSON.stringify(data);
		}
		buf = new Buffer(Buffer.byteLength(data));
		buf.write(data);
	} 

	var msg = new app_msg.MAppClientData(id,buf);
	return this.channel_client_.PushMessage(msg);
}
MasterClient.prototype.BroadCast = function(data){
	if(this.state_ != STATE_REGISTED){
		return false;
	}
	var buf;
	if( Buffer.isBuffer(data) ){
		buf = data;
	}else{
		if(typeof data != "string"){
			data = JSON.stringify(data);
		}
		buf = new Buffer(Buffer.byteLength(data));
		buf.write(data);
	} 

	var msg = new app_msg.MAppBroadCast(buf);
	return this.channel_client_.PushMessage(msg);
}
MasterClient.prototype.FollowOtherApp = function (appid){
	if(appid == this.appid_) return;
	var msg = new app_msg.MAppFollowOtherApp(appid);
	return this.channel_client_.PushMessage(msg);
}
MasterClient.prototype.UnFollowOtherApp = function (appid){
	if(appid == this.appid_) return;
	var msg = new app_msg.MAppUnFollowOtherApp(appid);
	return this.channel_client_.PushMessage(msg);
}