"use strict";

const elasticsearch = require('elasticsearch');
const esClient = new elasticsearch.Client({
  host: 'localhost:9200'
});

const INDEX_NAME = 'bigbrother';

module.exports = {
  saveMessage(message) {
    return new Promise((resolve, reject) => {
      esClient.index({
        index: INDEX_NAME,
        type: 'message',
        body: message
      }, (error, response) => {
        if (error) {
          return reject(error);
        }
        resolve(response);
      });
    });
  },

  globalSearch(query, limit) {
    limit = limit || 10;
    return new Promise((resolve, reject) => {
      esClient.search({
        index: INDEX_NAME,
        q: query,
        size: limit
      }, (error, response) => {
        if (error) {
          return reject(error);
        }
        resolve(response.hits);
      });
    });
  },

  buildDialogFromMessageId(messageId) {
    return new Promise((resolve, reject) => {
      esClient.search({
        index: INDEX_NAME,
        q: 'frites'
      }, (error, response) => {
        if (error) {
          return reject(error);
        }
        resolve(response.hits);
      });
    });
  },

  searchTerm(term) {
    return esClient.search({
      index: INDEX_NAME,
      q: term,
      size: 10
    }).then(resp => {
      var hits = resp.hits.hits;
      if (hits.length === 0) {
        return `Désolé aucun résultat ne correspond à votre recherche "${term}"`;
      } else {
        var nbResults = resp.hits.total;
        if (nbResults > 10) {
          var content = `Voici les 10 premiers résultats de votre recherche "${term}" :<br/>`;
        } else {
          var content = `Voici les ${nbResults} résultats de votre recherche "${term}" :<br/>`;
        }

        hits = hits.map(hit => {
          var message = hit._source.message.replace(term, '<b>' + term + '</b>');
          var date = new Date(hit._source.date);

          return `<td>${hit._source.author} <i>(@${hit._source.username})</i></td>
<td>${date.toUTCString()}</td>
<td>${message}</td>`;
        });
        content += `<table><tr>${hits.join('</tr><tr>')}</tr></table>`;
        return content;
      }
    }, err => {
      console.trace(err.message);
      return `Une erreur est survenue :( => ${err.message}`;
    });
  }
};

@@ -8,16 +8,50 @@
 rict%22;%0A%0A
+const moment = require('moment');%0A
 const el
@@ -86,24 +86,25 @@
 icsearch');%0A
+%0A
 const esClie
@@ -160,16 +160,52 @@
 t:9200'%0A
+  //host: 'tea-elasticsearch:9200'%0A%0A
 %7D);%0A%0Acon
@@ -238,27 +238,436 @@
 r';%0A
-%0Amodule.exports = %7B
+const MESSAGE_TYPE_NAME = 'message';%0A%0Amodule.exports = %7B%0A  getMessage(id) %7B%0A    return new Promise((resolve, reject) =%3E %7B%0A      esClient.get(%7B%0A        index: INDEX_NAME,%0A        type: MESSAGE_TYPE_NAME,%0A        id: id%0A      %7D, function (error, res) %7B%0A        if (error) %7B%0A          return reject(error);%0A        %7D%0A        res%5B'_source'%5D%5B'date'%5D = %222016-12-20T13:14:36.629890+00:00%22;%0A        resolve(res);%0A      %7D);%0A    %7D);%0A  %7D,%0A
 %0A  s
@@ -798,25 +798,33 @@
   type: 
-'message'
+MESSAGE_TYPE_NAME
 ,%0A      
@@ -1382,143 +1382,347 @@
 urn 
-new Promise((resolve, reject) =%3E %7B%0A      esClient.search(%7B%0A        index: INDEX_NAME,%0A        q: 'frites'%0A      %7D, (error, response) =%3E
+this.getMessage(messageId).then(message =%3E %7B%0A      const messageDate = moment(message.date);%0A      return esClient.search(%7B%0A        index: INDEX_NAME,%0A        query: %7B%0A          %22bool%22: %7B%0A            %22must%22: %5B%0A              %7B%0A                %22term%22: %7B%0A                  %22room%22: message.room%0A                %7D%0A              %7D,%0A             
  %7B%0A 
@@ -1732,52 +1732,134 @@
     
+ 
-if (error) %7B%0A          return reject(error);
+       %22range%22 : %7B%0A                  %22date%22 : %7B%0A                    %22gte%22 : messageDate.subtract(2, 'minutes').toISOString(),
 %0A   
@@ -1859,34 +1859,36 @@
 (),%0A        
-%7D%0A
+    
         
 resolve(resp
@@ -1879,47 +1879,237 @@
     
-resolve(response.hits);%0A      %7D);%0A    %7D
+%22lt%22 : messageDate.add(4, 'minutes').toISOString(),%0A                    %22format%22: %22date_time%22%0A                  %7D%0A                %7D%0A              %7D%0A            %5D%0A          %7D%0A        %7D%0A      %7D);%0A    %7D).then(results =%3E results.hits
 );%0A 

build dialog