import { Notification } from 'element-ui'
// import { MessageBox } from 'element-ui'
import { createUploadVideo, refreshUploadVideo } from '@/api/aliUpload'

// let watermark
function createUploader(_param) {
  _param.config = _param.config || {}
  const AliyunUpload = window.AliyunUpload
  // const self = this
  const uploader = new AliyunUpload.Vod({
    partSize: _param.config.partSize || 1048576, // 分片大小默认1M，不能小于100K
    parallel: _param.config.parallel || 5, // 并行上传分片个数，默认5
    retryCount: _param.config.retryCount || 3, // 网络原因失败时，重新上传次数，默认为3
    retryDuration: _param.config.retryDuration || 2, // 网络原因失败时，重新上传间隔时间，默认为2秒
    userId: _param.config.userId || '1130796220960824',
    // 添加文件成功
    addFileSuccess: function(uploadInfo) {
      // self.uploadDisabled = false
      // self.resumeDisabled = false
      // self.statusText = '添加文件成功, 等待上传...'
      // console.log('addFileSuccess: ' + uploadInfo.file.name)
      // MessageBox('是否为该文件添加水印?', '提示', {
      //   confirmButtonText: '添加',
      //   cancelButtonText: '不添加',
      //   type: 'success'
      // }).then(() => {
      //   watermark = true
      _param.addSucess && _param.addSucess(uploadInfo)
      // }).catch(() => {
      //   watermark = false
      //   _param.addSucess && _param.addSucess(uploadInfo)
      // })

      // _param.addSucess && _param.addSucess(uploadInfo)
    },
    // 开始上传
    onUploadstarted: function(uploadInfo) {
      // const uploadAuth = 'eyJTZWN1cml0eVRva2VuIjoiQ0FJUzJ3UjFxNkZ0NUIyeWZTaklyNWZtQjlQMnQ2NVJ3TE91TTI3MHRIWVdZdDkydkpmYWdEejJJSHBOZTNocUIrMGZzUGt3bEdsVTZmZ2NsclVxRXNRZEdoMmFONUVvdjhvUG9WbjlKcGZadjh1ODRZQURpNUNqUWRaQXFzd3RtcDI4V2Y3d2FmK0FVQXJHQ1RtZDVNZ1lvOWJUY1RHbFFDWnVXLy90b0pWN2I5TVJjeENsWkQ1ZGZybC9MUmRqcjhsbzF4R3pVUEcyS1V6U24zYjNCa2hsc1JZZTcyUms4dmFIeGRhQXpSRGNnVmJtcUpjU3ZKK2pDNEM4W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ENUaW1lIjoiMjAyMC0wOC0xM1QxMDoxNjo0MVoiLCJBY2Nlc3NLZXlTZWNyZXQiOiI0bmlFaUs0RnBKR1lHVjN6VmpOZm9maTFRdXlBUHFLQmlzS3FBQVpVRG92dCIsIkV4cGlyYXRpb24iOiIzNjAwIiwiUmVnaW9uIjoiY24tc2hhbmdoYWkifQ=='
      // const uploadAddress = 'eyJFbmRwb2ludCI6Imh0dHBzOi8vb3NzLWNuLXNoYW5naGFpLmFsaXl1bmNzLmNvbSIsIkJ1Y2tldCI6Im91dGluLTg4Yzg2NmNhYTYyZjExZWE4NDJmMDAxNjNlMDBiMTc0IiwiRmlsZU5hbWUiOiJjdXN0b21lclRyYW5zLzdmMmM4NWEzYjRhYmVlZDlmOTZkYWNjMDdhY2YzY2E0LzQ0MWUwZTYtMTczZTcxYjZmM2MtMDAwNC0wNDc0LTAzMS0wNTAzOC5tcDMifQ=='
      // const videoId = '6fcf653ecff349d08d6c52320aebc549'
      createUploadVideo(`title=${uploadInfo.file.name}&fileName=${uploadInfo.file.name}&watermark=${_param.watermark}`).then(res => {
        const { uploadAuth, uploadAddress, videoId } = res.data.d
        uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress, videoId)
      })
    },
    // 文件上传失败
    onUploadFailed: function(uploadInfo, code, message) {
      // console.log('onUploadFailed: file:' + uploadInfo.file.name + ',code:' + code + ', message:' + message)
      // self.statusText = '文件上传失败!'
      Notification.error({
        title: '错误',
        message: '文件上传失败!'
      })
      _param.uploadFailed && _param.uploadFailed(uploadInfo, code, message)
    },
    // 取消文件上传
    onUploadCanceled: function(uploadInfo, code, message) {
      // console.log('Canceled file: ' + uploadInfo.file.name + ', code: ' + code + ', message:' + message)
      // self.statusText = '文件已暂停上传'
      Notification({
        title: '警告',
        message: '文件已取消上传!',
        type: 'warning'
      })
      _param.uploadCanceled && _param.uploadCanceled(uploadInfo, code, message)
    },
    // 文件上传进度，单位：字节, 可以在这个函数中拿到上传进度并显示在页面上
    onUploadProgress: function(uploadInfo, totalSize, progress) {
      // console.log('onUploadProgress:file:' + uploadInfo.file.name + ', fileSize:' + totalSize + ', percent:' + Math.ceil(progress * 100) + '%')
      // const progressPercent = Math.ceil(progress * 100)
      // self.authProgress = progressPercent
      // self.statusText = '文件上传中...'
      // Notification.info({
      //   title: '消息',
      //   message: '文件上传中...'
      // })
      _param.uploadProgress && _param.uploadProgress(uploadInfo, totalSize, progress)
    },
    // 上传凭证超时
    onUploadTokenExpired: function(uploadInfo) {
      // 上传大文件超时, 如果是上传方式一即根据 UploadAuth 上传时
      // 需要根据 uploadInfo.videoId 调用刷新视频上传凭证接口(https://help.aliyun.com/document_detail/55408.html)重新获取 UploadAuth
      // 然后调用 resumeUploadWithAuth 方法, 这里是测试接口, 所以我直接获取了 UploadAuth
      // const refreshUrl = 'https://vod.cn-shanghai.aliyuncs.com/voddemo/RefreshUploadVideo?BusinessType=vodai&TerminalType=pc&DeviceModel=iPhone9,2&UUID=59ECA-4193-4695-94DD-7E1247288&AppVersion=1.0.0&Title=haha1&FileName=xxx.mp4&VideoId=' + uploadInfo.videoId
      // axios.get(refreshUrl).then(({ data }) => {
      //   const uploadAuth = data.UploadAuth
      //   uploader.resumeUploadWithAuth(uploadAuth)
      //   console.log('upload expired and resume upload with uploadauth ' + uploadAuth)
      // })
      // self.statusText = '文件超时...'
      refreshUploadVideo(`videoId=${uploadInfo.videoId}`).then(res => {
        uploader.resumeUploadWithAuth(res.data.d.uploadAuth)
      })
    },
    // 全部文件上传结束
    onUploadEnd: function(uploadInfo) {
      // console.log('onUploadEnd: uploaded all the files')
      // self.statusText = '文件上传完毕'

      Notification({
        title: '成功',
        message: '文件上传完毕',
        type: 'success'
      })
      _param.uploadEnd && _param.uploadEnd(uploadInfo)
    }
    // …… //其他回调
  })
  return uploader
}

export default createUploader
