/* -- Modules -- */
// Essential
var eventEmmiter = require('events').EventEmitter;
var util         = require("util");

// Core
var outbound = require('./outbound');

// Foundations
var schedule = require('node-schedule');
var colors = require('colors'); // Better looking error handling
var moment = require('moment');
var _ = require('lodash');
/* -- ------- -- */

colors.setTheme({
	silly: 'rainbow',
	input: 'grey',
	verbose: 'cyan',
	prompt: 'grey',
	success: 'green',
	data: 'grey',
	info: 'cyan',
	warn: 'yellow',
	debug: 'grey',
	bgWhite: 'bgWhite',
	bold: 'bold',
	error: 'red'
});

/* Initiate outbound queue. */
var Queue = function(port, callback){
	console.log("Queue created".success);
	eventEmmiter.call(this);
}

util.inherits(Queue, eventEmmiter);

var queueStart = function (databaseConnection) {
	console.log("Queue started".success);
	
	var maxConcurrent = 10;
	var outbox = databaseConnection.collections.queue;
	
	databaseConnection.collections.queue.count({state:'transit'}).exec(function (error, count){
		if(error) return console.log(colors.error(error));
		console.log(colors.info(count + " mails in transit"));
	  // Bit of a callback hell here
	  if(count <= maxConcurrent){
		  console.log(colors.success("Starting lookup"));
		  outbox.find().where({ or: [{ state: 'pending' }, { state: 'denied' }] }).limit(10).exec(function(error, models){
			  if(error) return console.log(colors.error(error));
			  
			  console.log(colors.info(models.length + " mails found"));
			  _.forEach(models, function(mail) {
					outbox.update({ eID: mail.eID }, { state: 'transit' }).exec(function(error, mail) {
						if(error) console.log(error.error);
						
						var mail = mail[0]; // Update returns and Array
						
						console.log(mail);
						
						var parsedMail = {
							from: mail.sender,
							to: mail.to,
							subject: mail.subject,
							text: mail.text,
							html: mail.html
						}
						
						var OUTBOUND = new outbound();
						OUTBOUND.send(parsedMail, function(error, response){
							if(error){
								console.log(response, parsedMail);
								console.log(colors.error(error));
								outbox.update({ eID: mail.eID }, { state: 'denied' }).exec(function(error, mail) {
									if(!error) console.log("Message ".error + parsedMail.subject + " denied".error);
								});
							}else{
								outbox.update({ eID: mail.eID }, { state: 'sent' }).exec(function(error, mail) {
									if(!error) console.log("Message ".success + parsedMail.subject + " sent".success);
								});
							}
						})
					});
			  });
		  });
	  }
	});
}

var queueAdd = function (databaseConnection, mail, options, callback) {
	var _this = this;
	
	// Humane programming
	if((options.constructor !== Object)&&(!callback)) callback = options;
	
	databaseConnection.collections.queue.create({
		sender: mail.from,
		to: mail.to,
		schedule: { attempted: moment().toISOString(), scheduled: moment().toISOString() },
		attempts: 0,
		subject: mail.subject,
		text: mail.text,
		html: mail.html,

		// STRING ENUM: ['pending', 'transit', 'sent', 'denied']
		state: 'pending'
	}, function(error, model){
		if(error) console.log(colors.error(error));
		
		console.log(colors.info(model));
		// Start queue
		queueStart(databaseConnection);
		
		_this.emit('queued', error, model, databaseConnection);
		if(callback) callback(error, model);
	});
};

Queue.prototype.start = queueStart;
Queue.prototype.add = queueAdd;

module.exports = Queue;

@@ -1735,40 +1735,8 @@
 %09%09%09%0A
-%09%09%09%09%09%09console.log(mail);%0A%09%09%09%09%09%09%0A
 %09%09%09%09
@@ -2012,51 +2012,8 @@
 r)%7B%0A
-%09%09%09%09%09%09%09%09console.log(response, parsedMail);%0A
 %09%09%09%09
@@ -3100,45 +3100,8 @@
 ));%0A
-%09%09%0A%09%09console.log(colors.info(model));
 %0A%09%09/

Fix update