 util      = require 'util'
 WebSocket = require 'faye-websocket'
 
-module.exports = class
+###*
+ * @class Server
+ * @classdesc Serves up a roots project locally, handles live reloading
+ * @todo investigate superstatic
+###
+
+class Server
+
+  ###*
+   * Creates a new instance of the server
+   * 
+   * @param  {Function} roots - roots class instance
+   * @param  {String} dir - directory to server
+  ###
+
   constructor: (@roots, @dir) ->
 
+  ###*
+   * Stores all the sockets being used for pushing reloads
+   * @type {Array}
+  ###
   sockets: []
+
+  ###*
+   * Start the local server on the given port.
+   * 
+   * @param  {Integer} port - number of port to start the server on
+   * @return {Promise} promise for the server object
+  ###
 
   start: (port) ->
     app = connect()


    nodefn.call(@server.listen.bind(@server), port).yield(@server)

  close: ->
    @server.close()
    delete @server

  send_msg: (k, v) ->
    sock.send(JSON.stringify(type: k, data: v)) for sock in @sockets

  reload: -> @send_msg('reload')
  compiling: -> @send_msg('compiling')
  show_error: (err) -> @send_msg('error', err)

  # @api private

  inject_development_js = (app) ->
    app.use(infestor content:

    )
    app.use('/__roots__', connect.static(path.resolve(__dirname, 'browser')))

  ###*
   * Initializes websockets on the server instance.
  ###
  
  initialize_websockets = ->
    @server.on 'upgrade', (req, socket, body) =>
      if WebSocket.isWebSocket(req)
        ws = new WebSocket(req, socket, body)
        ws.on('open', => @sockets.push(ws))

module.exports = Server
