<template>
  <div class="video-player" id="player"></div>
</template>

<script>
export default {
  name: 'VideoPlayer',
  props: { isSkip: Boolean, video: Object },
  data() {
    return { player: null }
  },
  methods: {
    createPlayer() {
      const _this = this
      const { FD, LD, SD } = this.video
      this.player = new Aliplayer(
        {
          id: 'player',
          source: JSON.stringify({ FD, LD, SD }),
          width: '100%',
          height: '100%',
          autoplay: true,
          isLive: false,
          controlBarVisibility: 'always',
          components: [
            {
              name: 'QualityComponent',
              type: AliPlayerComponent.QualityComponent
            }
          ]
        },
        function(player) {
          player.on('sourceloaded', function(params) {
            const paramData = params.paramData
            const desc = paramData.desc
            const definition = paramData.definition
            player
              .getComponent('QualityComponent')
              .setCurrentQuality(desc, definition)
          })
          player.on('timeupdate', function(event) {
            _this.$emit('timeupdate', player.getCurrentTime())
          })
        }
      )
    }
  },
  mounted() {
    this.createPlayer()
  },
  beforeDestroy() {
    this.player && this.player.dispose()
  }
}
</script>

<style lang="scss" scoped>
.video-player {
  width: 100%;
  height: 100%;
}
</style>
