"use strict";

var Backend = require('./backend'), pad = require('pad'), dateFormat = require('dateformat'), fs = require('fs');

var backend = new Backend(__dirname + '/../games.json');

// Test Logging
(function() {
  var maxLevelSize = 0;
  backend.getEmitter().on('logger.*', function(data) {
    if (data.level) {
      maxLevelSize = Math.max(maxLevelSize, data.level.length);
    }
    var prefix = '[' + dateFormat(data.timestamp) + ']' + ' ' + '[' + pad(data.level.toUpperCase(), maxLevelSize) + ']';
    if (data.message && typeof data.message === 'string') {
      console.log(prefix + ' - ' + data.message);
    } else {
      console.log(prefix, data.message);
    }
  });
})();

module.exports.routes = function(app) {
  app.get('/game*', function(req, res) {
    fs.readFile(
      __dirname + '/../static/game.html',
      function(err, data) {
        if (err) {
          res.writeHead(500);
          return res.end('Error loading game.html');
        }
        res.header('Content-Type', 'text/html');
        res.writeHead(200);
        res.end(data);
      }
    );
  });
};

module.exports.sockets = function(io) {
  backend.setSocketIO(io);
  io.sockets.on('connection', function(socket) {
    console.log('Client Connected');

    //socket.join(game.id);

    socket.on('message', function(data) {
      //socket.broadcast.emit('server_message', data);
      backend.dispatchMessage(socket, data);
    });

    socket.on('createGame', function(data) {
      backend.createGame(socket, data);
    });

    socket.on('joinGame', function(data) {
      backend.joinGame(socket, data);
    });

    socket.on('preMove', function(data) {
      backend.preMove(socket, data);
    });

    socket.on('move', function(data) {
      backend.move(socket, data);
    });

    socket.on('disconnect', function() {
      backend.deRegisterClient(socket);
    });

    //socket.broadcast.to(game.id).emit('server_message', {message: 'Player joined [' + game.id + ']', action: 'join'});
  });
};


@@ -732,22 +732,170 @@
 %7B%0A  
-app.get('/game
+var servedHtmlFiles = %5B'game'%5D;%0A  for (var i = 0, c = servedHtmlFiles.length; i %3C c; i++) %7B%0A    (function(servedHtmlFile) %7B%0A      app.get('/' + servedHtmlFile + '
 *', 
@@ -911,24 +911,28 @@
 req, res) %7B%0A
+    
     fs.readF
@@ -935,23 +935,16 @@
 eadFile(
-%0A      
 __dirnam
@@ -959,20 +959,38 @@
 /static/
-game
+' + servedHtmlFile + '
 .html',%0A
@@ -995,16 +995,20 @@
 ,%0A      
+  
+  
 function
@@ -1025,24 +1025,28 @@
 ) %7B%0A        
+    
 if (err) %7B%0A 
@@ -1046,32 +1046,36 @@
 rr) %7B%0A          
+   
+ 
 res.writeHead(50
@@ -1074,24 +1074,28 @@
 eHead(500);%0A
+    
           re
@@ -1122,20 +1122,38 @@
 loading 
-game
+' + servedHtmlFile + '
 .html');
@@ -1157,26 +1157,34 @@
 ');%0A        
-%7D%0A
+    %7D%0A    
         res.
@@ -1220,32 +1220,36 @@
 html');%0A        
+    
 res.writeHead(20
@@ -1256,24 +1256,28 @@
 0);%0A        
+    
 res.end(data
@@ -1289,22 +1289,66 @@
     
+ 
+   
 %7D%0A    
+ 
+   );%0A      %7D);%0A    %7D)(servedHtmlFiles%5Bi%5D
 );%0A  %7D
-);
 %0A%7D;%0A

Refactor routing to static html files.