import * as Chatbot from '../src/chatbot/chatbot-service';
import dialog from '../src/coaching-chatbot/dialog';
import * as Messenger from '../src/facebook-messenger/messenger-service';
import * as Sessions from '../src/util/sessions-service';
import * as sinon from 'sinon';

export function generate(discussions, states) {
  const messengerSpy = sinon.spy(Messenger, "send");
  var clock = sinon.useFakeTimers(new Date().getTime());

  const sessions = new Sessions();
  const bot = new Chatbot(dialog, sessions);

  sessions.db.load(states);

  let lines = [];
  let promise = bot.receive('', '');

  let messageQueue = {};

  function addBotReply(output) {
    for (let botReply of output) {
      for (let message of botReply.message.replace('\n\n', '\n').split('\n')) {
        if (message.length > 0) {
          lines.push('| ' + message + ' | |');
        }
      }
      if (botReply.quickReplies.length > 0) {
        lines.push('| [' +
          botReply.quickReplies
            .map((quickReply) => quickReply.title)
            .join('] [') + '] | |');
      }
    }
  }

  for (let scenario in discussions) {
    const messages = discussions[scenario].messages;
    const sessionID = discussions[scenario].session;

    if (discussions[scenario].time) {
      promise = promise.then(() => {
        clock = sinon.useFakeTimers(new Date(discussions[scenario].time).getTime());
      });
    }

    if (discussions[scenario].hide) {
      for (let message of messages) {
        promise = promise.then(() => {
          return bot.receive(sessionID, message);
        });
      }
    } else {
      promise = promise.then(() => {
        lines.push('# ' + scenario);
        lines.push('| Kehitystön vertaisohjausrobotti | ' + discussions[scenario].player + ' |');
        lines.push('|-|-|');
      });

      promise = promise.then(() => {
        if (messageQueue[sessionID] != undefined) {
          addBotReply(messageQueue[sessionID]);
          messageQueue[sessionID] = undefined;
        }
      });

      for (let message of messages) {
        promise = promise.then(() => {
          lines.push('| | ' + message + ' |');
          return bot.receive(sessionID, message);
        }).then((output) => {
          addBotReply(output);
        });
      }
    }

    promise = promise.then(() => {
      if (messengerSpy.callCount > 0) {
        for (let call of messengerSpy.args) {
          if (!messageQueue[call[0]]) {
            messageQueue[call[0]] = [];
          }
          messageQueue[call[0]].push({
            message: call[1],
            quickReplies: call[2] || {}
          });
        }
        messengerSpy.reset();
        clock.reset();
      }
    });
  }

  return promise
    .then(() => messengerSpy.restore())
    .then(() => {
      let scenarios = [];
      for (let line of lines) {
        if (line[0] == '#') {
          scenarios.push({
            title: line,
            content: []
          });
        } else {
          scenarios[scenarios.length - 1].content.push(line);
        }
      }
      return scenarios;
    });
}

@@ -545,20 +545,24 @@
 %0A%0A  let 
-line
+scenario
 s = %5B%5D;%0A
@@ -625,16 +625,215 @@
  = %7B%7D;%0A%0A
+  function newTitle(title) %7B%0A    scenarios.push(%7B%0A      title: '# ' + title,%0A      content: %5B%5D%0A    %7D);%0A  %7D%0A%0A  function newContent(line) %7B%0A    scenarios%5Bscenarios.length - 1%5D.content.push(line);%0A  %7D%0A%0A
   functi
@@ -1012,34 +1012,34 @@
 %7B%0A          
-lines.push
+newContent
 ('%7C ' + mess
@@ -1121,34 +1121,34 @@
 ) %7B%0A        
-lines.push
+newContent
 ('%7C %5B' +%0A   
@@ -1852,26 +1852,17 @@
     
-lines.push('# ' + 
+newTitle(
 scen
@@ -1872,34 +1872,34 @@
 o);%0A        
-lines.push
+newContent
 ('%7C Kehityst
@@ -1974,26 +1974,26 @@
         
-lines.push
+newContent
 ('%7C-%7C-%7C'
@@ -2302,18 +2302,18 @@
     
-lines.push
+newContent
 ('%7C 
@@ -2970,314 +2970,17 @@
  =%3E 
-%7B%0A      let scenarios = %5B%5D;%0A      for (let line of lines) %7B%0A        if (line%5B0%5D == '#') %7B%0A          scenarios.push(%7B%0A            title: line,%0A            content: %5B%5D%0A          %7D);%0A        %7D else %7B%0A          scenarios%5Bscenarios.length - 1%5D.content.push(line);%0A        %7D%0A      %7D%0A      return scenarios;%0A    %7D
+scenarios
 );%0A%7D

Improve discussion generation code