 
   constructor: (robot) ->
     super robot
-    @robot.logger.info "Constructed!"
 
   send: (envelope, strings...) ->
     console.log("**** send ****")
-    @robot.logger.info("**** send ****")
+    console.log(envelope)
+    console.log(strings)
+    console.log("**** send ****")
     @wss.clients.forEach (client) =>
       client.send(JSON.stringify(strings))
 


    @wss = new WebSocketServer {port: 4773}
    @wss.on 'connection', (ws) =>
      console.log("**** connected ****")
      @robot.logger.info("**** connected ****")
      ws.on 'message', @on_message
      ws.on 'heartbeat', @on_heartbeat

    console.log("**** Woohoo ****")
    @robot.logger.info("**** Woohoo ****")
    @emit "connected"

exports.use = (robot) ->


  reply: (envelope, strings...) ->
    console.log("**** reply ****")
    console.log(envelope)
    console.log(strings)
    console.log("**** reply ****")
    strings = strings.map (s) -> "#{envelope.user.name}: #{s}"
    @send envelope, strings...

  _on_text_message: (user, text) =>
    @robot.logger.debug("Received text message from #{user.name} => #{text}")
    @receive new TextMessage(user, text)

  on_message: (message) =>
    @robot.logger.debug("Received message #{message}")
    message = JSON.parse message
    user = @robot.brain.userForId(message.username)
    switch message.type
        when "text" then @_on_text_message(user, message.text)
        else @robot.logger.error("Unexpected message type #{message.type}")

  on_heartbeat: (e) =>
    @robot.logger.debug("heartbeat")

  run: ->
    # Hack robot to inject proxy settings
