'use babel';

import {buildPaneMap} from './paneMap'
import {PANE, PANE_AXIS} from '../constants/pane'

export default class Workspace {
  open (workspace) {
    if (!workspace) {
      return
    }

    atom.project.setPaths(workspace.projectPaths)
    const rootContainer = atom.workspace.paneContainer.getRoot()
    
    this.__closeAllPanes(rootContainer)  
    
    if (this.__isOldClient(workspace)) {
      this.__openTabs(workspace.tabs)
    } else {
      this.__openPanes(workspace.panes)
    }
  }

  save (workspaceName) {
    if (!workspaceName) {
      return
    }

    const projectPaths    = atom.project.getPaths()
    const rootContainer   = atom.workspace.paneContainer.getRoot()
    const panes           = buildPaneMap(rootContainer)
    const workspaceToSave = { panes, projectPaths }

    const savedWorkspaces = atom.config.get('save-workspace-Workspaces') || {}
    savedWorkspaces[workspaceName] = workspaceToSave

    atom.config.set('save-workspace-Workspaces', savedWorkspaces)
  }

  remove (workspaceName) {
    let model = atom.config.get('save-workspace-Workspaces')
    if ( workspaceName in model) {
      delete model[workspaceName]
      atom.config.set('save-workspace-Workspaces', model)
    }
  }

  __isOldClient (workspace) {
    return workspace.tabs
  }

  __openPanes (panes) {
    for (const i in panes) {
      const subPane = panes[i]

      if (subPane.type === PANE) {
        this.__openPane(subPane, i)
      } else if (subPane.type === PANE_AXIS) {
        this.__openPaneAxis(subPane, i)
      }
    }
  } 

  __closeAllPanes (paneContainer) {
    const ref = paneContainer.getPanes()
    for (let i = 0; i < ref.length; i++) {
      const pane = ref[i]
      pane.destroyItems()
    }

    paneContainer.destroy()
  }

  __openTabs (tabs) {
    for (const uri of tabs) {
      atom.workspace.open(uri, {})
    }
  }

  __openPane (pane, paneIndex) {
    if (paneIndex != '0' && pane.data.orientation) {
      const activePane = atom.workspace.paneContainer.getActivePane()
      this.__splitAndActivate(activePane, pane.data.orientation, 'vertical')
    }

    atom.workspace.paneForURI(pane.data.tabs[0])
    this.__openTabs(pane.data.tabs)
  }

  __openPaneAxis (paneAxis, paneIndex) {
    if (paneIndex !== '0') {
      this.__splitPane(paneAxis)
    }

    for (const i in paneAxis.data) {
      const subPane = paneAxis.data[i]

      if (subPane.type === PANE) {
        this.__openPane(subPane, i)
      } else if (subPane.type === PANE_AXIS) {
        this.__openPaneAxis(subPane, i)
      }
    }
  }

  __splitPane (paneAxis) {
    const activePane = atom.workspace.paneContainer.getActivePane()
    this.__splitAndActivate(activePane, paneAxis.orientation, 'horizontal')
  }

  __splitAndActivate (activePane, orientation, downSplitDirection) {
    const isSplitDown = orientation === downSplitDirection
    const newPane = isSplitDown ? activePane.splitDown() : activePane.splitRight()
    newPane.activate()
  }
}

@@ -269,30 +269,35 @@
 ainer = 
-atom.w
+this.__getW
 orkspace
 .paneCon
@@ -280,37 +280,36 @@
 s.__getWorkspace
-.pane
+Root
 Container.getRoo
@@ -297,32 +297,24 @@
 ootContainer
-.getRoot
 ()%0A    %0A    
@@ -650,30 +650,35 @@
 ner   = 
-atom.w
+this.__getW
 orkspace
 .paneCon
@@ -661,37 +661,36 @@
 s.__getWorkspace
-.pane
+Root
 Container.getRoo
@@ -686,16 +686,8 @@
 iner
-.getRoot
 ()%0A 
@@ -1220,24 +1220,270 @@
 %0A    %7D%0A  %7D%0A%0A
+  __getWorkspaceRootContainer () %7B%0A    const centerWorkspace = atom.workspace.getCenter()%0A    const workspacePaneContainer = centerWorkspace.paneContainer%0A%0A    const rootContainer = workspacePaneContainer.getRoot()%0A%0A    return rootContainer%0A  %7D%0A%0A
   __isOldCli
@@ -2226,38 +2226,24 @@
 m.workspace.
-paneContainer.
 getActivePan
@@ -2807,24 +2807,24 @@
 paneAxis) %7B%0A
+
     const ac
@@ -2849,30 +2849,16 @@
 rkspace.
-paneContainer.
 getActiv

replace deprecated method for getting center workspace