 CowriterView = require './cowriter-view'
-diff = require 'diff'
+Diff = require 'diff'
 AV = require 'avoscloud-sdk'
 {CompositeDisposable} = require 'atom'
 AV.initialize 'APPID','APPKEY'

    cowriterViewState: @cowriterView.serialize()

  stop: ->
    textBuffer = @editor.getText()
    @save(textBuffer)
    @cowriterView.setContent(@textBuffer)

  save: (text) ->
    @acticle.save { 'demo': text }, { success: (object) -> alert 'LeanCloud works!' }

  toggle: ->
    console.log 'Cowriter was toggled!'
    if @modalPanel.isVisible()
      @modalPanel.hide()
    else
      @modalPanel.show()

    @cowriterView = new CowriterView(state.cowriterViewState)
    @modalPanel = atom.workspace.addModalPanel(item: @cowriterView.getElement(), visible: false)

    # Current document's title
    @title= 'demo'

    # Save new diff
    @Acticles = AV.Object.extend 'Acticles'

    # Query for new diff
    @query = new AV.Query 'Acticles'

    # Base time for syncing
    @syncTime = new Date()

    @editor = atom.workspace.getActiveTextEditor()
    @oldText = ''
    @newText = ''

    # Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
    @subscriptions = new CompositeDisposable

    # Register command that toggles this view
    @subscriptions.add atom.commands.add 'atom-workspace', 'cowriter:toggle': => @toggle()

    # Server: Sends changes when change stops
    @subscriptions.add @editor.onDidStopChanging => @stop()

    # May suffered from unsafe-eval of CSP
    # Cilent: Refresh view every 3 seconds
    @interval = setInterval ( => @getDiff() ), 3000

  deactivate: ->
    @modalPanel.destroy()
