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

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

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

    var { handEyeConfig, handEyeMatrix, cameraCalibration, rois, camObjOffset, width, height } = connection.params || {}
    var { cameraMatrix, distortionCoefficient } = cameraCalibration || {}

    return {
      ...connection,
      cameraMatrix,
      distortionCoefficient,
      handEyeConfig,
      handEyeMatrix,
      rois,
      camObjOffset,
      width,
      height
    }
  }
}

export const TrackingCamera = {
  async baseRobotArm(trackingCamera) {
    const { domain } = trackingCamera
    const { baseRobotArm } = trackingCamera.params || {}

    var baseRobotArmObj = ConnectionManager.getConnectionInstanceByName(domain, baseRobotArm)
    if (!baseRobotArmObj || baseRobotArmObj.discriminator !== ROBOTICS_OBJECT_TYPES.ROBOT_ARM) {
      return
    }

    return baseRobotArmObj
  }
}
