const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
// var app1 = getApp();
Page({
  data: {
    url: '', //播放地址
    title: '', //视频标题
    message: '', //视频信息
    playTimes: 0, //播放次数
    playVideoDuration: 0, // 从这个时间点开始播放
    id: 0, //带过来的id
    studyLog: {}, //我得到的学习日志
    // intervalTime: 0, //隔五分钟清空重新计时 弹出信息确认在不在
    video_real_time: 0, //实时播放进度
    intervalCtx: null,
    videoContext: null,
    classInfo: {},
    showWW: true,
    showChange: true,
    showfullChange: false,
    networkType: '',
    isAgree: false, //表示不同意用4G
    fullScreen: false,
    spanTime: 0 //间隔时间弹窗小人
  },
  isClass(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selMyClas',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            classInfo: res.data.data
          })
        } else {
          if (res.data.msg == "运行时异常:学员不可同时存在两个班级中。") {
            wx.showModal({
              content: "学员不可同时存在多个班级中哦",
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }
              }
            })
            return false;
          } else {
            wx.showModal({
              content: "您当前还没有正在开放的班级！",
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }
              }
            })
            return false;
          }
        }
      }
    })
  },
  // 视频出错了
  videoErrorCallback: function (e) {
    console.log('视频错误信息:', e.detail.errMsg)
  },
  // 计时 五分钟弹出一个弹框
  // calculateTime() {
  // console.log(this.data.alertTime)

  // console.log(this.data.intervalTime)
  // let _this = this;
  // this.setData({
  //   intervalTime: _this.data.intervalTime + 5
  // })
  // if (_this.data.intervalTime > 300) {
  //   _this.data.videoContext.pause()
  //   this.setData({
  //     showWW: false,
  //     intervalTime: 0
  //   })
  // }
  // },
  // 点击开始播放视频
  async bindplay() {
    this.setData({
      showWW: true
    })
    const netStatus = await this.getNetStatus();
    this.setData({
      networkType: netStatus
    })
    if (netStatus != 'wifi' && !this.data.isAgree) {
      this.data.videoContext.pause()
      this.setData({
        showChange: false
      })
    }
    wx.onNetworkStatusChange((res) => {
      if (this.data.networkType == "wifi" && res.networkType != "wifi" && !this.data.isAgree) {
        this.data.videoContext.pause()
        this.setData({
          showChange: false
        })
      }
    })
  },
  goclose() {
    this.setData({
      showChange: true,
      isAgree: false
    })
  },
  goon() {
    this.setData({
      showChange: true,
      isAgree: true
    })
    this.data.videoContext.play()
  },
  fullScreenChange(e) {
    console.log(e.detail.fullScreen, "我是切换全屏的状态")
    this.setData({
      fullScreen: e.detail.fullScreen,
    })
  },
  startbf() {
    this.setData({
      showWW: true
    })
    this.data.videoContext.play()
  },
  // 视频暂停
  // bindpause() {
  //   clearInterval(this.data.intervalCtx)
  // },
  // 得到录播列表
  getdbArr(sessionKey) {
    let _this = this;
    wx.request({
      method: "post",
      url: app.globalData.publicUrl + '/wx/course/selectRecordByCourseId',
      data: {
        sessionKey: sessionKey,
        courseId: _this.data.id
      },
      success: (e) => {
        console.log(e, "888888888888888888")
        this.setData({
          url: e.data.url,
          show: false,
          title: e.data.course.courseName,
          message: e.data.course.courseInfo,
          playTimes: e.data.course.playTimes,
          courseTime: e.data.course.courseTime
        })
      }
    })
  },
  // 获取学习进度
  getStudyLog(sessionKey) {
    let _this = this;
    wx.request({
      method: "post",
      url: app.globalData.publicUrl + '/wx/course/getStudyLog',
      data: {
        sessionKey: sessionKey,
        courseId: _this.data.id,
      },
      success: (e) => {
        if (e.data.studyLog.isDone == 1) {
          _this.setData({
            studyLog: e.data.studyLog,
          })
        } else {
          _this.setData({
            studyLog: e.data.studyLog,
            playVideoDuration: e.data.studyLog.studyTime
          })
        }
      }
    })
  },
  // 设置不可快进
  timeUpdate: function (e) {
    let _this = this;
    let currentTime = parseInt(e.detail.currentTime) //当前播放秒数
    if (currentTime != 0 && currentTime % 300 == 0) {
      if (currentTime != this.data.spanTime) {
        this.setData({
          spanTime: currentTime,
          showWW: false
        })
        _this.data.videoContext.pause()
      }
    }
    let studyTime = 'studyLog.studyTime'
    //实时给页面的学习记录赋值
    let jump_time = parseInt(_this.data.video_real_time); //定义跳转的时间
    if (_this.data.studyLog.isDone != 1) {
      if (currentTime > jump_time && currentTime - jump_time > 3 && currentTime > _this.data.playVideoDuration) {
        _this.data.videoContext.seek(_this.data.playVideoDuration)
        if (_this.data.video_real_time > 0) {
          wx.showToast({
            title: '该视频不可以快进哦！',
            icon: 'none',
            duration: 2000,
          })
        }
        _this.setData({
          video_real_time: _this.data.playVideoDuration, //实时播放进度
        })
      }
      if (currentTime > jump_time && currentTime - jump_time < 3 && currentTime > _this.data.playVideoDuration) {
        _this.setData({
          playVideoDuration: currentTime,
          [studyTime]: currentTime
        })
      }
      if (currentTime > jump_time && currentTime - jump_time < 3) {
        _this.setData({
          video_real_time: currentTime, //实时播放进度
        })
      }
    }
  },
  // 修改学习进度
  chechEndStatus: function () {
    let _this = this;
    if (this.data.classInfo.id != -1) {
      console.log(this.data.video_real_time)
      console.log(this.data.courseTime / 1000 - 10)
      if (this.data.video_real_time > this.data.courseTime / 1000 - 10) {
        let isDone = 'studyLog.isDone'
        this.setData({
          [isDone]: 1
        })
        wx.request({
          method: "post",
          url: app.globalData.publicUrl + '/wx/course/updateStudyLog',
          data: {
            studyLog: _this.data.studyLog
          },
          success: (e) => {
            wx.showModal({
              showCancel: false,
              content: "该录播视频已经看完啦，快去参与该节课的考试和评价吧！只有考试和评价都参与了，才可以继续进行下一节哦！",
              success(res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }
            })
          }
        })
      }
    }
  },
  onUnload() {
    let _this = this;
    if (_this.data.studyLog.isDone != '1') {
      if (this.data.video_real_time > this.data.courseTime / 1000 - 10) {
        let isDone = 'studyLog.isDone'
        this.setData({
          [isDone]: 1
        })
      }
    }
    if (_this.data.classInfo.id != -1) {
      wx.request({
        method: "post",
        url: app.globalData.publicUrl + '/wx/course/updateStudyLog',
        data: {
          studyLog: _this.data.studyLog
        },
        success: (e) => {
          console.log(e, "我修改进度成功了")
        }
      })
    }
  },
  onHide() {
    let _this = this;
    if (_this.data.studyLog.isDone != '1') {
      if (this.data.video_real_time > this.data.courseTime / 1000 - 10) {
        let isDone = 'studyLog.isDone'
        this.setData({
          [isDone]: 1
        })
      }
    }
    if (_this.data.classInfo.id != -1) {
      wx.request({
        method: "post",
        url: app.globalData.publicUrl + '/wx/course/updateStudyLog',
        data: {
          studyLog: _this.data.studyLog
        },
        success: (e) => {
          console.log(e, "我修改进度成功了")
        }
      })
    }
  },
  // 获取当前网络状态
  async getNetStatus() {
    return new Promise((resolve, reject) => {
      wx.getNetworkType({
        success: (res) => {
          resolve(res.networkType);
        }
      })
    });
  },
  async onLoad(options) {
    this.setData({
      options: options,
      id: options.id,
      videoContext: wx.createVideoContext('myVideo')
    })
    const sessionKey = await tools.checkSessionAndLogin();
    this.isClass(sessionKey);
    this.getStudyLog(sessionKey);
    this.getdbArr(sessionKey);
  }
})