//
// # HoopMatchups Server
//
// * serves requests
// * is a parent to & directs socket connections to homemanager and gamemanager
// * 
// 
//

var http = require('http');
var path = require('path');

var async = require('async');
var socketio = require('socket.io');
var express = require('express');

var DBConnector = require("./modules/DBConnector.js");
var Player = require("./modules/Player.js");
var HomeManager = require("./modules/HomeManager.js");
var FakeSI = require("./modules/FakeSI.js");
var GameManager = require("./modules/GameManager.js");


//
// ## SimpleServer `SimpleServer(obj)`
//
// Creates a new instance of SimpleServer with the following options:
//  * `port` - The HTTP port to listen on. If `process.env.PORT` is set, _it overrides this value_.
//
var router = express();
var server = http.createServer(router);
var io = socketio.listen(server);
console.log("environment: ", router.settings.env);

router.use(express.static(__dirname + '/public'));
router.set('view engine', 'jade');
router.set('views', './public/views');

router.get('/', function (req, res) {
  res.render('index');
});

router.get('/game/:gameid', function(req, res) {
  var gid = req.params.gameid;
  res.render('game', {'gameid': gid, 'livegame': "livegame rendering data here"});
});


var unidentified_sockets = [];
var dbConnector = new DBConnector(router.settings.env);
var homeManager = new HomeManager();
var fakeSI = new FakeSI();

var gameManager = new GameManager(fakeSI, dbConnector);

io.on('connection', function (socket) {
  console.log("socket connected");
  unidentified_sockets.push(socket);

  socket.on('disconnect', function () {
    console.log("socket disconnected", socket.identification);
    if (!socket.identification || !socket.identification.type){
      unidentified_sockets.splice(unidentified_sockets.indexOf(socket));
    }
    else if (socket.identification.type == "home") {
      homeManager.removeSocket(socket);
    }
    else if (socket.identification.type == 'game') {
      gameManager.removeSocket(socket);
    }
  });

  socket.on('identify', function (identification) {
    console.log("socket identified: ", identification);
    socket.identification = identification;
    
    if (identification.type == "game") {
      unidentified_sockets.splice(unidentified_sockets.indexOf(socket), 1);
      gameManager.addSocket(socket, identification.gameid);
    }
    else if (identification.type == "home") {
      unidentified_sockets.splice(unidentified_sockets.indexOf(socket), 1);
      homeManager.addSocket(socket);
    }
  });
  
});

server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
  var addr = server.address();
  console.log("HoopMatchups server listening at", addr.address + ":" + addr.port);
});

gameManager.startPolling(250);

// SCHEDULE UDPATOR
// poll schedule changes
// forward changes to home manager to update home clients
// forward changes to game manager to prepare for games

// LIVE GAME UPDATOR
// knowledge of which games are live
// poll live game API
// package live updates to game clients
// continually 