         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'

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  if workspace

  return if checkWorkspace machineLabel, workspaceSlug, channelId
  then workspace
  else null


checkWorkspace = (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

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


    try

      dataProvider.getMachineAndWorkspaceByChannelId id, (machine, workspace) =>
        return routeToLatestWorkspace()  unless workspace

        query = socialApiId: channel.creatorId

        remote.api.JAccount.some query, {}, (err, account) =>
          if err
            routeToLatestWorkspace()
            return throw new Error err

          username  = account.first.profile.nickname
          channelId = channel.id

          return loadIDE { machine, workspace, username, channelId }

    catch e

      routeToLatestWorkspace()
      return console.error e


  # kd.singletons.socialapi.cacheable 'channel', id, (err, channel) ->

  #   return routeToLatestWorkspace()  if err

  #   try

  #     [workspace] = globals.userWorkspaces.filter (w) -> w.channelId is channel.id

  #     return routeToLatestWorkspace()  unless workspace

  #     [machine] = globals.userMachines.filter (m) -> m.uid is workspace.machineUId

  #     query = socialApiId: channel.creatorId
  #     remote.api.JAccount.some query, {}, (err, account) =>

  #       return throw new Error err  if err

  #       username  = account.first.profile.nickname
  #       channelId = channel.id

  #       return loadIDE { machine, workspace, username, channelId }

  #   catch e

  #     console.error e
  #     return routeToLatestWorkspace()


# refreshWorkspaces = (callback) ->

#   {mainView, computeController} = kd.singletons

#   computeController.ready ->
#     mainView.activitySidebar.fetchWorkspaces callback


module.exports = -> lazyrouter.bind 'ide', (type, info, state, path, ctx) ->

  globals.userMachines = []
  globals.userWorkspaces = []

  switch type

    when 'home' then routeToLatestWorkspace()

    when 'machine'
      { machineLabel } = info.params

      # we assume that if machineLabel is all numbers it is the channelId - SY
      if /^[0-9]+$/.test machineLabel
        loadCollaborativeIDE machineLabel
      else
        dataProvider.getMachineByLabel machineLabel, (machine) ->
          if machine then routeToMachineWorkspace machine
          else routeToLatestWorkspace()

      # {machineLabel} = info.params
      # # we assume that if machineLabel is all numbers it is the channelId - SY
      # if /^[0-9]+$/.test machineLabel
      #   refreshWorkspaces -> loadCollaborativeIDE machineLabel
      # else if machine = getMachine machineLabel
      #   routeToMachineWorkspace machine
      # else
      #   routeToLatestWorkspace()


    when 'workspace'
      { params } = info
      params.username = username = nick()

      dataProvider.getMachineAndWorkspace params, (machine, workspace) ->

        if machine and workspace
          loadIDE { machine, workspace, username }
        else if machine
          routeToMachineWorkspace machine
        else
          routeToLatestWorkspace()

      # {params} = info
      # {machineLabel} = params
      # params.username = nick()

      # refreshWorkspaces ->

      #   findWorkspace params, (workspace) ->

      #     if workspace
      #       return loadWorkspace workspace, params
      #     else
      #       for machine in globals.userMachines when machine.slug is machineLabel
      #         break

      #       if machine
      #       then routeToMachineWorkspace machine
      #       else loadWorkspace null, params

      #     routeToLatestWorkspace()
