'use strict'

var db = require('../db/index.js');
var app = require.main.exports;

/**
 * Process query results for NPC quoutes from the database
 *
 * @var npc {string} - name of NPC
 * @var tag {string} - tag passed to command
 * @var message {object} - message object that sent the command
 *
 * @returns {object} - returns a message object to the chat
 */
 
var processNpcQuotes = function (npc, tag, message) {
    // Empty Message Body
    var results = [];

    // If no tags are passed, run findByNPC
    if (!tag) {
        // Return random quote for NPC from database

        // Set Message Header
        results.push("__**Random Quote for " + npc + "**__");

        // Run Query to find NPC and return results
        db.findByNPC(npc, function (cb) {

            // Return message if NPC is not in the database
            if (cb.length < 1) {
                results.push(npc + " is currently not in the database! Let "+app.bot.user+" know.");
            }else{

                // Pick a random number between 0 and the length of the results
                var num = Math.floor(Math.random() * (cb.length - 1));

                // Iterate over the results to retrieve the index from above.
                for (var i = 0; i < cb.length; i++) {
                    var quote = cb[num].quote;
                }

                // Add response to message body
                results.push(quote);
            }            

            // Send the message to chat
            app.bot.sendMessage(message, results);

            // reset results to empty array
            results = [];

        });                
    }else{
        /**
          * Return set of quotes for npc based on tag
          */

        // Set Message Header
        results.push("__**" + npc + " Quotes on the topic of `" + tag + "` : **__");
        
        db.findTagByNPC(npc, tag, function (cb) {
            
            // Check for results
            if (cb.length < 1) {
                results.push("Sorry, but the tag _" + tag + "_ has not been assigned to any of _" + npc + "'s_ quotes.");
            }else{
                // Process results into separate reponses
                for (var i = 0; i < cb.length; i++) {
                    // Add responses to message body
                    results.push("- " + cb[i].quote);
                    //console.log(cb[i].quote);
                }
                
            }

            // Send the message to chat
            app.bot.sendMessage(message, results);

            // reset results to empty array
            results = [];
            
        });
    }
}

module.exports.processNpcQuotes = processNpcQuotes;

@@ -2629,52 +2629,834 @@
 %0A%7D%0A%0A
-module.exports.processNpcQuotes = processNpc
+/**%0A * Return a message containing quotes related to a tag%0A */%0A%0Avar processTagQuotes = function(tag, message) %7B%0A    var results = %5B%5D;%0A%0A    results.push(%22**__All quotes related to _%22 + tag + %22_ __**%22 +'%5Cn');%0A%0A    db.findByTag(tag, function(cb) %7B%0A        %0A        if (cb.length %3C 1) %7B%0A            results.push(%22Sorry, but the tag _%22 + tag + %22_ has not been assigned to any of npc's quotes.%22);%0A        %7Delse%7B%0A            var list = %5B%5D;%0A            for (var i = 0; i %3C cb.length; i++) %7B%0A                results.push(cb%5Bi%5D.name + %22 - %22 + cb%5Bi%5D.quote);%0A            %7D%0A        %7D%0A%0A        // Send the message to chat%0A        app.bot.sendMessage(message, results);%0A%0A        // reset results to empty array%0A        results = %5B%5D;%0A%0A    %7D);%0A%7D%0A%0Amodule.exports.processNpcQuotes = processNpcQuotes;%0Amodule.exports.processTagQuotes = processTag
 Quot

add method to search by tag