import paper from 'paper'

export default {
  setTask: (state, task) => {
    state.task = task
  },

  activateSnackbar: (state, payload) => {
    state.snackbar.active = true
    state.snackbar.text = payload.text ? payload.text : 'notification'
    state.snackbar.color = payload.color ? payload.color : 'info'
    state.snackbar.timeout = payload.timeout ? payload.timeout : 4000
  },

  dismissSnackbar: state => {
    state.snackbar.active = false
  },

  // Synchronise the position and scale of both the openseadragon image and the
  // annotation project.
  synchroniseAnnotationAndOSDCanvas: (state, viewer) => {
    viewer.addHandler('animation', () => {
      // Sync Zoom
      const currentZoom = viewer.viewport.getZoom(true)
      const baseImage = viewer.world.getItemAt(0)
      paper.view.zoom = baseImage.viewportToImageZoom(currentZoom)

      // Sync Center
      const center = baseImage.viewportToImageCoordinates(
        viewer.viewport.getCenter(true)
      )
      paper.view.center = new paper.Point(center.x, center.y)

      // Update path stroke widths.
      // If the current zoom level does not equal the target zoom level then we
      // need to scale the paths in the project to match the zoom changes.
      if (viewer.viewport.getZoom(true) !== viewer.viewport.getZoom()) {
        // Update paths to have strokeWidth reactive to zoom level.
        const strokeScale = state.strokeScale
        paper.project.getItems({ class: paper.Path }).forEach(path => {
          path.strokeWidth = (baseImage.getContentSize().x * strokeScale) / (currentZoom * 1000)

          // Ruler items need special treatment to re-draw the labels
          if (path.hasOwnProperty('drawLabel')) path.drawLabel()
        })
      }
    })

    viewer.addHandler('resize', function (e) {
      const baseImage = viewer.world.getItemAt(0)

      // Sync Size
      paper.view.viewSize.width = e.newContainerSize.x
      paper.view.viewSize.height = e.newContainerSize.y

      // Sync Center
      const center = baseImage.viewportToImageCoordinates(
        viewer.viewport.getCenter(true)
      )
      paper.view.center = new paper.Point(center.x, center.y)
    })
  },

  setActiveStep: (state, stepIndex) => {
    state.activeStep = stepIndex
  },

  setActiveLayer: (state, layerIndex) => {
    state.activeLayer = layerIndex
  },

  toggleSettings: state => {
    state.settingsFlag = !state.settingsFlag
  },

  setActiveValidationIndex: (state, payload) => {
    state.activeValidationIndex = payload
  },

  setSteps: state => {
    state.toolsDrawer = false
    state.studioDrawer = false
    if (state.task === 'review') {
      state.steps = [{
        id: 1,
        instruction: 'Review the data',
        type: 'review'
      }]
    } else if (state.task === 'validate') {
      state.steps = [{
        id: 1,
        instruction: 'Cycle through each of the predicted Megakaryocyte classifications and verify whether the prediction is correct or incorrect. In cases where you are not certain, flag the prediction for review.',
        type: 'validate'
      }]
    } else {
      state.toolsDrawer = true
      state.studioDrawer = true
      state.steps = null
    }
  },

  toggleToolsDrawer: state => {
    state.toolsDrawer = !state.toolsDrawer
  },

  toggleStudioDrawer: state => {
    state.studioDrawer = !state.studioDrawer
  },

  setToolsDrawerState: (state, payload) => {
    state.toolsDrawer = payload
  },

  setStudioDrawerState: (state, payload) => {
    state.studioDrawer = payload
  }
}

@@ -2539,27 +2539,84 @@
 tSteps: 
+(
 state
+, steps)
  =%3E %7B%0A
+    if (steps) state.steps = steps%0A    else %7B%0A  
     stat
@@ -2637,24 +2637,26 @@
 = false%0A    
+  
 state.studio
@@ -2666,24 +2666,26 @@
 wer = false%0A
+  
     if (stat
@@ -2703,24 +2703,26 @@
 'review') %7B%0A
+  
       state.
@@ -2732,39 +2732,43 @@
 ps = %5B%7B%0A        
+  
 id: 1,%0A
+  
         instruct
@@ -2791,16 +2791,18 @@
  data',%0A
+  
         
@@ -2814,35 +2814,39 @@
  'review'%0A      
+  
 %7D%5D%0A
+  
     %7D else if (s
@@ -2869,24 +2869,26 @@
 alidate') %7B%0A
+  
       state.
@@ -2898,39 +2898,43 @@
 ps = %5B%7B%0A        
+  
 id: 1,%0A
+  
         instruct
@@ -3138,24 +3138,26 @@
 .',%0A        
+  
 type: 'valid
@@ -3167,23 +3167,27 @@
 '%0A      
+  
 %7D%5D%0A    
+ 
+ 
 %7D else %7B
@@ -3179,32 +3179,34 @@
 %0A      %7D else %7B%0A
+  
       state.tool
@@ -3212,32 +3212,34 @@
 lsDrawer = true%0A
+  
       state.stud
@@ -3252,32 +3252,34 @@
 er = true%0A      
+  
 state.steps = nu
@@ -3281,16 +3281,24 @@
  = null%0A
+      %7D%0A
     %7D%0A  

Allow steps to be explicity set