 'use strict'
 {CompositeDisposable} = require 'atom'
+_ = require 'underscore'
 
 class BlockCursor
   cursorStyle = null

    @subs.add atom.config.observe 'block-cursor.cursorType', @applyCursorType.bind @
    @subs.add atom.config.observe 'block-cursor.primaryColor', @applyPrimaryColor.bind @
    @subs.add atom.config.observe 'block-cursor.secondaryColor', @applySecondaryColor.bind @
    @subs.add atom.config.observe 'block-cursor.pulseDuration', @applyPulseDuration.bind @
    @subs.add atom.config.observe 'block-cursor.cursorThickness', @applyCursorThickness.bind @
    atom.config.set 'block-cursor.zzzpreview', 'The quick brown fox jumps over the lazy dog'

  deactivate: ->
    @subs.dispose()

    color = color.toRGBAString?() or @toRGBAString color
    @updateStylesheet secondarySelector, 'background-color', color
    @updateStylesheet secondarySelector, 'border-color', color

  applyBlinkInterval: (interval) ->
    @editorSub?.dispose?()
    @editorSub = atom.workspace.observeTextEditors (editor) ->
      editorPresenter = atom.views.getView(editor).component.presenter
      editorPresenter.cursorBlinkPeriod = interval * 2
      editorPresenter.stopBlinkingCursors(true)
      if interval > 0
        editorPresenter.startBlinkingCursorsAfterDelay = do ->
          _.debounce(editorPresenter.startBlinkingCursors, editorPresenter.getCursorBlinkResumeDelay())
      else
        editorPresenter.startBlinkingCursorsAfterDelay = ->

  applyPulseDuration: (duration) ->
    @updateStylesheet primarySelector, 'transition-duration', "#{duration}ms"
