'use strict';

process.env.DEBUG = 'actions-on-google:*';
let Assistant = require('actions-on-google').ApiAiAssistant;
let express = require('express');
let bodyParser = require('body-parser');

let app = express();
app.use(bodyParser.json({type: 'application/json'}));

const INTENT_WELCOME = "input.welcome";
const INTENT_CHECK_BALANCE = "check_balance";

app.post('/', function (req, res) {
  const assistant = new Assistant({request: req, response: res});
  console.log('Request headers: ' + JSON.stringify(req.headers));
  console.log('Request body: ' + JSON.stringify(req.body));

  function init (assistant) {
    assistant.data.cashMoney = 2000;
    assistant.data.bills = [
      { recepient: "Anime Mystery Box", cost: 50.00 },
      { recepient: "University of Waterloo Tuition Bill", cost: 8000.00 },
      { recepient: "Waterloo North Hydro", cost: 120.00 }
    ];
    assistant.ask('Sup fam, Budgetbot at your service.');
  }

  function checkBalance (assistant) {
    assistant.ask("<speak>Your account balance is  " +
              " <say-as interpret-as='cardinal'>" + assistant.data.cashMoney + "</say-as> " +
              "  dollars.</speak>");
  }

  let actionMap = new Map();
  actionMap.set(INTENT_WELCOME, init);
  actionMap.set(INTENT_CHECK_BALANCE, checkBalance);

  assistant.handleRequest(actionMap);
});

if (module === require.main) {
  let server = app.listen(process.env.PORT || 8080, function () {
    let port = server.address().port;
    console.log('App listening on port %s', port);
  });
}

module.exports = app;

@@ -995,15 +995,8 @@
 sk(%22
-%3Cspeak%3E
 Your
@@ -1019,61 +1019,8 @@
  is 
- %22 +%0A              %22 %3Csay-as interpret-as='cardinal'%3E
 %22 + 
@@ -1051,38 +1051,8 @@
  + %22
-%3C/say-as%3E %22 +%0A              %22 
  dol
@@ -1060,16 +1060,8 @@
 ars.
-%3C/speak%3E
 %22);%0A

Remove SSML to fix spacing