'use babel';

import {CompositeDisposable} from 'atom';
import {showTreeView, hideTreeView, toggleTreeView,
  storeFocusedElement} from './autohide-tree-view.js';
import {getTreeView, getTreeViewEl} from './utils.js';

export default function initCommands() {
  var disposables = new CompositeDisposable(
    // resize the tree view when project.paths changes
    atom.project.onDidChangePaths(() =>
      resizeTreeView()
    ),

    // add command listeners
    atom.commands.add('atom-workspace', {
      // tree-view commands
      ['tree-view:show'](event) {
        event.stopImmediatePropagation();
        showTreeView();
      },
      // this one isn't actually in the tree-view package
      // but have it here just in case :)
      ['tree-view:hide'](event) {
        event.stopImmediatePropagation();
        hideTreeView();
      },
      ['tree-view:toggle'](event) {
        event.stopImmediatePropagation();
        toggleTreeView();
      },
      // patch reveal-active-file because it doesn't work
      // when the tree view isn't visible
      ['tree-view:reveal-active-file']() {
        showTreeView(0).then(() =>
          getTreeView().scrollToEntry(getTreeView().getSelectedEntries()[0])
        );
      },
      ['tree-view:toggle-focus']() {
        toggleTreeView();
      },
      ['tree-view:remove']() {
        resizeTreeView();
      },
      ['tree-view:paste']() {
        resizeTreeView();
      },
      ['tree-view:expand-directory']() {
        resizeTreeView();
      },
      ['tree-view:recursive-expand-directory']() {
        resizeTreeView();
      },
      ['tree-view:collapse-directory']() {
        resizeTreeView();
      },
      ['tree-view:recursive-collapse-directory']() {
        resizeTreeView();
      },
      // tree-view-finder package commands
      ['tree-view-finder:toggle']() {
        resizeTreeView().then(() => showTreeView());
      },
    }),

    // hide the tree view when `esc` key is pressed
    atom.commands.add(getTreeViewEl(), 'tool-panel:unfocus', () =>
      hideTreeView()
    ),
  );

  // hide the tree view when a file is opened by a command
  for(let direction of ['', '-right', '-left', '-up', '-down']) {
    disposables.add(atom.commands.add('atom-workspace', `tree-view:open-selected-entry${direction}`, didOpenFile));
  }

  for(let i of [1, 2, 3, 4, 5, 6, 7, 8, 9]) {
    disposables.add(atom.commands.add('atom-workspace', `tree-view:open-selected-entry-in-pane-${i}`, didOpenFile));
  }

  return disposables;
}

function didOpenFile() {
  process.nextTick(() => {
    storeFocusedElement(atom.views.getView(atom.workspace.getActiveTextEditor()));
    hideTreeView();
  });
}

@@ -122,16 +122,32 @@
 dElement
+, resizeTreeView
 %7D from '
@@ -1456,222 +1456,141 @@
     
-  %5B'
+%7D),%0A%0A    // hide the 
 tree
--
+ 
 view
-:expand-directory'%5D() %7B%0A        resizeTreeView();%0A      %7D,%0A      %5B'tree-view:recursive-expand-directory'%5D() %7B%0A        resizeTreeView();%0A      %7D,%0A      %5B'tree-view:collapse-directory'%5D() %7B
+ when %60esc%60 key is pressed%0A    atom.commands.add(getTreeViewEl(), 'tool-panel:unfocus', () =%3E
 %0A      
-  resiz
+hid
 eTre
@@ -1600,76 +1600,72 @@
 ew()
-;
 %0A    
-  %7D
+)
 ,%0A  
-    %5B'tree-view:recursive-collapse-directory
+);%0A%0A  for(let action of %5B'expand', 'collapse
 '%5D
-(
 ) %7B%0A    
     
@@ -1664,89 +1664,69 @@
     
-    resizeTreeView();%0A      %7D,%0A      // tree-view-finder package commands
+disposables.add(atom.commands.add('atom-workspace', %7B
 %0A      %5B
 'tre
@@ -1713,33 +1713,33 @@
 pace', %7B%0A      %5B
-'
+%60
 tree-view-finder
@@ -1735,228 +1735,104 @@
 view
--finder:toggle'%5D() %7B%0A        resizeTreeView().then(() =%3E showTreeView());%0A      %7D,%0A    %7D),%0A%0A    // hide the 
+:$%7Baction%7D-directory%60%5D: resizeTreeView,%0A      %5B%60
 tree
- 
+-
 view
- when %60esc%60 key is pressed%0A    atom.commands.add(getTreeViewEl(), 'tool-panel:unfocus', () =%3E%0A      hid
+:recursive-$%7Baction%7D-directory%60%5D: resiz
 eTre
@@ -1832,38 +1832,38 @@
 sizeTreeView
-()
+,
 %0A    
-),%0A  );
+%7D));%0A  %7D
 %0A%0A  // hide 

move some commands