var express = require('express');
var request = require('request');
var bodyParser = require('body-parser');
var watson = require('watson-developer-cloud');
var app = express();

var credentialsConstructor = require('./config/credentials.js');
//const usedCredHere = credentialsConstructor.getWorkspaceCredentials("conversation-simple-example");
const usedCredHere = credentialsConstructor.getWorkspaceCredentials("bluehack");

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

var conversation_id = "";
var w_conversation = watson.conversation({
    url: 'https://gateway.watsonplatform.net/conversation/api',
    username: usedCredHere.username,
    password: usedCredHere.password,
    version: 'v1',
    version_date: '2016-07-11'
});
var workspace = usedCredHere.WorkspaceID;

app.get('/webhook/', function (req, res) {
    if (req.query['hub.verify_token'] === 'tokenDeVerificacaoFacebook') {
        res.send(req.query['hub.challenge']);
    }
    res.send('Erro de validação no token.');
});

app.post('/webhook/', function (req, res) {
	var text = null;

    messaging_events = req.body.entry[0].messaging;
	for (i = 0; i < messaging_events.length; i++) {
        event = req.body.entry[0].messaging[i];
        sender = event.sender.id;

        if (event.message && event.message.text) {
			text = event.message.text;
		}else if (event.postback && !text) {
			text = event.postback.payload;
		}else{
			break;
		}

		var params = {
			input: text,
			context: {"conversation_id": conversation_id}
		}

		var payload = {
			workspace_id: workspace
		};

		if (params) {
			if (params.input) {
				params.input = params.input.replace("\n","");
				payload.input = { "text": params.input };
			}
			if (params.context) {
				payload.context = params.context;
			}
		}
		callWatson(payload, sender);
    }
    res.sendStatus(200);
});

function callWatson(payload, sender) {
	w_conversation.message(payload, function (err, convResults) {
        if (err) {
            return responseToRequest.send("Erro.");
        }

		if(convResults.context != null)
    	   conversation_id = convResults.context.conversation_id;
        if(convResults != null && convResults.output != null){
			var i = 0;
			while(i < convResults.output.text.length){
				sendMessage(sender, convResults.output.text[i++]);
			}
		}

    });
}

function sendMessage(sender, text_) {
	text_ = text_.substring(0, 319);
	messageData = {	text: text_ };

    request({
        url: 'https://graph.facebook.com/v2.6/me/messages',
        qs: { access_token: token },
        method: 'POST',
        json: {
            recipient: { id: sender },
            message: messageData,
        }
    }, function (error, response, body) {
        if (error) {
            console.log('Error sending message: ', error);
        } else if (response.body.error) {
            console.log('Error: ', response.body.error);
        }
    });
};

var token = "EAASR0eAjHecBAPVDXOJXDkKV3BiUOcQ0FNJuyQBkNyyDZBN55xCgadAqbzUbg6wemQIKhAT9eBqlApodgjLSMGSx6X7V5EkgqynAaWO8S4YIOjjy9YGuqvpNMUbCdgJ4cWE5bFK6M16iuGh49kn0bQqg01dziZA0NykvYINAZDZD";
var host = (process.env.VCAP_APP_HOST || 'localhost');
var port = (process.env.VCAP_APP_PORT || 3000);
app.listen(port, host);

@@ -501,16 +501,44 @@
 son())%0A%0A
+var conversationJson = %7B%7D;%0A%0A
 var conv
@@ -1893,17 +1893,33 @@
 0);%0A%7D);%0A
+var end = false;
 %0A
-
 function
@@ -2131,74 +2131,770 @@
 ull)
+%7B
 %0A    %09
-   conversation_id = convResults.context.conversation_id;%0A  
+conversation_id = convResults.context.conversation_id;%0A%0A      if(convResults.context.action === 'marcar-medico')%7B%0A        conversationJson.medico = convResults.context.medico;%0A      %7D%0A      if(convResults.context.action === 'set-cpf')%7B%0A        conversationJson.cpf = convResults.context.cpf;%0A        end = true;%0A      %7D%0A      if(convResults.context.action === 'set-bairro')%7B%0A        conversationJson.bairro = convResults.context.bairro;%0A      %7D%0A      if(convResults.context.action === 'set-inicio')%7B%0A        conversationJson.data = convResults.context.data;%0A      %7D%0A      if(convResults.context.action === 'set-plano')%7B%0A        conversationJson.plano = convResults.context.plano;%0A      %7D%0A      %0A      console.log(%22conversationJson: %22, conversationJson);%0A%0A
     
@@ -2950,16 +2950,18 @@
  null)%7B%0A
+  
 %09%09%09var i
@@ -2966,16 +2966,18 @@
  i = 0;%0A
+  
 %09%09%09while
@@ -3014,16 +3014,18 @@
 ength)%7B%0A
+  
 %09%09%09%09send
@@ -3072,18 +3072,131 @@
 i++%5D
+ + %22%5Cn%22 + JSON.stringify(conversationJson) 
 );%0A
+  
 %09%09%09%7D%0A
-%09%09
+        if(end)%7B%0A          conversationJson = %7B%7D;%0A        %7D%0A%09%09  %7D%0A    
 %7D%0A%0A 

producing a json