 
     if isSameMachine
       if isSameWorkspace then ideInstance = instance
+      # should not be the case anymore since 'my-workspace' deprecated.
       else if workspace.slug is 'my-workspace'
         if instance.workspaceData?.isDefault
           ideInstance = instance
 
   if ideInstance
     appManager.showInstance ideInstance
-    selectWorkspaceOnSidebar data
+    selectWorkspaceOnSidebar data # should not be required
   else
     callback()
 
 
 routeToFallback = ->
 
-  router = kd.getSingleton 'router'
-
-  for machine in globals.userMachines when machine.isMine()
-    return routeToMachineWorkspace machine
-
-  kd.singletons.computeController.fetchMachines (err, machines) ->
-
-    if err or not machines.length
-    then router.handleRoute "/IDE/koding-vm-0/my-workspace"
-    else routeToMachineWorkspace machines.first
+  machines = dataProvider.getMyMachines()
+  router   = kd.getSingleton 'router'
+  [ obj ]  = machines
+
+  if obj.machine
+    routeToMachineWorkspace obj.machine
+  else
+    router.handleRoute "/IDE/koding-vm-0/my-workspace"
+
+
+  # router = kd.getSingleton 'router'
+
+  # for machine in globals.userMachines when machine.isMine()
+  #   return routeToMachineWorkspace machine
+
+  # kd.singletons.computeController.fetchMachines (err, machines) ->
+
+  #   if err or not machines.length
+  #   then router.handleRoute "/IDE/koding-vm-0/my-workspace"
+  #   else routeToMachineWorkspace machines.first
 
 
 routeToMachineWorkspace = (machine) ->
 
-  if latestWorkspace = getLatestWorkspace()
-    {machineLabel} = latestWorkspace
+  latestWorkspace = getLatestWorkspace()
+  workspaceSlug   = 'my-workspace'
+
+  if latestWorkspace
+    { machineLabel } = latestWorkspace
     if machineLabel is machine.label
       workspaceSlug = latestWorkspace.workspaceSlug
 
-  workspaceSlug ?= 'my-workspace'
-
   kd.getSingleton('router').handleRoute "/IDE/#{machine.slug}/#{workspaceSlug}"
 
+  # if latestWorkspace = getLatestWorkspace()
+  #   {machineLabel} = latestWorkspace
+  #   if machineLabel is machine.label
+  #     workspaceSlug = latestWorkspace.workspaceSlug
+
+  # workspaceSlug ?= 'my-workspace'
+
+  # kd.getSingleton('router').handleRoute "/IDE/#{machine.slug}/#{workspaceSlug}"
+
 
 routeToLatestWorkspace = ->
 
-  router = kd.getSingleton 'router'
-
-  if latestWorkspace = getLatestWorkspace()
-    {machineLabel, workspaceSlug, channelId} = latestWorkspace
-  else
-    return routeToFallback()
+  router          = kd.getSingleton 'router'
+  latestWorkspace = getLatestWorkspace()
+
+  return routeToFallback()  unless latestWorkspace
+
+  { machineLabel, workspaceSlug, channelId } = latestWorkspace
 
   if channelId
-    for workspace in globals.userWorkspaces when workspace.channelId is channelId
-      return router.handleRoute "/IDE/#{channelId}"
-    routeToFallback()
+    dataProvider.getMachineAndWorkspaceByChannelId channelId, (machine, ws) =>
+      if machine and ws then router.handleRoute "/IDE/#{channelId}"
+      else routeToFallback()
 
   else if machineLabel and workspaceSlug
