<template>
  <div v-if="addrList.length > 0">
    <div class="vent-flex-row-wrap camera-box" >
      <div v-for="(item, index) in addrList" :key="index" class="player-box">
        <div class="player-name">{{ item.name }}</div>
        <div>
          <template v-if="item.addr.startsWith('rtsp://')">
            <video :id="`video${index}`" muted autoplay></video>
            <div class="click-box" @dblclick="goFullScreen(`video${index}`)"></div>
          </template>
          <template v-else>
            <div :id="'player'+index" ></div>
          </template>
        </div>
      </div>
    </div>
    <div class="pagination">
      <Pagination v-model:current="current" :total="total" show-less-items @change="onChange"/>
    </div>
  </div>
  <div class="camera-box" v-else>
    <Empty />
  </div>
  
</template>
<script lang="ts" setup>
  import {onMounted, onUnmounted, ref } from 'vue';
  import { Pagination, Empty } from 'ant-design-vue';
  import { list, cameraAddr } from './camera.api'
  import Player from 'xgplayer';
  import HlsPlugin from 'xgplayer-hls';
  import FlvPlugin from 'xgplayer-flv';
  import 'xgplayer/dist/index.min.css';

  const pageSize = 8
  const current = ref(1)
  const total = ref(0)
  const playerList = ref([])
  const webRtcServerList = <any[]>[]
  let addrList = ref<{ name: string, addr: string }[]>([])

  async function getVideoAddrs(){
    clearCamera();
    playerList.value = []
    const result = await list({ pageSize: pageSize, pageNo: current.value })
    if(result && result.records && result.records.length > 0){
      total.value = result['total']
      const cameraList = <{ name: string, addr: string }[]>[]
      const cameras = result.records
      // const camerasArr: [] = [
      //   {
      //     name: '1111',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: '2222',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: '3333',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: '4444',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: '5555',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: '6666',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: '7777',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: '8888',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: '9999',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: 'aaaa',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: 'bbbb',
      //     devicekind: 'toHKRtsp',
      //   },
      //   {
      //     name: 'cccc',
      //     devicekind: 'toHKRtsp',
      //   },
      // ];
      // const cameras = []
      // for (let index = (current.value - 1)*pageSize; index < current.value * pageSize && index < camerasArr.length ; index++) {
      //   cameras.push(camerasArr[index]) ;
      // }

      for (let i = 0; i < cameras.length; i++) {
        const item = cameras[i];
        
        if (item['devicekind'] === 'toHKRtsp') {
          // 从海康平台接口获取视频流
          const data = await cameraAddr({ cameraCode: item['addr'] });
          if (data && data['url']) {
            cameraList.push({ name: item['name'], addr: data['url'] });
          }
          // cameraList.push({
          //   name: item['name'],
          //   // addr: 'http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8'
          //   addr: 'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.mp4/.m3u8',
          // });
        } else {
          if(item['addr'].includes('0.0.0.0')){
            item['addr'].replace('0.0.0.0', window.location.hostname)
          }
          cameraList.push({ name: item['name'], addr: item['addr'] });
        }
      }
      addrList.value = cameraList
    }
  }

  function onChange(page) {
    current.value = page;
    getVideoAddrs().then(() => {
      getVideo()
    })
  }

  function getVideo() {
    const ip = VUE_APP_URL.webRtcUrl;
    for(let i = 0; i < addrList.value.length; i++){
      const item = addrList.value[i]
      if(item.addr.startsWith('rtsp://')){
        const dom = document.getElementById('video' + i) as HTMLVideoElement
        dom.muted = true;
        dom.volume = 0   
        const webRtcServer = new window['WebRtcStreamer'](dom, location.protocol + ip)
        webRtcServerList.push(webRtcServer)
        webRtcServer.connect(item.addr)
      }else{
        setNoRtspVideo('player'+i,item.addr )
      }
    }
  }

  function setNoRtspVideo(id,videoAddr) {
    const fileExtension = videoAddr.split('.').pop();
    // const plugins = fileExtension === 'flv' ? [ FlvPlugin ] : [ HlsPlugin ];
    if(fileExtension === 'flv'){
      const player = new Player({
        id: id,
        url: videoAddr,
        width:  421,
        height: 292,
        poster: '/src/assets/images/vent/noSinge.png',
        plugins: [ FlvPlugin ] ,
				fluid: true,
				autoplay: true,
				isLive: true,
				playsinline: false,
				screenShot: true,
				whitelist: [''],
				ignores: ['time'],
				closeVideoClick: true,
				customConfig: {
					isClickPlayBack: false
				},
				flv: {
					retryCount: 3, // 重试 3 次，默认值
            retryDelay: 1000, // 每次重试间隔 1 秒，默认值
            loadTimeout: 10000, // 请求超时时间为 10 秒，默认值
            fetchOptions: {
                // 该参数会透传给 fetch，默认值为 undefined
                mode: 'cors'
            },
            targetLatency: 10, // 直播目标延迟，默认 10 秒
            maxLatency:  20, // 直播允许的最大延迟，默认 20 秒
            disconnectTime: 10, // 直播断流时间，默认 0 秒，（独立使用时等于 maxLatency）
            maxJumpDistance: 10,
				}
      });
      playerList.value.push(player)
    }
    if(fileExtension === 'm3u8'){
      let player
      if (document.createElement('video').canPlayType('application/vnd.apple.mpegurl')) {
        // 原生支持 hls 播放
        player = new Player({
          id: id,
          url: videoAddr,
          width: 421,
          height: 292,
          isLive: true,
          autoplay: true,
          autoplayMuted: true,
          cors: true,
          poster: '/src/assets/images/vent/noSinge.png',
          hls: {
            retryCount: 3, // 重试 3 次，默认值
            retryDelay: 1000, // 每次重试间隔 1 秒，默认值
            loadTimeout: 10000, // 请求超时时间为 10 秒，默认值
            fetchOptions: {
                // 该参数会透传给 fetch，默认值为 undefined
                mode: 'cors'
            },
            targetLatency: 10, // 直播目标延迟，默认 10 秒
            maxLatency:  20, // 直播允许的最大延迟，默认 20 秒
            disconnectTime: 10, // 直播断流时间，默认 0 秒，（独立使用时等于 maxLatency）
            maxJumpDistance: 10,
          }
          
        })
      } else if (HlsPlugin.isSupported()) { // 第一步
        player = new Player({
          id: id,
          url: videoAddr,
          width: 421,
          height: 292,
          isLive: true,
          autoplay: true,
          autoplayMuted: true,
          plugins: [HlsPlugin], // 第二步
          poster: '/src/assets/images/vent/noSinge.png',
          hls: {
            retryCount: 3, // 重试 3 次，默认值
            retryDelay: 1000, // 每次重试间隔 1 秒，默认值
            loadTimeout: 10000, // 请求超时时间为 10 秒，默认值
            fetchOptions: {
                // 该参数会透传给 fetch，默认值为 undefined
                mode: 'cors'
            },
            targetLatency: 10, // 直播目标延迟，默认 10 秒
            maxLatency:  20, // 直播允许的最大延迟，默认 20 秒
            disconnectTime: 10, // 直播断流时间，默认 0 秒，（独立使用时等于 maxLatency）
            maxJumpDistance: 10,
          }
        })
      }
      playerList.value.push(player)
    }
  }
  

  function goFullScreen(domId) {
    const videoDom = document.getElementById(domId) as HTMLVideoElement
    if(videoDom.requestFullscreen){
      videoDom.requestFullscreen()
      videoDom.play()
    } else if(videoDom.mozRequestFullscreen){
      videoDom.mozRequestFullscreen()
      videoDom.play()
    } else if (videoDom.webkitRequestFullscreen) {
      videoDom.webkitRequestFullscreen()
      videoDom.play()
    } else if (videoDom.msRequestFullscreen) {
      videoDom.msRequestFullscreen()
      videoDom.play()
    }
  }

  function clearCamera() {
    const num = webRtcServerList.length
    for (let i = 0; i < num; i++) {
      webRtcServerList[i].disconnect()
      webRtcServerList[i] = null
    }
    for(let i = 0; i < playerList.value.length; i++){
      const player = playerList.value[i]
      if(player.destroy)player.destroy()
    }
  }

  onMounted(async() => {
    await getVideoAddrs()
    getVideo()
  })

  onUnmounted(() => {
    clearCamera()
  })

</script>
<style lang="less">
  
  .camera-box{
    height: 700px;
    overflow-y: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    .player-box{
      width: 451px;
      height: 312px;
      padding: 10px;
      background: url('/@/assets/images/vent/camera_bg.png');
      background-size: cover;
      position: relative;
      margin: 10px;
      
      .player-name{
        font-size: 14px;
        position: absolute;
        top: 15px;
        right: 15px;
        color: #fff;
        background-color: hsla(0, 0%, 50%, .5);
        border-radius: 2px;
        padding: 1px 5px;
        max-width: 120px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        z-index: 999;
      }
      .click-box{
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
      }
    }
  }
  .pagination{
    width: 100%;
    height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  // :deep(video){
  //   width: 100% !important;
  //   height: 100% !important;
  //   object-fit: cover !important;
  // }
</style>