'use strict';

var fs = require('fs');
var shellExec = require('child_process').exec;
var MailParser = require("mailparser").MailParser;
var Encoder = require('node-html-encoder').Encoder;
var AnyfetchClient = require('cluestr');
var async = require('async');

/**
 * HYDRATING FUNCTION
 *
 * @param {string} path Path of the specified file
 * @param {string} document to hydrate
 * @param {function} cb Callback, first parameter, is the error if any, then the processed data
 */

var reg = new RegExp("(<([^>]+)>)", "g");

function exists(item){
  return item ? item : '';
}

module.exports = function(path, document, cb) {
  if(!document.metadatas) {
    document.metadatas = {};
  }
  if(!document.datas) {
    document.datas = {};
  }

  var anyfetchClient = new AnyfetchClient();
  anyfetchClient.setAccessToken(document.access_token);

  var mailparser = new MailParser();
  var encoder = new Encoder('entity');
  mailparser.on("end", function(mail_object){
    document.document_type = "email";
    document.metadatas.to = [];
    (mail_object.to || []).forEach(function(item){
      document.metadatas.to.push(item);
    });
    document.metadatas.cc = [];
    (mail_object.cc || []).forEach(function(item){
      document.metadatas.cc.push(item);
    });
    document.metadatas.bcc = [];
    (mail_object.bcc || []).forEach(function(item){
      document.metadatas.bcc.push(item);
    });
    document.metadatas.from = exists(mail_object.from);
    document.metadatas.subject = exists(mail_object.subject);

    if (mail_object.html) {
      document.datas.html = encoder.htmlDecode(mail_object.html).trim();
    }

    if (mail_object.headers && mail_object.headers.date) {
      document.creation_date = new Date(mail_object.headers.date);
    }

    if (mail_object.text) {
      document.metadatas.text = mail_object.text;
    }
    else if (document.datas.html) {
      document.metadatas.text = document.datas.html.replace(reg, " ");
      document.metadatas.text = document.metadatas.text.replace("  ", " ");
    }
    document.metadatas.text = document.metadatas.text.trim();

    async.each((mail_object.attachments || []), function(attachment, cb){
      var docAttachment = {};
      docAttachment.identifier = document.identifier + "/" + attachment.fileName;
      docAttachment.document_type = "file";
      docAttachment.metadatas = {};
      // File to send
      var fileConfigAttachment = function(){
        return {
          file: attachment.content,
          filename: attachment.fileName,
          knownLength: attachment.length
        };
      };
      anyfetchClient.sendDocumentAndFile(docAttachment, fileConfigAttachment, function(err) {
        cb(err);
      });
    }, function(err){
      cb(err, document);
    });

  });

  fs.createReadStream(path).pipe(mailparser);
};
@@ -36,55 +36,8 @@
 ');%0A
-var shellExec = require('child_process').exec;%0A
 var 
@@ -2302,24 +2302,88 @@
 datas = %7B%7D;%0A
+      docAttachment.metadatas.path = %22/%22 + attachment.fileName;%0A
       // Fil

add attachment.metadatas.path