import bus from '@/utils/eventBus'

const listAppear = ref(true) // 列表展示
const spaceAppear = ref(true) // 空间轴展示

const roamkey = ref(null) // 漫游 key

const cars = ref([[], []]) // 车流
const trackId = ref(null) // 追踪车辆 id

const events = ref([[], []]) // 事件

const locatId = ref(null) // 定位事件
const locating = ref(null) // 定位事件状态
const locatPoints = ref([]) // 事件桩点模型缓存


const trackCar = computed(() => {

  if (!trackId) return null

  const xcar = cars.value.flat().find(item => item.id === trackId.value)

  if (!xcar) {
    return null
  }

  if (xcar.over_miles) {
    trackId.value = null
    return null
  }

  return xcar

})

watch(
  () => trackId.value,
  val => {
    listAppear.value = !Boolean(val)
  }
)

// 内存校验
const sceneCheck = (key) => {
  
  if (trackId.value) {
    if (key !== 'track') {
      ElMessage.warning('车辆追踪中，请稍后！')
      return false
    }
  }

  if (roamkey.value) {
    if (key !== 'roam') {
      ElMessage.warning('隧道漫游中，请稍后！')
      return false
    }
  }

  if (locatId.locating) {
    if (key !== 'locate') {
      ElMessage.warning('事件定位中，请稍后！')
      return false
    }
  }

  return true

}

const clearState = () => {
  listAppear.value = true
  spaceAppear.value = true
  roamkey.value = null
  cars.value = [[], []]
  trackId.value = null
  locating.value = null
  locatPoints.value = ref([])
}


export {
  listAppear,
  spaceAppear,
  trackId,
  cars,
  trackCar,
  roamkey,
  bus,
  events,
  locatId,
  locating,
  locatPoints,
  sceneCheck,
  clearState
}

