'use babel'

import {Emitter, CompositeDisposable} from 'atom'

export class Editor {
  constructor(editorLinter) {
    this.editor = editorLinter.editor
    this.editorLinter = editorLinter
    this.subscriptions = new CompositeDisposable()
    this.emitter = new Emitter()
    this.rowMessages = {}
    this.highlight = atom.config.get('linter-ui-default.highlightIssues')

    this.gutter = null
    this.bubble = null
    this.bubbleRange = null
    this.handleGutter()

    this.subscriptions.add(this.emitter)
  }
  handleGutter() {
    if (this.highlight) {
      const position = atom.config.get('linter-ui-default.gutterPosition')
      this.gutter = this.editor.addGutter({
        name: 'linter-ui-default',
        priority: position === 'Left' ? -100 : 100
      })
    }
  }
  updateMessages({added, removed}) {
    if (this.highlight) {
      const addedLength = added.length
      const removedLength = removed.length

      for (let i = 0; i < addedLength; ++i) {
        const message = added[i]
        // Add it to rowMessages
        const messageRow = message.range.start.row
        if (!this.rowMessages[messageRow]) {
          this.rowMessages[messageRow] = [message]
        } else {
          this.rowMessages[messageRow].push(message)
        }
      }
      for (let i = 0; i < removedLength; ++i) {
        const message = removed[i]
        // Remove it from rowMessages
        const messageRow = message.range.start.row
        if (this.rowMessages[messageRow]) {
          const rowMessages = this.rowMessages[messagRow]
          const index = rowMessages.indexOf(message)
          if (index !== -1) {
            rowMessages.splice(index, 1)
          }
        }
      }
    }
  }
  onDidDestroy(callback) {
    return this.emitter.on('did-destroy', callback)
  }
  destroy() {
    this.emitter.emit('did-destroy')
  }
  dispose() {
    this.editor = null
    this.editorLinter = null
    this.rowMessages = []
    this.subscriptions.dispose()
  }
}

@@ -151,45 +151,8 @@
 tor%0A
-    this.editorLinter = editorLinter%0A
     
@@ -198,16 +198,16 @@
 sable()%0A
+
     this
@@ -257,16 +257,45 @@
 es = %7B%7D%0A
+    this.markers = new Map()%0A
     this
@@ -1250,32 +1250,544 @@
 ssage)%0A        %7D
+%0A%0A        const marker = this.editor.markBufferRange(message.range, %7Binvalidate: 'inside'%7D)%0A        const gutter = document.createElement('span')%0A        gutter.className = %60linter-gutter linter-highlight $%7Bmessage.class%7D%60%0A        this.editor.decorateMarker(marker, %7B%0A          type: 'highlight',%0A          class: %60linter-highlight $%7Bmessage.class%7D%60%0A        %7D)%0A        this.gutter.decorateMarker(marker, %7B%0A          class: 'linter-row',%0A          item: gutter%0A        %7D)%0A        this.markers.set(message, marker)
 %0A      %7D%0A      f
@@ -2048,16 +2048,17 @@
 s%5Bmessag
+e
 Row%5D%0A   
@@ -2196,24 +2196,105 @@
 %7D%0A        %7D%0A
+        this.markers.get(message).destroy()%0A        this.markers.delete(message)%0A
       %7D%0A    
@@ -2483,28 +2483,23 @@
    this.
-editorLint
+mark
 er
+s
  = null%0A

Make gutters and underlines work