<template>
  <div id="dplayer" ref="dplayer" />
</template>

<script>
import { videoUrl, submitPlayRecord } from '@/api/media/video'
const hls = require('hls.js')
const dashjs = require('dashjs')
const flv = require('flv.js')
const DPlayer = require('dplayer')

export default {
  name: 'Play',
  props: {
    videoCover: {
      type: String,
      default: () => ''
    }
  },
  data() {
    return {
      hls,
      dashjs,
      flv,
      DPlayer,
      userId: 0,
      videoId: '',
      playRecord: null
    }
  },
  mounted() {
    const userInfo = this.$store.state.user.userInfo
    if (userInfo != null) {
      this.userId = userInfo.userId
    }

    this.videoId = this.$route.params.id
    videoUrl(this.$route.params.id)
      .then(res => {
        if (res.code === 0) {
          const urlType = res.data.type
          if (urlType === 'mp4') {
            const urls = res.data.urls
            for (const url of urls) {
              url.type = 'normal'
            }
            this.initMp4Player(this.userId, this.videoId, this.videoCover, urls)
          } else if (urlType === 'hls') {
            // this.initHlsPlayer(this.videoId, coverUrl, urls)
          } else if (urlType === 'dash') {
            // this.initDashPlayer(this.videoId, coverUrl, urls)
          } else if (urlType === 'flv') {
            // this.initFlvPlayer(this.videoId, coverUrl, urls)
          } else {
            console.log('无法识别 url 类型')
          }
        } else {
          console.error(res.msg)
        }
      }).catch(error => {
        console.error(error.message)
      })
  },
  methods: {
    // TODO 获取弹幕配置，将 videoUrl 作为本函数的回调
    danmakuConfig() {
    },
    submitVideoPlayRecord(currentTime) {
      if (this.playRecord == null) {
        return
      }

      this.playRecord.currentTime = currentTime
      submitPlayRecord(this.playRecord)
        .then(res => {
          if (res.code === 0) {
            console.log('播放进度已发送')
          } else {
            console.error(res.msg)
          }
        }).catch(error => {
          console.error(error.message)
        })
    },
    initMp4Player(userId, videoId, coverUrl, urls) {
      const player = new DPlayer({
        container: document.querySelector('#dplayer'),
        lang: 'zh-cn',
        logo: '/logo.png',
        autoplay: false,
        screenshot: false,
        video: {
          pic: coverUrl,
          defaultQuality: 0,
          quality: urls
          // url: videoUrl
          // type: 'auto',
        },
        danmaku: {
          id: videoId,
          maximum: 10000,
          api: '//api.reghao.cn/api/comment/danmaku/',
          token: 'bili',
          user: userId,
          bottom: '15%',
          unlimited: true
        }
      })

      if (userId !== 0) {
        console.log('初始化 playRecord')
        this.playRecord = {
          'userId': this.userId,
          'videoId': this.videoId,
          'currentTime': 0.0
        }
      }

      // 跳转到指定秒
      // dp.seek(100)
      player.on('play', function() {
        console.log('mp4Player 开始播放事件' + this.userId)
        console.log(this.playRecord)
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      player.on('pause', function() {
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      player.on('ended', function() {
        // TODO 当前视频播放完成后判断是否继续播放相关推荐中的视频
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      player.on('seeking', function() {
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      player.on('seeked', function() {
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      var i = 0
      player.on('progress', function() {
        console.log('i = ' + i)
        if (i % 5 === 0) {
          if (this.playRecord == null) {
            return
          }

          this.playRecord.currentTime = player.video.currentTime
          submitPlayRecord(this.playRecord)
            .then(res => {
            }).catch(error => {
              console.error(error.message)
            })
        }
        i++
      })
    },
    initHlsPlayer(videoId, coverUrl, videoUrl) {
      const player = new DPlayer({
        container: document.querySelector('#dplayer'),
        lang: 'zh-cn',
        autoplay: false,
        screenshot: true,
        video: {
          pic: coverUrl,
          url: videoUrl,
          type: 'hls'
        },
        logo: '/logo.png',
        danmaku: {
          id: videoId,
          maximum: 10000,
          api: '//api.reghao.cn/api/media/danmaku/',
          token: 'bili',
          user: this.userId,
          videoId: videoId,
          bottom: '15%',
          unlimited: true
        }
      })

      player.on('play', function() {
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      player.on('pause', function() {
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      player.on('ended', function() {
        // TODO 当前视频播放完成后判断是否继续播放相关推荐中的视频
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      player.on('seeking', function() {
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      player.on('seeked', function() {
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })

      var i = 0
      player.on('progress', function() {
        console.log('i = ' + i)
        if (i % 5 === 0) {
          if (this.playRecord == null) {
            return
          }

          this.playRecord.currentTime = player.video.currentTime
          submitPlayRecord(this.playRecord)
            .then(res => {
            }).catch(error => {
              console.error(error.message)
            })
        }
        i++
      })
    },
    initDashPlayer(videoId, coverUrl, videoUrl) {
      console.log('初始化 dash 播放器')
      const player = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
          url: videoUrl,
          type: 'dash'
        }
      })
      console.log('dash 播放器初始化完成')

      player.on('play', function() {
        if (this.playRecord == null) {
          return
        }

        this.playRecord.currentTime = player.video.currentTime
        submitPlayRecord(this.playRecord)
          .then(res => {
          }).catch(error => {
            console.error(error.message)
          })
      })
    },
    initFlvPlayer(videoId, coverUrl, videoUrl) {
      console.log('初始化 flv 播放器')
      const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
          url: videoUrl,
          type: 'flv'
        },
        pluginOptions: {
          flv: {
            // refer to https://github.com/bilibili/flv.js/blob/master/docs/api.md#flvjscreateplayer
            mediaDataSource: {
              // mediaDataSource config
            },
            config: {
              // config
            }
          }
        }
      })
      console.log(dp.plugins.flv) // flv 实例
    }
  }
}
</script>

<style>
#dplayer {
  height: 500px;
}
</style>
