// Description:
//   Gets the latest beers and their stats from the taphouse beerboard.
//
// Dependencies:
//   "tiny-rebel-web-scraper": "latest"
//
// Commands:
//   hubot what|which beers are on|available - gets the latest beer info from the board
//   hubot get me drunk - gets the strongest available beer
//
// Authors:
//   studioromeo
//   tomseldon
//

'use strict';

const tinyRebelWebScraper = require('tiny-rebel-web-scraper');

module.exports = function (robot) {
    const excuses = [
        'No beers here. Go to the City Arms instead.',
        'Sorry, dunno what\'s on, we\'re all drunk.',
        'ZZZzzz...wha..no, drank it all, try somewhere else...zzzZZZzzzZZZzzz...'
    ];

    robot.respond(/(what|which) beers are (on|available)/i, function (message) {
        tinyRebelWebScraper.getAllDrinks('cardiff')
            .then((drinks) => {
                const response = [
                    'Hey there!',
                    'The following drinks are available at Tiny Rebel (Cardiff):'
                ]
                    .concat(formatDrinks(drinks));

                message.send(response.join('\n'));
            })
            .catch((error) => {
                console.error(error);
                message.send(message.random(excuses));
            });
    });

    robot.respond(/get me drunk/i, function (message) {
        tinyRebelWebScraper.getAllDrinks('cardiff')
            .then((drinks) => {
                // Get the drink with the highest ABV level
                const drink = drinks.reduce((previous, current) => (previous.abv > current.abv) ? previous : current);

                const response = [
                    'Hey there!',
                    'The strongest drink on tap at Tiny Rebel (Cardiff) is:'
                ]
                    .concat(formatDrinks([drink]));

                message.send(response.join('\n'));
            })
            .catch((error) => {
                console.error(error);
                message.send(message.random(excuses));
            });
    });

    /**
     * Takes an array of drinks (from the web scraper) and parses them into an array
     * of formatted strings, ready to be returned by the robot.
     *
     * @param {Drink[]} drinks
     *
     * @returns {String[]}
     */
    function formatDrinks (drinks) {
        const formattedDrinks = [];

        for (const drink of drinks) {
            let output = `(beer) ${drink.name} [${drink.style}] - ${drink.formattedAbv} -`;

            if (drink.quantity === 'half') {
                output = `${output} ½`;
            }

            output = `${output} ${drink.formattedPrice}`;

            formattedDrinks.push(output);
        }

        return formattedDrinks;
    }
};

@@ -748,16 +748,40 @@
 ailable)
+ ?(%5C(sorted by (.+)?%5C))?
 /i, func
@@ -873,32 +873,253 @@
 n((drinks) =%3E %7B%0A
+                const sortField = message.match%5B4%5D;%0A                const shouldSort = Boolean(sortField);%0A%0A                if (shouldSort) %7B%0A                    drinks = sortDrinks(drinks, sortField);%0A                %7D%0A%0A
                 
@@ -2288,24 +2288,507 @@
 );%0A    %7D);%0A%0A
+    /**%0A     * Sort an array of drinks by the specified sort field.%0A     *%0A     * @param %7BDrink%5B%5D%7D drinks%0A     * @param %7Bstring%7D sortField%0A     *%0A     * @returns %7BDrink%5B%5D%7D%0A     */%0A    function sortDrinks (drinks, sortField) %7B%0A        return drinks.sort((a, b) =%3E %7B%0A            if (a%5BsortField%5D %3C b%5BsortField%5D) %7B%0A                return -1;%0A            %7D%0A%0A            if (a%5BsortField%5D %3E b%5BsortField%5D) %7B%0A                return 1;%0A            %7D%0A%0A            return 0;%0A        %7D);%0A    %7D%0A%0A
     /**%0A    

Add ability to sort drinks by field