function TriggerPoint() {}

TriggerPoint.prototype.Schema =
	"<optional>" +
		"<element name='Reference'>" +
			"<text/>" +
		"</element>" +
	"</optional>";

TriggerPoint.prototype.Init = function()
{
	if (this.template && this.template.Reference)
	{
		var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
		cmpTrigger.RegisterTriggerPoint(this.template.Reference, this.entity);
	}
	this.currentCollections = {};
	this.triggers = {};
};

TriggerPoint.prototype.OnDestroy = function()
{
	if (this.template && this.template.EntityReference)
	{
		var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
		cmpTrigger.RemoveRegisteredTriggerPoint(this.template.Reference, this.entity);
	}
};

/**
 * @param name Name of the trigger.
 * @param data The data is an object containing information for the range query
 * Some of the data has sendible defaults (mentionned next to the object)
 * data.players = [1,2,3,...]  * list of player ids
 * data.minRange = 0           * Minimum range for the query
 * data.maxRange = -1          * Maximum range for the query (-1 = no maximum)
 * data.requiredComponent = 0  * Required component id the entities will have
 * data.enabled = false        * If the query is enabled by default
 */
TriggerPoint.prototype.RegisterRangeTrigger = function(name, data)
{
	var players = data.players || Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetAllPlayers();
	var minRange = data.minRange || 0;
	var maxRange = data.maxRange || -1;
	var cid = data.requiredComponent || 0;

	var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
	var tag = cmpRangeManager.CreateActiveQuery(this.entity, minRange, maxRange, players, cid, cmpRangeManager.GetEntityFlagMask("normal"), true);

	this.currentCollections[tag] = [];
	this.triggers[tag] = name;
	return tag;
};

TriggerPoint.prototype.OnRangeUpdate = function(msg)
{
	var collection = this.currentCollections[msg.tag];
	if (!collection)
		return;

	for (var ent of msg.removed)
	{
		var index = collection.indexOf(ent);
		if (index > -1)
			collection.splice(index, 1);
	}

	for (var entity of msg.added)
		collection.push(entity);

	var r = { "currentCollection": collection.slice() };
	r.added = msg.added;
	r.removed = msg.removed;
	var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
	cmpTrigger.CallTrigger("OnRange", this.triggers[msg.tag], r);
};


Engine.RegisterComponentType(IID_TriggerPoint, "TriggerPoint", TriggerPoint);
