<template>
  <div :class="['x-direction-section-camera', direction ? 'out' : 'in']">
    <div
      v-for="(item, x) in cameras"
      :key="x"
      :style="item.style"
      class="x-camera"
      ref="cameraElements"
    >
      <div class="video-view">
        <VideoPlayer
          v-if="showCameras.includes(x)"
          v-bind="{
            frameW: 368,
            frameH: 234,
            vcode: item.cameraIndexCode,
            pileNumber: item.pileNumber
          }"
        />
      </div>
      <div class="footer">
        <div class="pile">{{ item.pileNumber }}</div>
        <!-- <div class="speed">平均车速：80km/h</div> -->
      </div>

      <div @click="openVideo(item)" class="full-screen"></div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getCurrentInstance } from 'vue'
import VideoPlayer from '@/views/components/VideoPlayer/index.vue'

// store
const app = getCurrentInstance()

// 事件
const emits = defineEmits(['camera-change', 'click-trigger'])

// 参数
const props = withDefaults(
  defineProps<{
    traffic: any
    tunnel: any
    direction: number
  }>(),
  {}
)

const cameraElements = ref<HTMLElement[]>([])
// const observer = ref<IntersectionObserver | null>(null)
const containerEl = ref<HTMLElement | null>(null)
const showCameras = ref<number[]>([])

// 处理坐标偏移
const adjustXPositions = (points: { x: number }[]) => {
  const adjustedPoints = [...points];
  let adjusted = true;

  // 持续调整，直到所有相邻点的间隔都不小于 368
  while (adjusted) {
    adjusted = false;
    for (let i = 1; i < adjustedPoints.length; i++) {
      const prevPoint = adjustedPoints[i - 1];
      const currentPoint = adjustedPoints[i];
      const gap = currentPoint.x - prevPoint.x;
      if (gap < 370) {
        const offset = (370 - gap) / 2;
        prevPoint.x -= offset;
        currentPoint.x += offset;
        adjusted = true;
      }
    }
  }

  return adjustedPoints;
};

// 路面摄像头铺设
const cameras = computed(() => {
  if (!props.traffic) return []
  const cameraList = props.traffic.cameraFormate(props.tunnel.cameras, props.direction)
  const adjustedList = adjustXPositions(cameraList);
  return adjustedList.map((item) => {
    return {
      ...item,
      style: {
        left: item.x + 'px'
      },
      isVisible: false // 初始不可见
    }
  })
})

const openVideo = (item) => {
  app?.proxy?.$openVideo(item)
}

const checkVisibility = () => {
  containerEl.value = document.querySelector('.road-scroll')
  if (!containerEl.value) return

  const containerRect = containerEl.value.getBoundingClientRect()

  const showList: number[] = []
  cameraElements.value.forEach((el, index) => {
    const elRect = el.getBoundingClientRect()
    const isVisible = elRect.right > containerRect.left && elRect.left < containerRect.right
    isVisible && showList.push(index)
  })
  showCameras.value = [...showList]
}

defineExpose({
  checkVisibility
})
</script>

<style lang="scss" scoped>
$base-url: '@/assets/images/home/';
$base-common: '@/assets/images/common/';

.x-direction-section-camera {
  position: relative;
  //   left: 0;
  width: 100%;
  height: 256px;
  margin: 27px 0 15px;
  // z-index: 2;

  .x-camera {
    width: 368px;
    height: 256px;
    cursor: pointer;
    position: absolute;
    transform: translateX(-50%);

    .video-view {
      position: relative;
      height: 234px;
      box-sizing: border-box;
      border: 1px solid #8eb8d1;
      border-bottom: none;
      border-radius: 6px 6px 0 0;
      overflow: hidden;
    }
    .footer {
      height: 24px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      // position: absolute;
      padding: 3px 8px;
      box-sizing: border-box;
      // left: 0;
      // bottom: 0;
      background: #35669c;
      font-size: 12px;
      font-weight: 600;
      line-height: 18px;
      color: rgba(240, 245, 255, 0.95);
      border-radius: 0 0 6px 6px;
    }
  }

  .full-screen {
    width: 20px;
    height: 20px;
    background: url($base-common + 'full-screen.png') no-repeat;
    background-size: cover;
    position: absolute;
    right: 8px;
    bottom: 40px;
    transition-property: all;
    transition-duration: 0.2s;
    z-index: 3;

    &:hover {
      cursor: pointer;
      box-shadow: 0 0 10px #aeaeae;
    }
  }
}
</style>
