
import { bus, trackId, sceneCheck } from '@/views/3DModel/state'

let env = null

// 设置追踪模式基本信息
const enviromentSet = () => {

  const context = env.getContext()
  context.controls.maxPolarAngle = 360

}

// 车辆追踪
const carTracking = (xCar) => {

  if (!sceneCheck('track')) {
    return
  }

  env.removeControls()

  const cars = env.getCars()
  const context = env.getContext()

  const preCar = cars.find(item => item.infos.id === trackId.value)


  if (preCar) {
    preCar.stopFollow('change')
  }

  const newCar = cars.find(item => item.infos.id === xCar.carId)

  if (newCar && !newCar.over_miles) {
    newCar.follow(context.camera)
    trackId.value = xCar.carId
  } else {
    context.camera.position.set(-25, 15, 0)
    context.camera.setLook(50, 10, 0)
    ElMessage.warning('车辆已驶离隧道！')
  }

}

// 俯瞰模式初始化
const trackSet = (provides) => {

  env = { ...provides }

  enviromentSet()

  // 车辆追踪
  bus.on('3d-track', e => {
    console.log('aaa1---')
    carTracking(e)
  })

}

export {
  trackSet
}