 #
 
 TENDERLOIN_ROOT = 'http://tenderloin-sf.herokuapp.com/api/'
+TENDERLOIN_API_KEY = ''
+
+authenticated_client = (msg, url) ->
+  msg.http(url).headers({'x-tenderloin-api-key': TENDERLOIN_API_KEY})
 
 module.exports = (robot) ->
 

  robot.respond /(.*) gist (https?:\/\/gist\.github\.com\/.*\/[0-9a-f]+)/i, (msg) ->
    room = msg.match[1]
    gist_url = msg.match[2] + '/raw'
    msg.http("#{TENDERLOIN_ROOT}/rooms/#{room}/gist")
       .query(q: gist_url)
       .get() (err, response, body) ->
         msg.send body

  robot.respond /(.*) sound stop/i, (msg) ->
    room = msg.match[1]
    msg.http("#{TENDERLOIN_ROOT}/rooms/#{room}/stop_sound")
       .get() (err, response, body) ->
         return msg.send "And there was silence" if res.statusCode is 200

  robot.respond /(.*) (cam|camera) (off|on)/i, (msg) ->
    room = msg.match[1]
    state = msg.match[3]
    authenticated_client("#{TENDERLOIN_ROOT}/rooms/#{room}/commands/camera_#{state}")
       .get() (err, res, body) ->
         return msg.send("Camera in #{room} #{if state is "on" then "enabled" else "disabled"}") if res.statusCode is 200

  ## Play an alert noise
  robot.respond /(.*) alert/i, (msg) ->
    room = msg.match[1]
    authenticated_client("#{TENDERLOIN_ROOT}/rooms/#{room}/commands/alert")
       .get() (err, res, body) ->
         return msg.send("That got their attention") if res.statusCode is 200
         return msg.send("Oh noes! It broke!")
