// ==UserScript==
// @name         Ender
// @namespace    EyeWire II
// @version      0.1
// @description  Allows quickly adding annotations at mouse pointer
// @author       Krzysztof Kruk
// @match        https://spelunker.cave-explorer.org/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=spelunker.cave-explorer.org
// @grant        none
// @updateURL    https://raw.githubusercontent.com/ChrisRaven/Ender-for-Eyewire-II/main/Ender.user.js
// @downloadURL  https://raw.githubusercontent.com/ChrisRaven/Ender-for-Eyewire-II/main/Ender.user.js
// ==/UserScript==

/* global viewer */


(function() {
  'use strict'

  const color = 'green'

  document.addEventListener('mouseup', (e) => {
    if (e.button !== 0 || !e.altKey) return
    if (!e.target.classList.contains('neuroglancer-rendered-data-panel')) return

    function createAnnotation() {
      const annotations = annotationLayer.layer_.localAnnotations.toJSON()
      const scales = [4, 4, 1]
      const position = Array.from(viewer.mouseState.position).map((el, i) => el * scales[i])
      annotations.push({id: crypto.randomUUID(), point: position, type: 'point'})
      annotationLayer.layer_.localAnnotations.restoreState(annotations)
    }

    let annotationLayer = viewer.layerManager.getLayerByName('True End 🟢')

    if (annotationLayer) {
      createAnnotation()
    }
  })

})()