'use strict';

var http = require('http'),
  socketio = require('socket.io'),
  mongoose = require('mongoose'),
  ObjectId = mongoose.Types.ObjectId,
  MomThemeColor = mongoose.model('MomThemeColor');

module.exports = function(app) {
  var server = http.createServer(app);
  var io = socketio(server);

  var generateThemeColor = function() {
    var hue = parseInt(Math.random() * 360);
    var saturation = parseInt(Math.random() * 75 + 25) + '%';
    var lightness = parseInt(Math.random() * 35 + 40) + '%';
    return 'hsl(' + hue + ',' + saturation + ',' + lightness + ')' ;
  };

  var attendeeList = [];
  io.on('connection', function (socket) {
    var name = socket.handshake.query.name;
    var id = socket.handshake.query.id;
    var momId = socket.handshake.query.momId;
    var isNewAttendee = true;
    attendeeList.every(function(attendee, index) {
      if (attendee.name === name) {
        isNewAttendee = false;
        return false;
      } else {
        return true;
      }
    });
    if (isNewAttendee) {
      MomThemeColor.findOne( { 'attendee': new ObjectId(id), 'mom': new ObjectId(momId) } , function(err, mtc) {
        if (err) {
          return console.log(err);
        }
        if (mtc) {
          attendeeList.push({ 'name': name, 'id': id, 'themeColor': mtc._doc.themeColor });
          io.emit('attendee logined', { 'attendeeList': attendeeList });
        } else {
          var color = generateThemeColor();
          var momThemeColor = new MomThemeColor({
            'attendee': new ObjectId(id),
            'mom': new ObjectId(momId),
            'themeColor': color
          });
          momThemeColor.save(function(err) {
            if (err) {
              return console.log(err);
            }
            attendeeList.push({ 'name': name, 'id': id, 'themeColor': color });
            io.emit('attendee logined', { 'attendeeList': attendeeList });
          });
        }
      });
    }
    

    socket.on('disconnect', function() {

      var name = socket.handshake.query.name;
      var logoutAttendeeIndex = 0;
      attendeeList.some(function(attendee, index) {
        if (attendee.name === name) {
          logoutAttendeeIndex = index;
          return true;
        } else {
          return false;
        }
      });
      attendeeList.splice(logoutAttendeeIndex, 1);
      socket.broadcast.emit('attendee logouted', { 'attendeeName': name });
    });

  });

  return server;
};
@@ -1020,24 +1020,101 @@
 Attendee) %7B%0A
+      // check if current user loggined the current miutes of meeting before%0A
       MomThe
@@ -1292,24 +1292,94 @@
   if (mtc) %7B
+// if it's first time, we genetate a theme color automatically for him
 %0A          a
@@ -1964,32 +1964,170 @@
 lor': color %7D);%0A
+            // emit all the attendees' info to client side because all the client side need to%0A            // know the full attendee list%0A
             io.e
@@ -2230,17 +2230,16 @@
  %7D%0A    %0A
-%0A
     sock
@@ -2274,55 +2274,8 @@
 () %7B
-%0A%0A      var name = socket.handshake.query.name;
 %0A   

clean up js code of mom.server.socket.js