'use strict';

const _ = require('lodash');
const Script = require('smooch-bot').Script;

const scriptRules = require('./script.json');

module.exports = new Script({
    processing: {
        //prompt: (bot) => bot.say('Beep boop...'),
        receive: () => 'processing'
    },

    start: {
        receive: (bot) => {
            return bot.say('So you want to learn about Esther? Just say HELLO to get started.')
                .then(() => 'speak');
        }
    },

    speak: {
        receive: (bot, message) => {

            let upperText = message.text.trim().toUpperCase();

            function updateSilent() {
                switch (upperText) {
                    case "CONNECT ME":
                        return bot.setProp("silent", true);
                    case "DISCONNECT":
                        return bot.setProp("silent", false);
                    default:
                        return Promise.resolve();
                }
            }

            function getSilent() {
                return bot.getProp("silent");
            }

            function processMessage(isSilent) {
                if (isSilent) {
                    return Promise.resolve("speak");
                }

                if (!_.has(scriptRules, upperText)) {
                    return bot.say(`I didn't understand that.`).then(() => 'speak');
                }

                var response = scriptRules[upperText];
                var lines = response.split('\n');

                var p = Promise.resolve();
                _.each(lines, function(line) {
                    line = line.trim();
                    if (!line.startsWith("<")) {
                        p = p.then(function() {
                            return bot.say(line);
                        });
                    } else {
                        // p = p.then(function() {
                        //     var start = line.indexOf("'") + 1;
                        //     var end = line.lastIndexOf("'");
                        //     var imageFile = line.substring(start, end);
                        //     return bot.sendImage(imageFile);
                        // });
                    }
                })

                return p.then(() => 'speak');
            }

            return updateSilent()
                .then(getSilent)
                .then(processMessage);
        }
    }
});
@@ -1634,445 +1634,72 @@
     
-if (!line.startsWith(%22%3C%22)) %7B%0A                        p = p.then(function() %7B%0A                            return bot.say(line);%0A                        %7D);%0A                    %7D else %7B%0A                        // p = p.then(function() %7B%0A                        //     var start = line.indexOf(%22'%22) + 1;%0A                        //     var end = line.lastIndexOf(%22'%22);%0A                        //     var imageFile = line.substring(start, end
+p = p.then(function() %7B%0A                        console.log(line
 );%0A 
@@ -1716,23 +1716,16 @@
         
-     //
      ret
@@ -1737,25 +1737,14 @@
 ot.s
-endImage(imageFil
+ay(lin
 e);%0A
@@ -1767,40 +1767,11 @@
     
-  
-  // %7D);%0A                    %7D
+%7D);
 %0A   

test arrays