import { ConnectionManager } from '@things-factory/integration-base'
import { ROBOTICS_OBJECT_TYPES } from '../../../controllers/robotics-types'

export const Query = {
  async trackingWorkspace(_: any, { name }, context: any) {
    const { domain } = context.state

    var connection = ConnectionManager.getConnectionInstanceByName(domain, name)
    if (!connection || connection.discriminator !== ROBOTICS_OBJECT_TYPES.WORKSPACE) {
      throw Error(`TrackingWorkspace '${name}' Not Found`)
    }

    return connection
  },

  async trackingWorkspaces(_: any, {}, context: any) {
    const { domain } = context.state

    var items = Object.keys(ConnectionManager.getConnectionInstances(domain))
      .map(name => ConnectionManager.getConnectionInstanceByName(domain, name))
      .filter(connection => connection.discriminator == ROBOTICS_OBJECT_TYPES.WORKSPACE)

    return {
      items,
      total: items.length
    }
  }
}

export const TrackingWorkspace = {
  detectionMethod(workspace) {
    return workspace.params?.detectionMethod
  },
  checkVideoStream(workspace) {
    return workspace.params?.checkVideoStream
  },

  // TODO workspace에 해당하는 데이타들만 가져와야 한다.
  async trackingCameras(workspace, _, context) {
    const { domain } = workspace

    return Object.keys(ConnectionManager.getConnectionInstances(domain))
      .map(name => ConnectionManager.getConnectionInstanceByName(domain, name))
      .filter(connection => connection.discriminator == ROBOTICS_OBJECT_TYPES.CAMERA)
      .map(connection => {
        var { handEyeConfig, handEyeMatrix, cameraCalibration, rois, camObjOffset, width, height } = connection.params || {}
        var { cameraMatrix, distortionCoefficient } = cameraCalibration || {}

        return {
          ...connection,
          cameraMatrix,
          distortionCoefficient,
          handEyeConfig,
          handEyeMatrix,
          rois,
          camObjOffset,
          width,
          height
        }
      })
  },
  async trackableObjects(workspace, _, context) {
    const { domain } = workspace

    return Object.keys(ConnectionManager.getConnectionInstances(domain))
      .map(name => ConnectionManager.getConnectionInstanceByName(domain, name))
      .filter(connection => connection.discriminator == ROBOTICS_OBJECT_TYPES.OBJECT)
      .map(connection => {
        var { poiOffset } = connection.params || {}

        return {
          ...connection,
          poiOffset
        }
      })
  },
  async robotArms(workspace, _, context) {
    const { domain } = workspace

    return Object.keys(ConnectionManager.getConnectionInstances(domain))
      .map(name => ConnectionManager.getConnectionInstanceByName(domain, name))
      .filter(connection => connection.discriminator == ROBOTICS_OBJECT_TYPES.ROBOT_ARM)
      .map(connection => {
        var { markerOffset, toolOffset } = connection.params || {}

        return {
          ...connection,
          markerOffset,
          toolOffset
        }
      })
  }
}

export const Domain = {
  async trackingWorkspaces(domain) {
    return Object.keys(ConnectionManager.getConnectionInstances(domain))
      .map(name => ConnectionManager.getConnectionInstanceByName(domain, name))
      .filter(connection => connection.discriminator == ROBOTICS_OBJECT_TYPES.WORKSPACE)
  }
}
