import gql from 'graphql-tag'
import { client } from '@things-factory/shell'

export async function getRobotArmStatus(name) {
  const response = await client.query({
    query: gql`
      query($name: String!) {
        robotArmGetStatus(name: $name) {
          running
          ready
          busy
          home
          zero
          teachingMode
          error
          collided
          emergency
        }
      }
    `,
    variables: {
      name
    }
  })

  console.log('detected status', response.data?.robotArmGetStatus)
  return response.data?.robotArmGetStatus
}

export async function getRobotArmPose(name) {
  const response = await client.query({
    query: gql`
      query($name: String!) {
        robotArmGetPose(name: $name) {
          x
          y
          z
          u
          v
          w
        }
      }
    `,
    variables: {
      name
    }
  })

  console.log('detected pose', response.data?.robotArmGetPose)
  return response.data?.robotArmGetPose
}

export async function getRobotArmJoints(name) {
  const response = await client.query({
    query: gql`
      query($name: String!) {
        robotArmGetJoints(name: $name) {
          j1
          j2
          j3
          j4
          j5
          j6
        }
      }
    `,
    variables: {
      name
    }
  })

  console.log('detected joints', response.data?.robotArmGetJoints)
  return response.data?.robotArmGetJoints
}

export async function startTeachingMode(name) {
  const response = await client.mutate({
    mutation: gql`
      mutation($name: String!) {
        robotArmStartTeachingMode(name: $name)
      }
    `,
    variables: {
      name
    }
  })

  return response.data?.robotArmStartTeachingMode
}

export async function finishTeachingMode(name) {
  const response = await client.mutate({
    mutation: gql`
      mutation($name: String!) {
        robotArmFinishTeachingMode(name: $name)
      }
    `,
    variables: {
      name
    }
  })

  return response.data?.robotArmFinishTeachingMode
}

export async function robotArmTaskMoveTo(name, pose) {
  const response = await client.mutate({
    mutation: gql`
      mutation($name: String!, $pose: PoseInput!) {
        robotArmTaskMoveTo(name: $name, pose: $pose) {
          x
          y
          z
          u
          v
          w
        }
      }
    `,
    variables: {
      name,
      pose
    }
  })

  return response.data?.robotArmTaskMoveTo
}

export async function robotArmJointMoveTo(name, joints) {
  const response = await client.mutate({
    mutation: gql`
      mutation($name: String!, $joints: JointsInput!) {
        robotArmJointMoveTo(name: $name, joints: $joints) {
          j1
          j2
          j3
          j4
          j5
          j6
        }
      }
    `,
    variables: {
      name,
      joints
    }
  })

  return response.data?.robotArmJointMoveTo
}

export async function robotArmSetHome(name, joints) {
  const response = await client.mutate({
    mutation: gql`
      mutation($name: String!, $joints: JointsInput!) {
        robotArmSetHome(name: $name, joints: $joints) {
          j1
          j2
          j3
          j4
          j5
          j6
        }
      }
    `,
    variables: {
      name,
      joints
    }
  })

  return response.data?.robotArmSetHome
}

export async function robotArmReset(name) {
  const response = await client.mutate({
    mutation: gql`
      mutation($name: String!) {
        robotArmReset(name: $name)
      }
    `,
    variables: {
      name
    }
  })

  return response.data?.robotArmReset
}

export async function robotArmGoHome(name) {
  const response = await client.mutate({
    mutation: gql`
      mutation($name: String!) {
        robotArmGoHome(name: $name)
      }
    `,
    variables: {
      name
    }
  })

  return response.data?.robotArmGoHome
}
