-#= require trix/models/selection_manager
+{handleEvent} = Trix
 
 class Trix.AutoComplete
    constructor: (@editor, @dropDownContainer, @strategies)->
       @autoCompleteOn = false
       @documentString = @editor.getDocument().toString()
-      #@dropDownController = new Trix.DropDownController @editor, this, @strategies, @dropDown
       @startingPosition = 0
       @endingPosition = 0
-      @strategyIndex = 0
       @numDropDownItems = 0
-      @dropDownItem = '.autoComplete-item'
       @currentStrategy
 
-      $(document).ready ->
-         $(@dropDown).hide()
-
    autoCompleteHandler: ->
-      newDocumentString = @editor.getDocument().toString()
+      @documentString = @editor.getDocument().toString()
       position = @editor.getPosition()
 
-      @dropDownContainer.empty()
-   #   $(@dropDownContainer).show()
+      @dropDownContainer.hide()
 
-      @documentString = newDocumentString
-      @autoCompleteStart(newDocumentString[position - 1], position - 1)
+      return if position == 0
 
-      #@autoCompleteOn = false
+      @checkAutoComplete(@documentString[position - 1], position - 1)
 
-   autoCompleteStart: (character, position) ->
+   checkAutoComplete: (currentString, position) ->
       @autoCompleteOn = false
       searchForSymbol = true
       results = []
-      currentString = ""
 
-      currentString += character
       @endingPosition = position
 
       self = this
 
-      do ->
-         while searchForSymbol
-            i = 0
-            while i < self.strategies.length
-               regexObj = self.strategies[i].match
-               if regexObj.test(currentString)
-                  searchForSymbol = false
-                  self.autoCompleteOn = true
-                  self.startingPosition = position
-                  self.strategyIndex = i
-                  self.currentStrategy = self.strategies[i]
-                  return
-               ++i
+      while searchForSymbol
+         if currentString[0] == ' '
+            searchForSymbol = false
+            break
+         else if position < 0
+            searchForSymbol = false
+            break
 
-            # slight optimization by moving if statement before loop idk if care
-            if currentString[0] == ' '
+         for strategy in self.strategies
+            if strategy.trigger.test(currentString)
                searchForSymbol = false
-               return
-            else
-               if position <= 0
-                  searchForSymbol = false
-                  return
-               position--
+               @autoCompleteOn = true
+               @startingPosition = position
+               @currentStrategy = strategy
+               @searchTerm = currentString
+               break
 
-               currentString = self.documentString[position] + currentString
+         position--
+         currentString = @documentString[position] + currentString
 
       if @autoCompleteOn
-         searchTerm = ""
+         if @currentStrategy.index
+            @searchTerm = @searchTerm.slice(@currentStrategy.index)
 
-         if @currentStrategy.index
-            searchTerm = currentString.slice(@currentStrategy.index)
+         @currentStrategy.search(@searchTerm, @populateDropDown)
 
-         @currentStrategy.search(searchTerm, @populateDropDown)
-
-      return results
 
    autoCompleteEnd: ->
       @autoCompleteOn = false


      @positionDropDown()

      @addDropDown result for result in results
      @dropDownContainer.show()

      @numDropDownItems = results.length
      @initEventListeners()

   addDropDown: (result)->
      if result[0] == '<'
         result = result.slice(1, -1)

      html = @currentStrategy.template(result)

      container = $('<li/>')

            $(this).removeClass('active')
         )

      dropDownItemJQ.on 'mousedown', () ->
         self.insertAutoCompleteItem($(this))

      dropDownItemJQ.on 'keydown', (event) ->
         currentDataIndex = parseInt($(this).attr('data-index'))
         nextDataIndex = 0
         #jquerySelector = dropDownItemSelector + "[data-index='" + nextDataIndex + "']"

         if event.keyCode == 38
            if currentDataIndex == 0
               nextDataIndex = self.numDropDownItems - 1
            else
               nextDataIndex = currentDataIndex - 1

            jquerySelector = dropDownItemSelector + "[data-index='" + nextDataIndex + "']"
            $(this).blur()
            $(jquerySelector).focus()
            event.preventDefault()
