var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var chatters = 0;

app.get('/', function(req, res){
  res.sendFile('/var/www/chat/index.html');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

io.on('connection', function(socket){
  	socket.nickname = "MSNLover" + chatters;
  	chatters++;
   	console.log('user: ' + socket.id + " connected and is called " + socket.nickname + ". IP: " + socket.handshake.address);
   	socket.emit('greeting', 'Hey there, ' + socket.nickname + '. Welcome to this nostalgia trip! You can change your nick with /nick nick');
    socket.emit('alert', 'chatters:' + chatters);
    socket.emit('alert', 'nick:' + socket.nickname);

  	socket.on('disconnect', function(){
      chatters--;
      socket.emit('alert', 'chatters:' + chatters);
      console.log(socket.nickname + ' disconnected');
    });

socket.on('chat message', function(msg){ 
		var msg = msg.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
   			return '&#'+i.charCodeAt(0)+';';
		});
		console.log(socket.nickname);
  		console.log(msg);
  		console.log("the split: " + msg.split(" ")[0]);
  		var firstWord = msg.split(" ")[0];
  		if(isCommand(firstWord))
  		{
  			switch(firstWord)
  			{
  				case '/nick':
  					var newNick = msg.split(" ")[1];
  					changeNick(socket, newNick);
  					break;
  			}
  		}

  		else
  		{
  			if(msg != "" && msg != " ")
  			{
  				msg = socket.nickname + ": " + msg;
				io.emit('chat message', msg);
				console.log('message: ' + msg);
			}
		}
	});
});

function isCommand(string)
{
	var cmds = ["/nick", "/msg"];
	var bool = false;
	if( cmds.indexOf(string) != -1 )
	{
		bool = true;
	}
	return bool;
}

function changeNick(socket, nick)
{
	var clients = findClientsSocket();
	for (var i in clients) 
	{
		console.log("searching for: " + socket.id + " found: " + clients[i].id);
		if(clients[i].id == socket.id)
		{
			if(!nickTaken(nick) && nick.length > 2 && nick.length < 20)
			{
        		clients[i].nickname = nick;
            clients[i].emit('alert', 'nick:' + nick);
        		console.log(clients[i].id + "is now: " + clients[i].nickname);
        	}
        	else
        	{
        	console.log("nick taken");
        	}
        }
    }
}

function nickTaken(nick)
{
	var bool = false;
	var clients = findClientsSocket();
	for(var i in clients)
	{
		if(clients[i].nickname == nick)
		{
			console.log(clients[i].nickname + " is same as " + nick);
			bool = true;
		}
	}
	return bool;
}

function findClientsSocket(roomId, namespace) {
    var res = [];
    var ns = io.of(namespace ||"/");    // the default namespace is "/"

    if(ns) 
    {
        for (var i in ns.connected) 
        {
            res.push(ns.connected[i]);
        }
    }
    return res;
}


@@ -1070,62 +1070,8 @@
 %7D);%0A
-%09%09console.log(socket.nickname);%0A  %09%09console.log(msg);%0A
   %09%09
@@ -1135,32 +1135,28 @@
 irst
-Word
+Char
  = msg.
-split(%22 %22)%5B0%5D
+charAt(0)
 ;%0A  
@@ -1175,20 +1175,20 @@
 nd(first
-Word
+Char
 ))%0A  %09%09%7B
@@ -1184,24 +1184,61 @@
 har))%0A  %09%09%7B%0A
+        var cmd = msg.split(%22 %22)%5B0%5D;%0A
   %09%09%09switch(
@@ -1241,16 +1241,10 @@
 tch(
-firstWor
+cm
 d)%0A 
@@ -1311,21 +1311,79 @@
 )%5B1%5D;%0A  
-%09%09%09%09%09
+          if(newNick != undefined)%0A            %7B%0A              
 changeNi
@@ -1403,16 +1403,30 @@
 wNick);%0A
+            %7D%0A
   %09%09%09%09%09b
@@ -1596,22 +1596,8 @@
 log(
-'message: ' + 
 msg)
@@ -1665,20 +1665,13 @@
  %5B%22/
-nick%22, %22/msg
+%22, %22!
 %22%5D;%0A

fix for commands