<template>
  <div class="qingliu">
    <div ref="player_area"></div>
  </div>
</template>

<script>
import { dataURLToBlob, canvasToDataURL } from "../../../../lib/image-tool";

export default {
  name: "QingLiuPlayer",
  props: {
    data: Object,
    raw_msg: Number,
    infoflag: Boolean,
  },
  data() {
    return {
      name: 'QingLiuPlayer' + Date.now(),
    };
  },
  created(){
    window.addEventListener("message", this.onDebugInfo);
  },
  mounted() {
    console.log(this.raw_msg,"raw_msg");
    window.onresize = (e) =>{
      const clientheight =  document.documentElement.clientHeight || document.body.clientHeight
      let isFullScreen = screen.height == clientheight
      if(isFullScreen == true){
        return
      }else{
        this.$emit("close")
      }
    }
    window.qingliu = this.init;
    this.init();
  },
  beforeDestroy() {
    window.kbt_player_destroy(this.name);
    window.removeEventListener("message", this.onDebugInfo);
  },
  methods: {
    /**
     * 初始化
     */
    init() {
      window.kbt_sdk_load().then( () => {
        let parentObj = this.$refs.player_area;
        window.kbt_player_create(
          this.name,
          this.data.vUrl,
          970,
          545,
          parentObj
        );
      });
    },
    screenShot(){
      var canvas = document.querySelector("#sdk_canvas_" + this.name);
      const image = canvasToDataURL(canvas);
      let blob = dataURLToBlob(image);
      return blob
    },
    fullScreen(){
      // var canvas = document.querySelector("#sdk_canvas_" + this.name);
      var canvas = this.$refs.player_area.querySelector('canvas');
      canvas && canvas.requestFullscreen();
    },
    onDebugInfo(e){
      switch(e.type){
        case 'qoeinfo':
        case 'raw_msg':
          this.$emit('debugInfo', e);
          break;
      }
    }
  },
};
</script>

<style scoped lang='scss'>
.qingliu {
  .player_area{
    width: 100%;
    height: 100%;
  }
  .cf{
    position: fixed;
    top: 0;
    left: 0;
    padding: 10px;
    color: #fff;
    background: rgba($color: #000000, $alpha: 0.6);
  }
  // width: 100%;
  // height: 100%;
  ::v-deep canvas {
    width: 100% !important;
  }
}
</style>