 settings = null
 utilities = null
 
-SettingsHelper = null
-MenuManager = null
-SerialHelper = null
-StatusView = null
-LoginView = null
-SelectCoreView = null
-RenameCoreView = null
-ClaimCoreView = null
-IdentifyCoreView = null
-ListeningModeView = null
-SelectPortView = null
-CompileErrorsView = null
-CloudVariablesAndFunctions = null
-ApiClient = null
-
 module.exports =
+  SettingsHelper: null
+  MenuManager: null
+  SerialHelper: null
+  StatusView: null
+  LoginView: null
+  SelectCoreView: null
+  RenameCoreView: null
+  ClaimCoreView: null
+  IdentifyCoreView: null
+  ListeningModeView: null
+  SelectPortView: null
+  CompileErrorsView: null
+  CloudVariablesAndFunctions: null
+  ApiClient: null
+
   statusView: null
   loginView: null
   selectCoreView: null

  serialize: ->

  login: ->
    LoginView ?= require './views/login-view'
    @loginView ?= new LoginView()
    # You may ask why this isn't in LoginView? This way, we don't need to
    # require/initialize login view until it's needed.
    atom.workspaceView.command 'spark-ide:cancel-login', => @loginView.cancelCommand()
    @loginView.show()

  logout: ->
    if !SettingsHelper.isLoggedIn()
      return

    LoginView ?= require './views/login-view'
    @loginView ?= new LoginView()

    @loginView.logout()

  selectCore: ->
    SelectCoreView ?= require './views/select-core-view'
    @selectCoreView ?= new SelectCoreView()

    if !SettingsHelper.isLoggedIn()
      return

    @selectCoreView.show()

  renameCore: ->
    RenameCoreView ?= require './views/rename-core-view'

    if !SettingsHelper.isLoggedIn()
      return

    if !SettingsHelper.hasCurrentCore()
      return

    @renameCoreView ?= new RenameCoreView(SettingsHelper.get 'current_core_name')
    @renameCoreView.attach()

  removeCore: ->
    if !SettingsHelper.isLoggedIn()
      return

    if !SettingsHelper.hasCurrentCore()
      return

    removeButton = 'Remove ' + SettingsHelper.get('current_core_name')
    buttons = {}
    buttons['Cancel'] = ->

    buttons['Remove ' + SettingsHelper.get('current_core_name')] = =>
      workspace = atom.workspaceView
      ApiClient = require './vendor/ApiClient'
      client = new ApiClient SettingsHelper.get('apiUrl'), SettingsHelper.get('access_token')
      @removePromise = client.removeCore SettingsHelper.get('current_core')
      @removePromise.done (e) =>
        if !@removePromise
          return
        atom.workspaceView = workspace
        SettingsHelper.clearCurrentCore()
        atom.workspaceView.trigger 'spark-ide:update-core-status'
        atom.workspaceView.trigger 'spark-ide:update-menu'


        @compileCloudPromise = null

  showCompileErrors: ->
    @CompileErrorsView = require './views/compile-errors-view'
    @compileErrorsView ?= new @CompileErrorsView
    @compileErrorsView.show()

  toggleCloudVariablesAndFunctions: ->
    # TODO: Implement
    @CloudVariablesAndFunctions ?= require './views/cloud-variables-and-functions-view'
    @cloudVariablesAndFunctions ?= new @CloudVariablesAndFunctions
