var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var twitter = require('twitter');
var sediment = require('sediment');

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

server.listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});


app.get('/', function(request, response) {
  response.render('pages/index');
});


var client = new twitter({
  consumer_key: process.env.CONSUMER_KEY,
  consumer_secret: process.env.CONSUMER_SECRET,
  access_token_key: process.env.ACCESS_TOKEN_KEY,
  access_token_secret: process.env.ACCESS_TOKEN_SECRET
});


function findBoxCenter(box){
  point1 = box[0];
  point2 = box[1];
  point3 = box[2];
  midX = (point1[1] + point2[1]) / 2;
  midY = (point1[0] + point3[0]) / 2;
  return [midX, midY];
}

function colorizeBlueAttitude(attitude){
  if (attitude >= 0.5)
  {
    return '#62ceff'; //light blue
  }
  else if (attitude <= -0.5){
    return '#007ab1'; //dark blue
  }
  else {
    return '#00a2eb'; //normal blue
  }
}

function colorizeRedAttitude(attitude){
  if (attitude >= 0.5)
  {
    return '#8B0000'; //light red
  }
  else if (attitude <= -0.5){
    return '#DB7093'; //dark red
  }
  else {
    return '#FF0000'; //normal red
  }
}

function sendTweets(socket, tweet, color){
  if (tweet.coordinates === null) {
    if (tweet.place === null){
      socket.emit('geocoder', { location: tweet.user.location, tweet: tweet.text, color: color });
    }
    else{
      midPoint = findBoxCenter(tweet.place.bounding_box.coordinates[0]);
      socket.emit('tweet', {coordinates: midPoint, tweet: tweet.text, color: color });
    }
  }
  else{
    socket.emit('tweet', {coordinates: tweet.coordinates.coordinates, tweet: tweet.text, color: color });
  }
}

io.on('connection', function(socket){
  socket.on('search', function(data){
    var words = data.word.split(",");
    client.stream('statuses/filter', {track: data.word}, function(stream){
      socket.on('disconnect', function(){
        console.log("DESTROYED MWAHAHAHAHHAHHAHAHAHAH");
        stream.destroy();
      });

      socket.on('newSearch', function(){
        console.log("stream is closin...");
        // socket.emit('hideToast');
        stream.destroy();
      });

      stream.on('error', function(error){
        console.log(error);
        if(error instanceof TypeError) {
          console.error("SWALLOWING THE FOLLOWING ERROR! YOLO.");
          console.trace(error);
        } else {
          console.log(error);
          socket.emit('openModal');
        }
      });

      stream.on('data', function(tweet) {
        socket.emit('hideToast');
        var attitude = (sediment.analyze(tweet.text).score);
        var regexp = new RegExp(words[0],"i");
        if (words.length === 1) {
          console.log("Colorizing Blue!");
          var color = colorizeBlueAttitude(attitude);
        }
        else
        {
          if (tweet.text.match(regexp)){
            console.log("Colorizing Blue!");
            var color = colorizeBlueAttitude(attitude);
          }
          else {
            console.log("Colorizing Red!")
            var color = colorizeRedAttitude(attitude);
          }
        }
        sendTweets(socket, tweet, color);
      });

    });
  });
});



@@ -2908,296 +2908,270 @@
     
-var regexp = new RegExp(words%5B0%5D,%22i%22);%0A        if (words.length === 1) %7B%0A          console.log(%22Colorizing Blue!%22);%0A          var color = colorizeBlueAttitude(attitude);%0A        %7D%0A        else%0A        %7B%0A          if (tweet.text.match(regexp))%7B%0A            console.log(%22Colorizing Blue!%22);
+if (words.length === 1) %7B%0A          var color = colorizeBlueAttitude(attitude);%0A        %7D%0A        else%0A        %7B%0A          var firstWord = new RegExp(words%5B0%5D,%22i%22);%0A          var secondWord = new RegExp(words%5B1%5D, %22i%22);%0A          if (tweet.text.match(firstWord))%7B
 %0A   
@@ -3229,27 +3229,60 @@
 ;%0A          
-%7D%0A 
+  sendTweets(socket, tweet, color);%0A
          els
@@ -3278,22 +3278,18 @@
         
-else %7B
+ %7D
 %0A       
@@ -3295,40 +3295,48 @@
     
-  console.log(%22Colorizing Red!%22)
+else if (tweet.text.match(secondWord)) %7B
 %0A   
@@ -3395,26 +3395,8 @@
     
-      %7D%0A        %7D%0A
     
@@ -3429,24 +3429,85 @@
 et, color);%0A
+          %7D%0A          else%0A          %7B%0A          %7D%0A        %7D%0A
       %7D);%0A%0A 
@@ -3519,14 +3519,12 @@
 ;%0A  %7D);%0A%7D);%0A
-%0A%0A

Throw away tweets that do not specifically contain the word you are searching IF you are searching for multiple terms