var express = require('express');
var builder = require('botbuilder');
var app = express();


//=========================================================
// Bot Setup
//=========================================================

// Setup Express Server
app.listen(process.env.port || process.env.PORT || 3978, '::', () => {
    console.log('Server Up');
});
// Create chat bot
var connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
app.post('/api/messages', [ connector.listen(), function(req, res, next) {
    console.log(req);
    console.log(res);
}] );

// Create endpoint for agent / call center
app.use('/agent', express.static('public'));
// app.post('/agent/:args', function(req, res, next) {
//     console.log('requests');
//     // connector.listen();
//     console.log(req);
//     console.log(res);
// });

var connectorQueue = function(deets) {
    // if deets don't match mine, return them?? then send msgs that way??
    console.log(deets);
};

//=========================================================
// Bots Dialogs
//=========================================================

var emergencies = ["Health", "Crime", "Catastrophe"];

bot.dialog('/', [
    //welcome the user, ask the emergency
    function (session) {
        connectorQueue(session.message.address);
        // console.log(session.message.address.id);
        // console.log(session.message.address.user);
        // console.log(session.message.address.user.address);
        
        // console.log(session.message.address.conversation);
        // console.log(session.message.address.bot);
        // console.log(session.message.address);
        builder.Prompts.choice(session, "What's the emergency?", emergencies);
    },
    //work with selected emergency
    function (session, results) {
        session.userData.emergency = results.response.entity;
        switch (session.userData.emergency) {
            case emergencies[0]:
                session.send(emergencies[0]);
                session.replaceDialog('/Health');
                break;
            case emergencies[1]:
                session.send(emergencies[1]);
                break;
            case emergencies[2]:
                session.send(emergencies[2]);
                break;
            default:
        }
    }
]);

//health conversation
bot.dialog('/Health', [
    function (session) {
        builder.Prompts.text(session, "What type of pain are you experiencing");
    },
    //figure out the type of emergency. Later use LUIS to get the emergency
    function (session, results) {
        if (results.response.includes("chest")) {
            session.userData.painType = "Chest Pain";
            builder.Prompts.choice(session, "How severe is the pain?", ["Mild", "Sharp", "Severe"]);
        } else {
            builder.Prompts.text(session, "I can only help diagnosing chestpain & headache");
            session.endConversation();
        }
    },
    function (session, results) {
        session.userData.painLevel = results.response.entity;
        switch (session.userData.painLevel) {
            case "Mild":
            case "Sharp":
                session.send("Fetching your heartrate");
                session.endConversation();

                break;
            case "Severe":
                session.send("Connecting to a Professional");
                session.endConversation();

                break;
            default:
                session.endConversation();

            //no default case required as the framework handles invalid inputs
            //and prompts the user to enter a valid input
        }
    }
]);

@@ -584,17 +584,16 @@
 ages', %5B
- 
 connecto
@@ -604,32 +604,33 @@
 sten(), function
+ 
 (req, res, next)
@@ -678,17 +678,16 @@
 res);%0A%7D%5D
- 
 );%0A%0A// C
@@ -974,16 +974,17 @@
 function
+ 
 (deets) 
@@ -1059,16 +1059,38 @@
 t way??%0A
+    var checkIn = %5B%5D;%0A
     cons
@@ -1105,16 +1105,139 @@
 deets);%0A
+    checkIn.push(deets);%0A    if (checkIn.length %3E= 2) %7B%0A        return checkIn%5B0%5D;%0A    %7D else %7B%0A        return null;%0A    %7D%0A
 %7D;%0A%0A//==
@@ -1521,46 +1521,129 @@
     
-connectorQueue(session.message.address
+session.privateConversationData.connect = connectorQueue(session.message.address);%0A        session.replaceDialog('/chats'
 );%0A 
@@ -1813,17 +1813,9 @@
 s);%0A
-        
 %0A
+
     
@@ -2630,21 +2630,390 @@
 %7D%0A    %7D%0A
-
 %5D);%0A%0A
+bot.dialog('/chats', %5B%0A    function (session, args, next) %7B%0A        if (session.message.text !== 'break') %7B%0A            bot.send(%0A                new builder.Message()%0A                    .text(session.message.text)%0A                    .address(session.privateConversationData.connect));%0A        %7D else %7B%0A            session.endConversation('bye');%0A        %7D%0A    %7D%0A%5D)%0A%0A
 //health

add more logic