<template>
  <view>
    <div class="video">
      <video
        id="myVideo"
        :src="path"
        :initial-time="studyDuration"
        :show-progress="false"
        :enable-progress-gesture="false"
        @error="videoErrorCallback"
        @timeupdate="videoTimeUpdate"
      />
    </div>

    <u-modal
      :show="showModal"
      title="提示"
      :content="content"
      showCancelButton
      @cancel="showModal = false"
      @confirm="handleEnd"
    />

    <div class="btn">
      <u-button type="primary" shape="circle" text="结束学习" size="small" @click="openModal" />
    </div>
  </view>
</template>

<script>
import config from '@/config'
import { pathToBase64, base64ToPath } from 'image-tools'
import { updStudyDuration } from '@/api/eduApp'

export default {
  props: {
    states: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      isLoading: false,
      studyId: '', // 学习id
      stageId: '', // 阶段id
      stageContentId: '', // 阶段内容id
      stageType: '', // 阶段类型
      studyCourseId: '',
      sourceId: '', // 素材id
      path: '', // 视频路径
      studyDuration: 0, // 学习时长
      allStudyDuration: 0, // 总时长
      studyPercentage: 0, // 学习进度
      // 当前播放时间
      currentTime: 0,
      showModal: false, // 是否显示弹窗
      content: '是否确认结束学习？',
      isEnd: false,
      video: null,
      isHide: false,
      oldTime: 0,
      random1: 0,
      random2: 0,
      // 失败次数
      failCount: 3
    }
  },
  onLoad(opt) {
    opt = JSON.parse(opt.params)
    console.log('🚀 ~ onLoad ~ :', opt)
    this.studyId = opt.studyId
    this.stageId = opt.stageId
    this.stageContentId = opt.stageContentId
    this.stageType = opt.stageType
    this.studyCourseId = opt.studyCourseId
    this.sourceId = opt.sourceId
    this.path = config.fileUrl + opt.path
    console.log('🚀 ~ onLoad ~ this.path:', this.path)
    this.studyDuration = this.currentTime = Number(opt.studyDuration)
    this.allStudyDuration = Number(opt.allStudyDuration)
    this.random1 = ((this.allStudyDuration / 3) * 2).toFixed(0)
    this.random2 = (this.allStudyDuration / 3).toFixed(0)
    this.isEnd = opt.isEnd
    if (opt.isEnd) {
      this.studyDuration = 0
    }
    this.video = uni.createVideoContext('myVideo')
  },
  onHide() {
    console.log('🚀 ~ onHide ~ 页面隐藏')
    if (!this.isEnd && !this.isHide) {
      // 关闭页面时，修改项目进度
      this.handleEnd()
    }
  },
  onUnload() {
    console.log('🚀 ~ onUnload ~ 页面关闭')
    if (!this.isEnd && !this.isHide) {
      // 关闭页面时，修改项目进度
      this.handleEnd()
    }
  },
  methods: {
    videoErrorCallback(e) {
      console.log('视频错误信息：', e)
    },
    // 视频播放时间更新
    videoTimeUpdate(e) {
      this.currentTime = Math.ceil(e.detail.currentTime)
      console.log('🚀 ~ 视频播放时间： ~ this.currentTime:', this.currentTime)
      console.log('🚀 ~ 视频总时长： ~ this.allStudyDuration:', this.allStudyDuration)
      console.log('🚀 ~ videoTimeUpdate ~ random1-2:', this.random1, this.random2)

      if (this.currentTime != this.oldTime) {
        if (this.currentTime == this.random2 || this.currentTime == this.random1) {
          this.oldTime = this.currentTime // 记录当前时间, 防止重复执行, 如果下次时间等于当前时间, 则不执行
          this.video.pause()
          this.openPhotograph()
        }
      }
    },
    openModal() {
      this.showModal = true
    },
    // 结束学习
    handleEnd() {
      if (this.isLoading) {
        uni.showToast({
          title: '正在提交中, 请稍后',
          icon: 'none'
        })
        return
      }
      this.isLoading = true
      if (this.isEnd) {
        uni.reLaunch({
          url: '/pages/YNEduApp/learnProj/learnProjDetail?id=' + this.studyId
        })
        return
      }
      // 手动暂停视频
      this.video.pause()
      // 获取当前播放时间 - 用于记录学习时长
      console.log('当前播放时间：', this.currentTime, this.allStudyDuration)
      this.studyDuration = this.currentTime
      // 计算学习进度
      this.studyPercentage =
        Math.ceil((this.studyDuration / this.allStudyDuration) * 100).toFixed(2) > 100
          ? 100
          : Math.ceil((this.studyDuration / this.allStudyDuration) * 100).toFixed(2)
      console.log('🚀 ~ handleEnd ~ this.studyPercentage:', this.studyPercentage)

      const params = {
        userId: uni.getStorageSync('userId'),
        studyId: this.studyId,
        stageId: this.stageId,
        stageContentId: this.stageContentId,
        stageType: this.stageType,
        studyCourseId: this.studyCourseId,
        sourceId: this.sourceId,
        studyDuration: this.studyDuration,
        studyPercentage: this.studyPercentage
      }
      console.log('🚀 ~ handleEnd ~ params:', params)
      // updStudyDuration(params).then(res => {
      //   console.log('🚀 ~ handleEnd ~ res:', res)
      //   this.showModal = false
      //   uni.navigateBack()
      // })
      this.$verificationToken()
      uni.request({
        url: config.baseUrl + '/exam-student/student/updStudyDuration',
        method: 'post',
        data: params,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          Authorization: uni.getStorageSync('access_token')
        },
        success: res => {
          console.log('🚀 ~ handleEnd ~ res:', res)
          uni.reLaunch({
            url: '/pages/YNEduApp/learnProj/learnProjDetail?id=' + this.studyId
          })
          setTimeout(() => {
            this.isLoading = false
            this.showModal = false
          }, 1000)
        },
        fail: err => {
          console.log('🚀 ~ handleEnd ~ err:', err)
        }
      })
    },
    // 拍照录入
    openPhotograph() {
      this.isHide = true
      setTimeout(() => {
        uni.chooseImage({
          count: 1,
          sizeType: ['compressed'],
          sourceType: ['camera'],
          success: res => {
            let url = ''
            console.log('🚀 ~ res-拍照:', res)
            this.imgToBase64(res.tempFilePaths[0]).then(base64 => {
              url = base64
              console.log('🚀 ~ this.imgToBase64 ~ base64:', url)
              this.getFaceRecognition({ userId: uni.getStorageSync('userId'), img: url })
            })
          },
          fail(err) {
            console.log('🚀 ~ openFaceScan ~ 人脸识别失败', err)
            uni.showToast({
              title: '人脸识别失败',
              icon: ''
            })
            setTimeout(() => {
              this.handleEnd()
            }, 1000)
          }
        })
      }, 100)
    },
    imgToBase64(data) {
      return new Promise((resolve, reject) => {
        pathToBase64(data)
          .then(base64 => {
            resolve(base64)
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 发送请求
    getFaceRecognition(params) {
      console.log('🚀 ~ getFaceRecognition ~ params:', params)
      this.$verificationToken()
      uni.request({
        url: config.baseUrl + '/exam-student/personalCenter/getFaceRecognition',
        method: 'POST',
        header: {
          'content-type': 'application/json',
          Authorization: uni.getStorageSync('access_token')
        },
        data: params,
        success: res => {
          console.log('🚀 ~ openFaceScan ~ res-人脸识别:', res)
          res = res.data
          console.log('🚀 ~ openFaceScan ~ res-人脸识别:', res.code)
          if (res.code == 200) {
            // 提示
            uni.showToast({
              title: '人脸识别成功',
              icon: '',
              duration: 1500
            })
            this.failCount = 3 // 重置次数
            this.isHide = false
            this.video.play()
          } else {
            this.failCount--
            if (this.failCount == 0) {
              uni.showToast({
                title: '人脸识别失败, 即将结束学习',
                icon: 'none',
                duration: 1500
              })
              setTimeout(() => {
                this.handleEnd()
              }, 1500)
            } else {
              uni.showToast({
                title: '人脸识别失败, 请重新录入, 剩余次数: ' + this.failCount + '次',
                icon: 'none',
                duration: 1500
              })
              setTimeout(() => {
                this.openPhotograph()
              }, 1500)
            }
          }
        },
        fail(err) {
          console.log('🚀 ~ openFaceScan ~ 人脸识别失败', err)
          uni.showToast({
            title: '人脸识别失败',
            icon: ''
          })
          setTimeout(() => {
            this.handleEnd()
          }, 1000)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
// 隐藏 video 默认控制条
::v-deep .uni-video-controls {
  display: none !important;
}

.video {
  width: 100%;
  height: 260px;
  uni-video {
    width: 100%;
    height: 100%;
  }
}

.btn {
  margin: 60px auto;
  width: 100px;
}

::v-deep .u-modal__content {
  flex-direction: column;
  align-items: center;
}
</style>
