var   assert 			= require("assert")
	, chai 				= require("chai")
	, events 			= require('events')
	;

var app = {
	eventEmitter: new events.EventEmitter()
};
var userMessages 		= require("../../lib/userMessages.js")(app);
	
describe('Adding a message with event', function(){
	var session;
	before(function(done){
		session = {};
		done();
	});

	after(function(done){
		delete session;
		done();
	});

	beforeEach(function(){
 
	});

	afterEach(function(){

	});
	
 	it('should increase the size of messages', function(done){
    	chai.expect(userMessages.size(session)).to.equal(0);
    	app.eventEmitter.emit('usermessages-add', session, 'This is a event message.', function(err, message){
    		chai.expect(userMessages.size(session)).to.equal(1);
    		chai.expect(err).to.equal(null);
    		chai.expect(message).to.have.property('text').is.equal('This is a event message.');
    	});
    	done();
 	});
 	it('should have a unreed messages', function(){
	 	chai.expect(userMessages.sizeUnread(session)).to.equal(1);
 	});
 	
 	it('should allow to mark it as reed', function(done){
    	app.eventEmitter.emit('usermessages-mark', session, 'This is a event message.', function(err, message){
    		chai.expect(userMessages.sizeUnread(session)).to.equal(0);
    		chai.expect(err).to.equal(null);
    		chai.expect(message).to.have.property('read').is.equal(true);
    	});
    	done();
 	});
 	it('should not have a unreed messages anymore', function(){
	 	chai.expect(userMessages.sizeUnread(session)).to.equal(0);
 	});
});

describe('Processing all unread message', function(){
	var session;
	before(function(done){
		session = {};
		userMessages.add(session, 'Message #1');
		userMessages.add(session, 'Message #2');
		userMessages.add(session, 'Message #3');
		userMessages.add(session, 'Message #4');
		userMessages.add(session, 'Message #5');
		done();
	});

	after(function(done){
		delete session;
		done();
	});

	beforeEach(function(){
 
	});

	afterEach(function(){

	});
	
 	it('should get an array of 4', function(done){
	 	userMessages.markAsRead(session, 'Message #4');
    	app.eventEmitter.emit('usermessages-process', session, function(err, messages){
    		assert.equal(4, messages.length );
    		chai.expect(err).to.equal(null);
    	});
    	done();
 	});
});

describe('For each unread message in the session', function(){
	var session;
	before(function(done){
		session = {};
		userMessages.add(session, 'Message #1');
		userMessages.add(session, 'Message #2');
		userMessages.add(session, 'Message #3');
		userMessages.add(session, 'Message #4');
		userMessages.add(session, 'Message #5');
		done();
	});

	after(function(done){
		delete session;
		done();
	});

	beforeEach(function(){
 
	});

	afterEach(function(){

	});
	
 	it('it should call the callback function.', function(done){
 		var cnt = 0;
 		userMessages.markAsRead(session, 'Message #4');
    	app.eventEmitter.emit('usermessages-foreach', session, function(err, message){
    		chai.expect(message).to.have.property('text').with.length(10);
    		chai.expect(message).to.have.property('read').to.equal(false);
    		cnt++;
    	});
    	assert.equal(4, cnt);
    	done();
 	});
 	
 	it('it should remove the message.', function(done){
    	app.eventEmitter.emit('usermessages-remove', session, 'Message #5', function(err, message){
    		chai.expect(message).to.have.property('text').with.length(10);
    	});
    	done();
 	});
 	
	it('it should find the message.', function(done){
    	app.eventEmitter.emit('usermessages-find', session, 'Message #1', function(err, message){
    		chai.expect(message).to.have.property('text').with.length(10);
    	});
    	done();
 	});
});
