// App
var express = require('express');

// Sessions
var session = require('express-session');
// var cookieParser = require('cookie-parser');

// Handles auth signin and signout
var passport = require('passport');

// Flash to send messages to client
var flash = require('connect-flash');

// Body parser parses xhr request data to req.body for easy usage
var bodyParser = require('body-parser');

// Use Path to locate static assets and config files
var path = require('path');

// ENV
var env = process.env.NODE_ENV;


// Set PORT based on environment Production or local
var PORT = process.env.PORT || 3000;

// SESSION OPTIONS under SESSION managment
var sessionOptions = {
  secret: 'surehopethecanadianslikethis',
  resave: false,
  saveUninitialized: false,
  maxAge: 5000
};

// Initialize EXPRESS APP -- Was going to use basic http routing but Express is faster to setup --
var app = express();

// Import and run PASSPORT CONFIG
require('./server/config/passport')(passport);

// ## DEV ##

if(env === 'development') {
  // Asset compiler and middleware for Express compatability
  // For Route Logging
  var morgan = require('morgan');
  var webpack = require('webpack');
  var webpackHotMiddleware = require('webpack-hot-middleware');
  var webpackDevMiddleware = require('webpack-dev-middleware');
  var webpackConfig = require('./webpack.dev.config');

  var compiler = webpack(webpackConfig);

  app.use(webpackHotMiddleware(compiler));

  app.use(webpackDevMiddleware(compiler, {
    publicPath: webpackConfig.output.publicPath,
    stats: { colors: true }
  }));

  // Log routing to console
  app.use(morgan('dev'));
} else {
  // ## PUBLIC STATIC folder client will read from in production ##

  // Set static directory to public which will be compiled by Webpack

  app.use(express.static(path.join(__dirname, 'public')));
}


// ## XHR managment ##

// Handle body parsing for xhr requests from app -- mostlikely Game data and such
app.use(bodyParser.json());

// If there were more config I need to pass to BODYPARSER I would move it to an easier to see variable
app.use(bodyParser.urlencoded({ extended: false }));

// ## VIEW ENGINE ##
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'server/views'));

// ## Session management ##

// Use cookie parser to parse cookise for express-session
// app.use(cookieParser());

// Set express-session secret with SESSION OPTIONS
app.use(session(sessionOptions));


// Initialize PASSPORT
app.use(passport.initialize());

// Take over session management from EXPRESS SESSION
app.use(passport.session());

// Flash for messages sent from server
app.use(flash())


// Here I run the exported function from the routing section of the server folder and pass it the EXPRESS ROUTER and the configured PASSPORT variable for authentication
require('./server/routes')(app, passport);

// ## SOCKETS ##

// SocketIo for WebSockets
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
// Games List
var gameList = []


io.on('connection', function(socket) {
  // console.log('Client -- %s -- connected to the server', socket.id);

  socket.on('make game', function() {
    var gameId = (Math.random()+1).toString(32).slice(2,18);

    var game = {
        gameId: gameId,
        playerOne: socket.id,
        playerTwo: null
    }

    gameList.push(game)

    io.emit('waiting for opponent')
  })

  socket.on('join game', function(playerId) {
    if(gameList.length) {
      for(var i=0; i<gameList.length;i++) {
        if(!gameList[i].playerTwo) {
          gameList.playerTwo = socket.id

          io.emit('start game', gameList[i])

          break;
        }
      }
    } else {
      socket.emit('make game')
    }
  })

  socket.on('disconnect', function() {
    io.sockets.in('room-'+roomNum).emit('playerDisconnect');
  })
})


// Start server listening on the PORT variable
server.listen(PORT);

@@ -3142,16 +3142,19 @@
 .id);%0A%0A 
+ //
  socket.
@@ -3184,18 +3184,21 @@
 n() %7B%0A  
+//
   
+ 
 var game
@@ -3248,16 +3248,19 @@
 ,18);%0A%0A 
+ //
    var g
@@ -3264,24 +3264,27 @@
 r game = %7B%0A 
+ //
        gameI
@@ -3291,24 +3291,27 @@
 d: gameId,%0A 
+ //
        playe
@@ -3328,16 +3328,19 @@
 et.id,%0A 
+ //
        p
@@ -3360,14 +3360,20 @@
 l%0A  
+//
   
+ 
 %7D%0A%0A 
+ //
    g
@@ -3393,16 +3393,19 @@
 game)%0A%0A 
+ //
    io.em
@@ -3432,22 +3432,28 @@
 nent')%0A 
+ //
  %7D)%0A%0A 
+ //
  socket.
@@ -3486,24 +3486,27 @@
 layerId) %7B%0A 
+ //
    if(gameLi
@@ -3515,24 +3515,27 @@
 .length) %7B%0A 
+ //
      for(var
@@ -3562,24 +3562,27 @@
 gth;i++) %7B%0A 
+ //
        if(!g
@@ -3607,24 +3607,26 @@
 wo) %7B%0A  
+//
         
 gameList
@@ -3613,24 +3613,25 @@
   //        
+ 
 gameList.pla
@@ -3651,16 +3651,19 @@
 et.id%0A%0A 
+ //
         
@@ -3700,16 +3700,19 @@
 t%5Bi%5D)%0A%0A 
+ //
         
@@ -3720,16 +3720,19 @@
 break;%0A 
+ //
        %7D
@@ -3733,16 +3733,19 @@
      %7D%0A 
+ //
      %7D%0A 
@@ -3745,18 +3745,21 @@
     %7D%0A  
+//
   
+ 
 %7D else %7B
@@ -3760,16 +3760,19 @@
 else %7B%0A 
+ //
      soc
@@ -3798,20 +3798,29 @@
 ')%0A 
+ //
    %7D%0A 
+ //
  %7D)%0A%0A 
+ //
  soc
@@ -3854,16 +3854,19 @@
 on() %7B%0A 
+ //
    io.so
@@ -3918,16 +3918,19 @@
 ect');%0A 
+ //
  %7D)%0A%7D)%0A%0A

fix bug