<template>
  <!-- FIXME: 通过元素绑定 -->
  <div>
    <video ref="videoPlayer" class="video-js" />
  </div>
</template>
<script>
import videojs from 'video.js';
import 'video.js/dist/video-js.css';
export default {
  name,
  props: {
    sources: {
      type: Array,
      required: true
    },
    options: {
      type: Object
    },
    defaultOptions: {
      type: Object,
      default: () => {
        return {
          autoplay: true,
          muted: true, // 自动播放必须静音
          controls: true,

          fluid: true,
          loop: false,
          preload: 'auto',
          playsinline: true, // 内联播放
          techOrder: ['html5'],

          language: 'zh-CN',
          notSupportedMessage: '此视频暂无法播放，请稍后再试',

          controlBar: {
            fullscreenToggle: false,
            pictureInPictureToggle: false,
            remainingTimeDisplay: false,
            volumePanel: {
              inline: false
            }
          },

          userActions: {
            // 禁止双击全屏
            doubleClick: false
          }
        };
      }
    },
    playsinline: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      player: null
    };
  },
  mounted() {
    this.init();
  },
  beforeDestroy() {
    if (this.player) {
      this.player.dispose();
    }
  },
  methods: {
    init() {
      const options = Object.assign({}, this.defaultOptions, this.options);
      options.sources = this.sources;

      if (options.playsinline) {
        this.$refs.videoPlayer.setAttribute('playsinline', options.playsinline);
        this.$refs.videoPlayer.setAttribute('webkit-playsinline', options.playsinline);

        this.$refs.videoPlayer.setAttribute('x5-playsinline', options.playsinline);
        this.$refs.videoPlayer.setAttribute('x5-video-player-type', 'h5');
        this.$refs.videoPlayer.setAttribute('x5-video-player-fullscreen', false);
      }

      this.player = videojs(this.$refs.videoPlayer, options, function onPlayerReady() {
        this.play();
      });

      this.player.on('play', () => {
        console.log('play');
      });
      this.player.on('pause', () => {
        console.log('pause');
      });
      this.player.on('ended', () => {
        console.log('ended');
      });
      this.player.on('timeupdate', () => {
        console.log('timeupdate');
      });
    },
    play() {
      if (this.player) {
        this.player.play();
      }
    },
    pause() {
      if (this.player) {
        this.player.pause();
      }
    },
    onTouchstart(event) {
      this.touchStartTime = new Date();
      this.touchStart(event);
    },
    onTouchmove(event) {
      this.touchMove(event);
    },
    onTouchend(event) {
      console.log(10, event);
      const { offsetX = 0, offsetY = 0 } = this;
      const deltaTime = new Date() - this.touchStartTime;
      const TAP_TIME = 250;
      const TAP_OFFSET = 10;
      if (offsetX < TAP_OFFSET && offsetY < TAP_OFFSET && deltaTime < TAP_TIME) {
        if (
          event &&
          event.path &&
          event.path[1] &&
          event.path[1].className === 'vjs-big-play-button'
        ) {
          console.log('点击的是播放按钮', this.player.paused());
          // // 单击切换播放状态
          if (this.player.paused()) {
            this.player.play();
          } else {
            this.player.pause();
          }
        }
      }
    }
  }
};
</script>
