import { reactive, computed } from 'vue'
import { useTunnelStoreWithOut } from '@/store/modules/tunnel'

// store
const tunnelStore = useTunnelStoreWithOut()

// 参数
const params = reactive({

  // 上行线
  uproad: {
    focus: null,
    // cameras: [0, 4, 12, 14, 15],
    cameras: [0, 3, 6, 11, 14],
    allCameras: []
  },

  // 下行线
  downroad: {
    focus: null,
    cameras: [1, 5, 8, 14, 16],
    allCameras: []
  }

})

const setCameras = (xcamera, direction) => {

  const key = direction ? 'downroad' : 'uproad'

  if (params[key].focus === null ) return

  if (!params[key].allCameras.length) {

    const { cameras } = [
      tunnelStore.getUpTunnel,
      tunnelStore.getDownTunnel
    ][direction]

    params[key].allCameras = cameras

  }

  const x = params[key].allCameras.findIndex(item => item.id === xcamera.id)

  if (x === -1) return
  
  if (params[key].focus !== null) {
    params[key].cameras.splice(params[key].focus, 1, x)
  }
  
}

const setFocus = (focus, direction) => {
  const key = direction ? 'downroad' : 'uproad'
  params[key].focus = focus
}

const getCameras = (direction) => {
  const key = direction ? 'downroad' : 'uproad'
  return params[key].cameras
}

const getFocus = (direction) => {
  const key = direction ? 'downroad' : 'uproad'
  return params[key].focus
}

const getRealCameras = (direction) => {
  
  const { cameras } = [
    tunnelStore.getUpTunnel,
    tunnelStore.getDownTunnel
  ][direction]

  const key = direction ? 'downroad' : 'uproad'

  return params[key].cameras.map(x => cameras[x]).filter(item => item)


}

export {
  setFocus,
  setCameras,
  getFocus,
  getCameras,
  getRealCameras
}