 
       appManager.on "AppUnregistered", (name, options) =>
         @setNavItemState {name, options}, 'initial'
-
-

class DockController extends KDViewController

  defaultItems = [
    { title : "Activity",  path : "/Activity", order : 10, type :"" }
    # { title : "Topics",    path : "/Topics",   order : 20, type :"" }
    { title : "Teamwork",  path : "/Teamwork", order : 20, type :"" }
    { title : "Terminal",  path : "/Terminal", order : 30, type :"" }
    { title : "Editor",    path : "/Ace",      order : 40, type :"" }
    { title : "Apps",      path : "/Apps",     order : 50, type :"" }
    # { title : "About",     path : "/About",    order : 70, type :"" }
  ]

  createHash = (arr)->

    hash = {}
    arr.forEach (item)-> hash[item.title] = item
    return hash


  constructor:(options = {}, data)->


      title        : 'navigation'
      items        : []

    mainController = KD.getSingleton 'mainController'
    mainController.ready @bound 'accountChanged'

    @storage.fetchValue 'navItems', (usersNavItems)=>

      unless usersNavItems
        @setNavItems defaultItems
        return @emit 'ready'

      @setNavItems @buildNavItems usersNavItems
      @emit 'ready'

  buildNavItems:(sourceItems)->

    finalItems = []

    _sources = KD.utils.arrayToObject sourceItems, 'title'
    for defaultItem in defaultItems
      sourceItem = _sources[defaultItem.title]
      if sourceItem
        continue  if sourceItem.deleted
        sourceItem.type = defaultItem.type
        finalItems.push sourceItem
      else
        finalItems.push defaultItem

    _defaults = KD.utils.arrayToObject defaultItems, 'title'
    for sourceItem in sourceItems
      defaultItem = _defaults[sourceItem.title]
      unless defaultItem
        finalItems.push sourceItem

    return finalItems

  saveItemOrders:(items)->

    items or= @navController.itemsOrdered

    navItems = []
    for own index, item of items
      {data} = item
      data.order = index
      navItems.push data

    @storage.setValue 'navItems', navItems, (err)->
      warn "Failed to save navItems order", err  if err

  resetItemSettings:->
    item.order = index  for own index, item of defaultItems

    @storage.unsetKey 'navItems', (err)=>
      warn "Failed to reset navItems", err  if err

      KD.resetNavItems defaultItems
      @navController.reset()
      "Navigation items has been reset."

  setNavItems:(items)->
    KD.setNavItems items
    @navController.reset()

  addItem:(item)->

    if KD.registerNavItem item
      @navController.addItem item
      @saveItemOrders()

  removeItem:(item)->

