var util = require("util");

var BaseTrigger = require("./baseTrigger.js").BaseTrigger;

/*
Trigger that responds to certain messages with specified responses.
matches = array of strings - messages that trigger the response
responses = array of strings - the response will be a randomly selected string from this array
exact = boolean - if this is true, the message received must be an exact match, if it's false the message just must contain the match (both case-insensitive)
users = array of string - the steamIds of the users that can trigger a response, can be null or empty to match all users
respond = string - where to respond? in 'group', 'pm', or nothing to respond wherever the command was sent (default)
*/

var ChatReplyTrigger = function() {
	ChatReplyTrigger.super_.apply(this, arguments);
};

util.inherits(ChatReplyTrigger, BaseTrigger);

var type = "ChatReplyTrigger";
exports.triggerType = type;
exports.create = function(name, chatBot, options) {
	var trigger = new ChatReplyTrigger(type, name, chatBot, options);
	return trigger;
};

// Return true if a message was sent
ChatReplyTrigger.prototype._respondToFriendMessage = function(userId, message) {
	if(!this.options.respond || this.options.respond !== "group") {
		return this._respond(userId, message, userId);
	}
}

// Return true if a message was sent
ChatReplyTrigger.prototype._respondToChatMessage = function(roomId, chatterId, message) {
	if(!this.options.respond || this.options.respond !== "pm") {
		return this._respond(roomId, message, chatterId);
	}
}


ChatReplyTrigger.prototype._respond = function(toId, message, fromId) {
	message = message.split("ː").join(":");
	if (this._messageTriggers(toId, message, fromId)) {
		var response = this._pickResponse();
		this._sendMessageAfterDelay(toId, response);

		return true;
	}
	return false;
}

ChatReplyTrigger.prototype._messageTriggers = function(toId, message, fromId) {
	if (!message) {
		return false; 
	}

	if (!this._checkMessage(message)) {
		return false;
	}

	return true;
}

// Check for any text match
ChatReplyTrigger.prototype._checkMessage = function(message) {
	if (!this.options.matches || this.options.matches.length === 0) {
		return true; // match-all
	}

	for (var i=0; i < this.options.matches.length; i++) {
		var match = this.options.matches[i];
		if ((this.options.exact && message.toLowerCase() === match.toLowerCase()) || 
			(!this.options.exact && message.toLowerCase().indexOf(match.toLowerCase()) >= 0)) {

			return true;
		}
	}

	return false;
}

ChatReplyTrigger.prototype._pickResponse = function(message) {
	if (this.options.responses && this.options.responses.length > 0) {
		var index = Math.floor(Math.random() * this.options.responses.length);
		return this.options.responses[index];
	}
	return "";
}

@@ -708,16 +708,143 @@
 efault)%0A
+inPrivate = bool - respond in groupchat (default) or privately? (will always respond in private if command was sent privately)%0A
 */%0A%0Avar 
@@ -1153,16 +1153,179 @@
 tions);%0A
+%09trigger.options.inPrivate = options.inPrivate %7C%7C false;%0A%09trigger.options.respond = options.respond %7C%7C undefined;%0A%09trigger.options.exact = options.exact %7C%7C false;%0A
 %09return 
@@ -1451,33 +1451,32 @@
  message) %7B%0A%09if(
-!
 this.options.res
@@ -1483,49 +1483,38 @@
 pond
- %7C%7C this.options.respond !== %22group%22) %7B%0A%09
+===%22pm%22) %7B%0A%09%09return false;%0A%09%7D%0A
 %09ret
@@ -1557,19 +1557,16 @@
 serId);%0A
-%09%7D%0A
 %7D%0A%0A// Re
@@ -1691,17 +1691,16 @@
 ) %7B%0A%09if(
-!
 this.opt
@@ -1715,46 +1715,41 @@
 pond
- %7C%7C this.options.respond !== %22pm%22) %7B%0A%09
+===%22group%22) %7B%0A%09%09return false;%0A%09%7D%0A
 %09ret
@@ -1795,19 +1795,16 @@
 terId);%0A
-%09%7D%0A
 %7D%0A%0A%0AChat
@@ -1871,16 +1871,82 @@
 omId) %7B%0A
+%09var dest = toId;%0A%09if(this.options.inPrivate) %7B%0A%09%09dest=fromId;%0A%09%7D%0A
 %09message

Add destination for chatReplyTrigger