 # Todo:
 # - Save favourites?
 
+#
+# Config
+#
+TWITCH_MAX_RESULTS = 5
+TWITCH_STORAGE_KEY = process.env.HUBOT_TWITCH_KEY or "_twitch"
+
 module.exports = (robot) ->
-    maxResults = 5
-
-    twitchData = ->
-        robot.brain.data.twitch or= {}
+    GetTTVData = ->
+        robot.brain.data[TWITCH_STORAGE_KEY] or= {}
 
     robot.respond /ttv fav/i, (msg) ->
         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 live streams matching your search query: \"#{query}\"."

    robot.respond /ttv stream (.+)/i, (msg) ->
        twitchRequest msg, "/streams/#{msg.match[1]}", null, (object) ->
            if object.status is 404
                msg.reply "The stream you have entered (\"#{msg.match[1]}\") does not exist."
                return

        return

    robot.respond /ttv featured/i, (msg) ->
        GetTwitchResult msg, '/streams/featured', null, (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]
        GetTwitchResult msg, '/streams', { game: category }, (object) ->
            if object._total is 0
                msg.reply "No live streams were found in \"#{category}\". Try a different category or try again later."
                return
