import { Arg, Ctx, Mutation, Resolver } from 'type-graphql'

import { ConnectionManager } from '@things-factory/integration-base'

import { ROBOTICS_OBJECT_TYPES } from '../../controllers/robotics-types'
import { TrackableObject, TrackableObjectStateInput } from './trackable-object-type'

// extend type Mutation {
//   updateTrackableObjectState(name: String!, state: TrackableObjectStateInput!): TrackableObject
// }

@Resolver(TrackableObject)
export class TrackableObjectMutation {
  @Mutation(returns => TrackableObject, { description: 'To start roi configuration and get the result' })
  async updateTrackableObjectState(
    @Arg('name', type => String, { nullable: false }) name: string,
    @Arg('state', type => TrackableObjectStateInput, { nullable: false }) state: TrackableObjectStateInput,
    @Ctx()
    context: any
  ): Promise<TrackableObject> {
    const { domain } = context.state

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

    connection.state = state

    return {
      ...connection
    }
  }
}
