 #   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

#

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

                channel = stream.channel
                msg.send "#{channel.display_name} (\"#{channel.status}\") - #{channel.url} [Viewers: #{stream.viewers}]"

            if object._total > maxResults
                msg.reply "There are #{object._total - maxResults} other \"#{category}\" live streams."

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

            for stream in object.streams
                channel = stream.channel
                msg.send "#{channel.display_name} (\"#{channel.status}\") - #{channel.url} [Viewers: #{stream.viewers}]"

            if object._total > maxResults
                msg.reply "There are #{object._total - maxResults} other live streams matching your search query: \"#{query}\"."

    robot.respond /ttv stream (.*)/i, (msg) ->
        twitch_request msg, "/streams/#{msg.match[1]}", null, (object) ->
