<template>
  <div class="hello-ezuikit-js">
    <div id="video-content">
      <div id="video-container" style="width:600px;height:400px"></div>
      <!-- <div>
        <button v-on:click="stop">stop</button>
        <button v-on:click="play">play</button>
        <button v-on:click="openSound">openSound</button>
        <button v-on:click="closeSound">closeSound</button>
        <button v-on:click="startSave">startSave</button>
        <button v-on:click="stopSave">stopSave</button>
        <button v-on:click="capturePicture">capturePicture</button>
        <button v-on:click="fullScreen">fullScreen</button>
        <button v-on:click="getOSDTime">getOSDTime</button>
        <button v-on:click="ezopenStartTalk">开始对讲</button>
        <button v-on:click="ezopenStopTalk">结束对讲</button>
        <button v-on:click="destroy">销毁</button>
      </div> -->
      <div class="btn-close" @click="handleClose"></div>

    </div>
  </div>
</template>

<script setup>
import { onMounted, watch } from 'vue'
import { GetVideoToken } from '@/apis/hdsj'
import EZUIKit from "ezuikit-js"
var player = null

let accessToken
let videoId = ''

onMounted(() => {
  getToken()
})

const props = defineProps(['videoId'])

watch(()=>props.videoId, (val) => {
  if(val) {
    console.log('视频id:',val)
    videoId = val
    initPlayer()
    setTimeout(() => {
      play()
    }, 1000);
  }
})

// 获取播放的token权限
function getToken() {
  GetVideoToken().then(res => {
    console.log('播放权限获取成功')
    accessToken = res
  }).catch((e)=> {
    console.log('播放权限获取失败:' + e.msg)
  })
}

// 初始化播放器
function initPlayer() {
  player = new EZUIKit.EZUIKitPlayer({
    id: 'video-container', // 视频容器ID
    accessToken,
    // url: `ezopen://open.ys7.com/G39444019/1.live`,
    url: `ezopen://open.ys7.com/${videoId}/1.live`,
    // simple - 极简版; pcLive-pc直播；pcRec-pc回放；mobileLive-移动端直播；mobileRec-移动端回放;security - 安防版;voice-语音版;
    // template: 'pcLive',
    // plugin: ['talk'], // 加载插件，talk-对讲
    width: 600,
    height: 400,
  });
  window.player = player;
  document.getElementById('video-container-loading-id-0').style.height = '100%'
}

const emit = defineEmits(['close'])

// 关闭
function handleClose() {
  if(player) {
    destroy()
  }
  emit('close')
}

function play() {
  var playPromise = player.play();
  playPromise.then((data) => {
    console.log("promise 获取 数据", data)
  })
}
/* function stop() {
  var stopPromise = player.stop();
  stopPromise.then((data) => {
    console.log("promise 获取 数据", data)
  })
}
function getOSDTime() {
  var getOSDTimePromise = player.getOSDTime();
  getOSDTimePromise.then((data) => {
    console.log("promise 获取 数据", data)
  })
}
function capturePicture() {
  var capturePicturePromise = player.capturePicture(`${new Date().getTime()}`);
  capturePicturePromise.then((data) => {
    console.log("promise 获取 数据", data)
  })
}
function openSound() {
  var openSoundPromise = player.openSound();
  openSoundPromise.then((data) => {
    console.log("promise 获取 数据", data)
  })
}
function closeSound() {
  var openSoundPromise = player.closeSound();
  openSoundPromise.then((data) => {
    console.log("promise 获取 数据", data)
  })
}
function startSave() {
  var startSavePromise = player.startSave(`${new Date().getTime()}`);
  startSavePromise.then((data) => {
    console.log("promise 获取 数据", data)
  })
}
function stopSave() {
  var stopSavePromise = player.stopSave();
  stopSavePromise.then((data) => {
    console.log("promise 获取 数据", data)
  })
}
function ezopenStartTalk() {
  player.startTalk();
}
function ezopenStopTalk() {
  player.stopTalk();
}
function fullScreen() {
  player.fullScreen();
} */
function destroy() {
  var destroyPromise = player.destroy();
  destroyPromise.then((data) => {
    console.log("promise 获取 数据", data)
  })
}

</script>

<style lang="scss" scoped>
.hello-ezuikit-js {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba($color: #000000, $alpha: 0.8);
  z-index: 1;
}

#video-content {
  position: absolute;
  top: 48%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}

.btn-close {
  display: block;
  margin: 30px auto 0;
  width: 30px;
  height: 30px;
  background: url('@/assets/imgs/page_kkjk/bi-pics-close.png') no-repeat;
  background-size: contain;
  cursor: pointer;
}
</style>