
var util = require("util");
var events = require("events");
var  adcloud = require("node_adcloud");

const STATE_INITED = 1;
const STATE_DISPATCHING = 2;
const STATE_DISPATCHED = 3;
const STATE_REGISTING = 4;
const STATE_REGISTED = 5;
const STATE_CLOSED = 6;

exports.Create = function(appid,proxyaddr,pwd){
	return new AdcloudApp(appid,proxyaddr,pwd);
}
exports.version = adcloud.app_version;
function AdcloudApp( appid ,proxyaddr,pwd){
	events.EventEmitter.call(this);
	this.state_ = STATE_INITED;
	this.appid_ = appid;
	
	var self =this;
	this.proxy_client_ = new adcloud.ProxyClient(proxyaddr);
	this.master_client_ = new adcloud.MasterClient(appid,pwd);
	this.master_client_.on("open",function(){
		self.state_ = STATE_REGISTED;
		self.emit("open");
	})
	this.master_client_.on("client_in",function(id,addr,plat,param){
		self.emit("client_in",id,addr,plat,param);
	})
	this.master_client_.on("client_data",function(id,buf){
		self.emit("client_data",id,buf);
	})
	this.master_client_.on("client_out",function(id){
		self.emit("client_out",id);
	})
	this.master_client_.on("app_data",function(fromappid,buf){
		self.emit("app_data",fromappid,buf);
	})
	this.master_client_.on("close",function(err){
		self.emit("close","channel.error=<"+err+">");
	})
	this.master_client_.on("app_enter",function(appid){
		self.emit("app_enter",appid);
	})
	this.master_client_.on("app_leave",function(appid){
		self.emit("app_leave",appid);
	})
}

util.inherits(AdcloudApp, events.EventEmitter);


AdcloudApp.prototype.Start = function(){
	if(this.state_ != STATE_INITED){
		console.log("state.error.state=",this.state_);
		return false;
	}
	this.state_ = STATE_DISPATCHING;
	var self = this;
	this.proxy_client_.GetMaster(this.appid_,1000,function(err,addr){
		if(err != null){
			self.emit("close","get.free.master.fail.err = <"+ err + ">");
		}else{
			if(self.state_ == STATE_CLOSED) return;
			self.state_ = STATE_DISPATCHED;
			self.state_ = STATE_REGISTING;
			self.master_client_.Connect(addr.ip,addr.port);
		}
	})
}

AdcloudApp.prototype.Stop = function(){
	this.removeAllListeners();
	this.master_client_.Close();
	this.state_ = STATE_CLOSED;
}

AdcloudApp.prototype.CloseClient = function(id){
	return this.master_client_.CloseClient(id);
}
AdcloudApp.prototype.SendToClient = function(id,data){
	return this.master_client_.SendToClient(id,data);
}

AdcloudApp.prototype.AddScope = function(scope,id){
	return this.master_client_.AddScope(scope,id);
}
AdcloudApp.prototype.DelScope = function(scope,id){
	return this.master_client_.DelScope(scope,id);
}
AdcloudApp.prototype.SendToScope = function(scope,data){
	return this.master_client_.SendToScope(scope,data);
}
AdcloudApp.prototype.BroadCast = function(data){
	return this.master_client_.BroadCast(data);
}
AdcloudApp.prototype.SendToOtherApp = function (appid,data){
	if(this.appid_ == appid) return false;
	return this.master_client_.SendToOtherApp(appid,data);
}
AdcloudApp.prototype.FollowOtherApp = function(appid){
	return this.master_client_.FollowOtherApp(appid);
}
AdcloudApp.prototype.UnFollowOtherApp = function(appid){
	return this.master_client_.UnFollowOtherApp(appid);
}