-    return routeToFallback()  unless machine = getMachine machineLabel
-
-    findWorkspace {workspaceSlug, machineLabel}, (workspace) ->
-
-      if workspace
-      then router.handleRoute "/IDE/#{machineLabel}/#{workspaceSlug}"
-      else routeToMachineWorkspace machine
+    dataProvider.getMachineByLabel machineLabel, (machine, workspace) =>
+      if machine and workspace
+        router.handleRoute "/IDE/#{machineLabel}/#{workspaceSlug}"
+      else if machine
+        routeToMachineWorkspace machine
+      else
+        routeToFallback()
+
+
+  # router = kd.getSingleton 'router'
+
+  # if latestWorkspace = getLatestWorkspace()
+  #   {machineLabel, workspaceSlug, channelId} = latestWorkspace
+  # else
+  #   return routeToFallback()
+
+  # if channelId
+  #   for workspace in globals.userWorkspaces when workspace.channelId is channelId
+  #     return router.handleRoute "/IDE/#{channelId}"
+  #   routeToFallback()
+
+  # else if machineLabel and workspaceSlug
+  #   return routeToFallback()  unless machine = getMachine machineLabel
+
+  #   findWorkspace {workspaceSlug, machineLabel}, (workspace) ->
+
+  #     if workspace
+  #     then router.handleRoute "/IDE/#{machineLabel}/#{workspaceSlug}"
+  #     else routeToMachineWorkspace machine
 
 
 loadCollaborativeIDE = (id) ->

        app.amIHost           = yes

      appManager.tell 'IDE', 'mountMachineByMachineUId', machineUId
      selectWorkspaceOnSidebar data

  return callback()  unless ideApps?.instances


remote = require('app/remote').getInstance()
Machine = require 'app/providers/machine'
lazyrouter = require 'app/lazyrouter'
dataProvider = require 'app/userenvironmentdataprovider'


# loadWorkspace = (workspace, options) ->

#   {machine, machineLabel, username} = options

#   username or= nick()
#   machine  or= getMachine machineLabel, username

#   loadIDE { machine, workspace, username }


# findWorkspace = (options, callback) ->

#   {machineLabel, workspaceSlug, username} = options

#   kallback = (workspaces) ->

#     username ?= nick()

#     for machine in globals.userMachines
#       unless machine instanceof Machine
#         machine = new Machine machine: remote.revive machine

#       sameLabel = (machine.slug or machine.label) is machineLabel
#       sameOwner = machine.getOwner() is username

#       if sameLabel and sameOwner
#         machineUId = machine.uid
#         break

#     for workspace in workspaces
#       continue  unless workspace.machineUId is machineUId
#       continue  unless workspace.slug is workspaceSlug

#       return callback workspace

#     return callback null

#   if username
#   then filterWorkspacesByUsername username, kallback
#   else filterOwnWorkspaces kallback


# filterWorkspacesByUsername = (username, callback) ->

#   remote.cacheable username, (err, [account]) ->
#     if err
#       console.error err
#       callback []

#     originId = account.getId()

#     callback globals.userWorkspaces.filter (workspace) ->
#       originId is workspace.originId


# filterOwnWorkspaces = (callback) ->

#   callback globals.userWorkspaces.filter (workspace) ->
#     workspace.originId is whoami()._id


selectWorkspaceOnSidebar = (data) ->

  mainView = kd.getSingleton 'mainView'
  mainView.activitySidebar.selectWorkspace data


# getMachine = (label, username = nick()) ->

#   for m in globals.userMachines

#     unless m instanceof Machine
#       m = new Machine machine: remote.revive m

#     sameUser  = m.getOwner() is username
#     sameLabel = (m.slug or m.label) is label

#     return m  if sameLabel and sameUser


getLatestWorkspace = ->

  storage   = kd.getSingleton('localStorageController').storage 'IDE'
  workspace = storage.getValue 'LatestWorkspace'

  return  unless workspace

  { machineLabel, workspaceSlug, channelId } = workspace

  ws = null

  if checkWorkspace machineLabel, workspaceSlug, channelId
   ws = workspace

  return ws


checkWorkspace = (machineLabel, workspaceSlug, channelId) ->

  return dataProvider.validateCollaborationWorkspace machineLabel, workspaceSlug, channelId

  # for workspace in globals.userWorkspaces
  #   sameLabel = workspace.machineLabel is machineLabel
  #   sameSlug = workspace.slug is workspaceSlug
  #   sameChannelId = not channelId or workspace.channelId is channelId

  #   if sameLabel and sameSlug and sameChannelId
  #     return workspace


loadIDE = (data) ->

  { machine, workspace, username, channelId } = data
  selectWorkspaceOnSidebar data

  appManager = kd.getSingleton 'appManager'
  ideApps    = appManager.appControllers.IDE
  machineUId = machine.uid
  callback   = ->
    appManager.open 'IDE', { forceNew: yes }, (app) ->
      app.mountedMachineUId   = machineUId
