 #   None
 #
 # Commands:
+#   hubot ttv featured - Returns the first 5 featured live streams
 #   hubot ttv game <category> - Returns the first 5 live streams in a game <category> (case-sensitive)
-#   hubot ttv featured - Returns the first 5 featured live streams
+#   hubot ttv search <query> - Returns the first 5 live streams matching the search <query>
 #   hubot ttv stream <name> - Returns information about stream <name>
-#   hubot ttv search <query> - Returns a list of live streams matching the search <query>
+#   hubot ttv top - Returns the top 5 games sorted by the number of current viewers on Twitch, most popular first
 #
 # Author:
 #   MrSaints

                return

            if not object.stream
                msg.reply "The stream you have entered (\"#{msg.match[1]}\") is currently offline."
                return

            channel = object.stream.channel
            msg.send "#{channel.display_name} is streaming #{channel.game} @ #{channel.url}"
            msg.send "Stream status: \"#{channel.status}\""
            msg.send "Viewers: #{object.stream.viewers}"

twitch_request = (msg, api, params = {}, handler) ->
    msg.http("https://api.twitch.tv/kraken#{api}")

#

module.exports = (robot) ->
    maxResults = 5

    robot.respond /ttv featured/i, (msg) ->
        twitch_request msg, '/streams/featured', limit: maxResults, (object) ->
            for feature in object.featured
                channel = feature.stream.channel
                msg.send "#{feature.stream.game}: #{channel.display_name} (#{channel.status}) - #{channel.url} [Viewers: #{feature.stream.viewers}]"

    robot.respond /ttv game (.*)/i, (msg) ->
        category = msg.match[1]
        twitch_request msg, '/streams', { game: category, limit: maxResults }, (object) ->
            if object._total is 0
                msg.reply "No live streams were found in \"#{category}\". Try a different category or try again later."
                return
