var util = require("util");
var BaseTrigger = require("./baseTrigger.js").BaseTrigger;
var Roll = require("roll");
var dice = new Roll();
/*
Trigger that responds to messages using Wolfram Alpha.
command = string - a message must start with this + a space before a response will be given
*/

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

util.inherits(RollTrigger, BaseTrigger);

var type = "RollTrigger";
exports.triggerType = type;
exports.create = function(name, chatBot, options) {
	var trigger = new RollTrigger(type, name, chatBot, options);
	trigger.options.command = trigger.options.command || "!roll";
	trigger.respectsMute = false;
	return trigger;
};

// Return true if a message was sent
RollTrigger.prototype._respondToFriendMessage = function(userId, message) {
	return this._respond(null, userId, message);
}

// Return true if a message was sent
RollTrigger.prototype._respondToChatMessage = function(roomId, chatterId, message) {
	return this._respond(roomId, chatterId, message);
}


RollTrigger.prototype._respond = function(roomId, userId, message) {
	var toId = roomId || userId;

	var that = this;
	var params = that._stripCommand(message, that);
	if(params) {
		try {
			var rolled = dice.roll(params);
			that._sendMessageAfterDelay(toId, rolled.result.toString() + (parseInt(rolled.input.quantity) > 1 ? " ("+JSON.stringify(rolled.rolled).replace(/,/g,", ")+")" : ""));
		} catch(err) {
			that._sendMessageAfterDelay(toId, "Try using syntax from www.github.com/troygoode/node-roll");
		} return true;
	}
	return false;
}

RollTrigger.prototype._stripCommand = function(message, that) {
	if (that.options.command && message && message.toLowerCase().indexOf(that.options.command.toLowerCase() + " ") === 0) {
		return message.substring(that.options.command.length + 1);
	}
	return null;
}

@@ -1213,16 +1213,105 @@
 ) %7B%0A
+%09%09var split = params.split('d');%0A%09%09console.log(split);%0A%09%09if(parseInt(split%5B0%5D) === 1) %7B%0A%09
 %09%09try %7B%0A
 %09%09%09v
@@ -1306,16 +1306,17 @@
 %09%09try %7B%0A
+%09
 %09%09%09var r
@@ -1342,16 +1342,17 @@
 arams);%0A
+%09
 %09%09%09that.
@@ -1514,16 +1514,17 @@
 %22%22));%0A%09%09
+%09
 %7D catch(
@@ -1526,24 +1526,25 @@
 atch(err) %7B%0A
+%09
 %09%09%09that._sen
@@ -1629,16 +1629,17 @@
 roll%22);%0A
+%09
 %09%09%7D retu
@@ -1647,16 +1647,579 @@
 n true;%0A
+%09%09%7D%0A%09%09else if(parseInt(split%5B0%5D) %3E 64 %7C%7C parseInt(split%5B1%5D) %3E 64) %7B%0A%09%09%09this._sendMessageAfterDelay(toId, 'You cannot specify a number greater than 64 on either side of the %22d%22.');%0A%09%09%09return true;%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09try %7B%0A%09%09%09%09var rolled = dice.roll(params);%0A%09%09%09%09that._sendMessageAfterDelay(toId, rolled.result.toString() + (parseInt(rolled.input.quantity) %3E 1 ? %22 (%22+JSON.stringify(rolled.rolled).replace(/,/g,%22, %22)+%22)%22 : %22%22));%0A%09%09%09%7D catch(err) %7B%0A%09%09%09%09that._sendMessageAfterDelay(toId, %22Try using syntax from www.github.com/troygoode/node-roll%22);%0A%09%09%09%7D return true;%0A%09%09%7D%0A
 %09%7D%0A%09retu

Fix !roll abuse