/*jslint node: true, vars: true, indent: 4 */
'use strict';

var util = require('util'),
    Imap = require('imap'),
    MailParser = require('mailparser').MailParser,
    EventEmitter = require('events').EventEmitter;


function Notifier(opts) {
    EventEmitter.call(this);
    var self = this;
    self.options = opts;
    if (self.options.username) { //backward compat
        self.options.user = self.options.username;
    }
    self.connected = false;
    self.imap = new Imap(opts);
    self.imap.on('end', function () {
        self.connected = false;
        self.emit('end');
    });
    self.imap.on('error', function (err) {
        self.emit('error', err);
    });
}
util.inherits(Notifier, EventEmitter);

module.exports = function (opts) {
    return new Notifier(opts);
};


Notifier.prototype.start = function () {
    var self = this;
    self.imap.once('ready', function () {
        self.connected = true;
        self.imap.openBox(self.options.box || 'INBOX', false, function () {
            self.scan();
        });
        self.imap.on('mail', function (id) {
            self.scan();
        });
    });
    self.imap.connect();
    return this;
};

Notifier.prototype.scan = function () {
    var self = this;
    self.imap.search(self.options.search || ['UNSEEN'], function (err, seachResults) {
        if (err) {
            self.emit('error', err);
        }
        if (!seachResults || seachResults.length === 0) {
            util.log('no new mail in INBOX');
            return;
        }
        var fetch = self.imap.fetch(seachResults, {
            markSeen: self.options.markSeen !== false,
            bodies: ''
        });
        fetch.on('message', function (msg) {
            var mp = new MailParser();
            mp.once('end', function (mail) {
                self.emit('mail', mail);
            });
            msg.on('body', function (stream, info) {
                stream.on('data', function (chunk) {
                    mp.write(chunk);
                });
                stream.once('end', function () {
                    mp.end();
                });
            });
        });
        fetch.once('end', function () {
            util.log('Done fetching all messages!');
        });
        fetch.on('error', function () {
            self.emit('error', err);
        });
    });
    return this;
};

Notifier.prototype.stop = function () {
    if (this.connected) {
        this.imap.end();
    }
    return this;
};

@@ -1020,32 +1020,24 @@
 scan();%0A    
-    %7D);%0A
         self
@@ -1073,32 +1073,36 @@
 ) %7B%0A            
+    
 self.scan();%0A   
@@ -1090,32 +1090,48 @@
    self.scan();%0A
+            %7D);%0A
         %7D);%0A    
@@ -1874,16 +1874,18 @@
   msg.on
+ce
 ('body',
@@ -1938,191 +1938,15 @@
 eam.
-on('data', function (chunk) %7B%0A                    mp.write(chunk);%0A                %7D);%0A                stream.once('end', function () %7B%0A                    mp.end();%0A                %7D
+pipe(mp
 );%0A 

fix #17 : fix mail event sent twice, add mail listener on imap connection when the mail box is opened.