import { ConnectionManager, TaskRegistry } from '@things-factory/integration-base'
import { access } from '@things-factory/utils'
import { getTrackingWorkspace } from './get-tracking-workspace'

const debug = require('debug')('aidoop:robotics:ws-get-roi-state')

async function TrackingWorkspaceGetROIState(step, { logger, data, domain }) {
  var {
    connection,
    params: { accessor, cameraName }
  } = step

  var object = ConnectionManager.getConnectionInstanceByName(domain, connection) || {}
  if (!object) {
    throw new Error(`no connection : ${connection}`)
  }

  var roiName = accessor ? access(accessor, data) : cameraName
  logger.info(`accessor: ${accessor}, cameraName: ${cameraName}, roiName: ${roiName}`)

  var workspace = getTrackingWorkspace(domain)
  var { engine } = workspace
  var { trackingStorage } = engine

  return {
    data: trackingStorage.getROIState(roiName)
  }
}

TrackingWorkspaceGetROIState.parameterSpec = [
  {
    type: 'scenario-step-input',
    label: 'accessor',
    name: 'accessor'
  },
  {
    type: 'string',
    label: 'camera name',
    name: 'cameraName'
  }
]

TrackingWorkspaceGetROIState.help = 'robotics/task/tracking-workspace-get-roi-state'

TaskRegistry.registerTaskHandler('tracking-ws-get-roi-state', TrackingWorkspaceGetROIState)
