/* Seen command
 * by jd and panpawn
 */

var fs = require('fs');
var moment = require('moment');

var seenData = {};
function loadData() {
	try {
		seenData = JSON.parse(fs.readFileSync('config/seenData.json', 'utf8'));
	} catch (e) {
		seenData = {};
	}
}
loadData();

function saveData() {
	fs.writeFileSync('config/seenData.json', JSON.stringify(seenData));
}

function updateSeen(userid) {
	if (!userid) return false;
	seenData[toId(userid)] = Date.now();
	saveData();
}
global.updateSeen = updateSeen;

exports.commands = {
	seen: function (target, room, user) {
		try {
			switch (target) {
				case 'obj':
					if (!this.canBroadcast()) return;
					this.sendReplyBox("There have been " + Object.size(seenData) + " user names recorded in this database.");
					break;
				default:
					if (!this.canBroadcast()) return;
					var userid = toId(target);
					if (userid.length < 1) return this.sendReply("/seen - Please specify a name.");
					if (Users(target) && Users(target).connected) return this.sendReplyBox(Tools.escapeHTML(target) + " is currently <font color=\"green\">online</green>.");
					if (!seenData[userid]) return this.sendReplyBox(Tools.escapeHTML(target) + " has <font color=\"red\">never</font> been seen online.");
					var date = new Date(seenData[userid]);
					var userLastSeen = moment(seenData[userid]).format("MMMM Do YYYY, h:mm:ss a");
					var userLastSeenLabel = userLastSeen.substr(-2).toUpperCase(); //AM or PM
					this.sendReplyBox("The user " + Tools.escapeHTML(target) + " was last seen online " + userLastSeen.substring(0, userLastSeen.length - 2) + userLastSeenLabel + " EST.");
			}
		} catch (e) {
			return this.sendReply("Something failed: \n" + e.stack);
		}
	}
};

@@ -501,17 +501,67 @@
 teSeen;%0A
+var userNameColor = Core.hashColor(toId(target)); 
 %0A
-
 exports.
@@ -1068,36 +1068,73 @@
 Box(
-Tools.escapeHTML(
+'%3Cb%3E%3Cfont color=%22' + userNameColor + '%22%3E' + 
 target
-)
  + 
-%22
+'%3C/font%3E%3C/b%3E
  is 
@@ -1159,16 +1159,14 @@
 lor=
-%5C
 %22green
-%5C
 %22%3Eon
@@ -1178,17 +1178,17 @@
 /green%3E.
-%22
+'
 );%0A%09%09%09%09%09
@@ -1239,36 +1239,73 @@
 Box(
-Tools.escapeHTML(
+'%3Cb%3E%3Cfont color=%22' + userNameColor + '%22%3E' + 
 target
-)
  + %22
+%3C/font%3E%3C/b%3E
  has
@@ -1589,17 +1589,17 @@
 eplyBox(
-%22
+'
 The user
@@ -1603,40 +1603,72 @@
 ser 
-%22 + Tools.escapeHTML(
+%3Cb%3E%3Cfont color=%22' + userNameColor + '%22%3E' + 
 target
-)
  + 
-%22
+'%3C/font%3E%3C/b%3E
  was
@@ -1685,17 +1685,17 @@
  online 
-%22
+'
  + userL
@@ -1766,15 +1766,15 @@
 l + 
-%22
+'
  EST.
-%22
+'
 );%0A%09

use hashColor