 # with the document's contents unless keepEditorContents is true. (In which case the document's
 # contents are nuked and replaced with the editor's).
 window.sharejs.extendDoc 'attach_ace', (session, keepEditorContents) ->
-  console.log "attach_ace, this", session
-
   doc = this
   editorDoc = session.getDocument()
   editorDoc.setNewLineMode 'unix'

      state =
        description: scratch.plunk.description
        tags: scratch.plunk.tags
        files: []
      
      for buffer in scratch.buffers.queue
        state.files.push
          content: buffer.content
          filename: buffer.filename
    
      state
      
    join: (id = uid()) ->
      console.log "Stream::join", arguments...
      self = @
      
      scratch.loading = true
      
      @getConnection().open "stream:#{id}", "json", (err, doc) ->
        if err then return console.error "message", "Connection error", """
          Failed to join the stream #{id}. Please double-check that you entered
          the right stream id. If the problem persists, please contact the
          administrator.
        """
        
        if doc.created is true
          # Reset the channel to the current local state
          doc.submitOp [ { p: [], od: doc.snapshot, oi: self.getLocalState() } ], (err) ->
            if err then console.error "error", "Error setting initial state"
            else self.start(id, doc, true)
            self.scope.$apply -> scratch.loading = false
        else $timeout ->
          self.start(id, doc)
          scratch.loading = false
    
    start: (id, @doc, keep = false) ->
      console.log "Stream::start", arguments...
      self = @
      
      unless keep
        state = @doc.get()
        
        console.log "State is", state

        # We don't want to reset the *whole* scratch here as that would result
        # in changing the active plunk to a blank one. We will just adjust the
        # plunk's description and tags and reset the buffers.
        scratch.plunk.description = state.description
        scratch.plunk.tags = state.tags
        
        scratch.buffers.reset(state.files)
        scratch.buffers.activate(index) if index = scratch.buffers.findBy("filename", "index.html")
        
      self.scope.$apply ->
        stream.streaming = true
        stream.doc = self.doc
        stream.keep = keep
        
        # Assign the scratch to the local scope which will trigger creation of
        # custom directives for each channel
        self.scope.scratch = scratch
      
      @doc.at("files").on "insert", (pos, data) ->
        console.log "sharejs::insert", arguments...
        $timeout -> scratch.buffers.add(data)
      
      @doc.at("files").on "delete", (pos, data) ->
        console.log "sharejs::delete", arguments...
        $timeout -> scratch.buffers.remove(buffer) if buffer = scratch.buffers.findBy("filename", data.filename)
        
      @doc.on "change", (events) ->
        console.log "sharejs::change", arguments...
        ###
        angular.forEach events, (e) ->
          if e.p.length then switch e.p[0]
            when "description" then self.remote.trigger "description:change", e.oi or ""
            when "channels"
              # TODO: This first comparison is VERY inefficient
              if e.p.length == 1 and not _.isEqual(self.getLocalState().channels, e.oi) then self.remote.trigger "channels:reset", e.oi
              else if e.p.length == 2
                if e.od? then self.remote.trigger "channels:remove", e.od
                else if e.oi? then self.remote.trigger "channels:add", e.oi
              else if e.p.length == 3
                self.remote.trigger "channels:rename", e.oi, e.od, e.p[2]
          else
            self.remote.trigger "description:change", e.oi.description
            self.remote.trigger "channels:reset", e.oi.channels, keep: false
        ###
          
    link: ($scope, el, attrs) ->
      self = @
      self.scope = $scope
      
      $scope.stream = stream
      
      $scope.startStream = (id) ->
        self.join(id)
        
      
    deactivate: ($scope, el, attrs) ->

  doc: null
]

module.run [ "$location", "$timeout", "$q", "panels", "scratch", "stream", ($location, $timeout, $q, panels, scratch, stream) ->
  panels.push new class
    name: "streamer"
    order: 3
