'use strict';

var responseFactory = require('../services/responseFactory').getInstance();
var facebookApi = require('../services/facebookService').getInstance();

function WebHookService() {
}

WebHookService.prototype = {
    verify: function (req) {
        var token = req.query['hub.verify_token'];
        var challenge = req.query['hub.challenge'];

        if (token === process.env.VERIFY_TOKEN) {
            return challenge;
        }

        return "Invalid token received!";
    },
    handle: function (req) {
        var data = req.body;
        if (data.object == 'page') {
            data.entry.forEach(function (pageEntry) {
                var pageID = pageEntry.id;
                var timeOfEvent = pageEntry.time;

                // Iterate over each messaging event
                pageEntry.messaging.forEach(function (messagingEvent) {
                    var senderId = messagingEvent.sender.id;
                    if (messagingEvent.optin) {
                        //receivedAuthentication(messagingEvent);
                    } else if (messagingEvent.message) {
                        console.log('Text: ', messagingEvent.message.text);
                        var msg = responseFactory.getResponse(messagingEvent.message.text);
                        if (postBackMsg !== undefined) {
                            facebookApi.sendMessage(senderId, msg);
                        }
                    } else if (messagingEvent.delivery) {
                        //receivedDeliveryConfirmation(messagingEvent);
                    } else if (messagingEvent.postback) {
                        var payload = messagingEvent.postback.payload;
                        var postBackMsg = responseFactory.getResponse(messagingEvent.postback.payload);
                        if (postBackMsg !== undefined) {
                            facebookApi.sendMessage(senderId, postBackMsg);
                        }
                    } else if (messagingEvent.read) {
                        //receivedMessageRead(messagingEvent);
                    } else if (messagingEvent.account_linking) {
                        //receivedAccountLink(messagingEvent);
                    } else {
                        console.log("Webhook received unknown messagingEvent: ", messagingEvent);
                    }
                });
            });
        }
    }

};

/**
 * @returns {WebHookService}
 */
module.exports.getInstance = function () {
    return new WebHookService();
};

@@ -548,16 +548,44 @@
 q.body;%0A
+        console.info(body);%0A
         

add product reponse