// Setup basic express server
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var socket = require('./mylibs.js')
var io = socket(server);
var port = process.env.PORT || 3000;


server.listen(port, function () {
  console.log('Server listening at port %d', port);
});

// Routing
app.use(express.static(__dirname + '/public'));

// Chatroom

// usernames which are currently connected to the chat
var usernames = {};
var numUsers = 0;

io.on('connection', function (socket) {
  var addedUser = false;

  // when the client emits 'new message', this listens and executes
  socket.on('new message', function (data) {
    // we tell the client to execute 'new message'
    socket.broadcast.emit('new message', {
      username: socket.username,
      message: data
    });
/* // be sure to add your twilio key before uncommenting below
// twilio is currently crashing, commented out error handling to get around the issue
    if (data.search(/:/) == 1){
      // setup twilio
      var twilio = require('twilio');
      var client = require('twilio')('xxx', 'xxx');
      data = data.replace(/.:/, '')
      client.sendSms({
         to: "+1xxxxxxxxxx",
         body: "m@ chat: ".concat(data),
         from: "+xxxxxxxxxxx"
      }, function(err, message) {
         if (!error) {
           console.log('Success!');
         } else {
           console.log('Failure!');
         }
      });
      // console.log(data);
    }
*/
  });

  // when the client emits 'add user', this listens and executes
  socket.on('add user', function (username) {
    // we store the username in the socket session for this client
    socket.username = username;
    // add the client's username to the global list
    usernames[username] = username;
    ++numUsers;
    addedUser = true;
    socket.emit('login', {
      numUsers: numUsers
    });
    // echo globally (all clients) that a person has connected
    socket.broadcast.emit('user joined', {
      username: socket.username,
      numUsers: numUsers
    });
  });

  // when the client emits 'typing', we broadcast it to others
  socket.on('typing', function () {
    socket.broadcast.emit('typing', {
      username: socket.username
    });
  });

  // when the client emits 'stop typing', we broadcast it to others
  socket.on('stop typing', function () {
    socket.broadcast.emit('stop typing', {
      username: socket.username
    });
  });

  // when the user disconnects.. perform this
  socket.on('disconnect', function () {
    // remove the username from global usernames list
    if (addedUser) {
      delete usernames[socket.username];
      --numUsers;

      // echo globally that this client has left
      socket.broadcast.emit('user left', {
        username: socket.username,
        numUsers: numUsers
      });
    }
  });
});
