<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(),
    };
  },
  watch: {
    raw_msg: {
      handler(val, oldVal) {
        if (val) {
          console.log(val, "val");
        }
      },
      deep: true,
    },
  },
  created() {
    console.log(1111);

    window.addEventListener("message", this.onDebugInfo);
  },
  mounted() {
    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) {
      console.log(e.type, "e.type");
      switch (e.type) {
        case "qoeinfo":
        case "raw_msg":
          console.log(e, "MMCPlayer");
          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>