 #
 
 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: 5 }, (object) ->
+        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

                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}")

                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) ->
