import utils from './utils'
import TimeUtils from './TimeUtils'
import {uploadFile} from './../service'
import lrz from 'lrz'

// 文件上传下载工具类

let fileUtils = {
  // /** 批量上传文件 */
  // uploadImages(array) {
  //   let error = null
  //   if (!utils.isArray(array)) {
  //     error = new Error('传入的不是数组')
  //     throw error
  //   }

  //   if(array.length === 0) {
  //     error = new Error('传入图片数组长度为0')
  //     throw error
  //   }

  //   let index = 0
  //   let totalCount = array.length
  //   console.log('总数：' + totalCount)
  //   let successCount = 0

  //   return this.uploadMultiImages(array, index).then(() => {

  //   }
  // },

  /** 批量上传文件 */
  uploadMultiImages(array) {
    if (array.length === 0) {
      return new Promise ((resolve, reject) => {
        resolve([])
      })
    }

    let promiseArray = []
    let length = array.length
    for (let index = 0; index < length; index++) {
      const element = array[index];
      let uuid = utils.UUID()
      promiseArray.push(this.uploadSingleImage(element, uuid))
    }

    return Promise.all(promiseArray)
  },

  dataURLtoFile(dataurl, filename) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
      bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {type:mime});
  },

  /** 单个上传文件 */
   uploadSingleImage(obj, uuid) {
    EbeiPlugins.onLog('uploadSingleImage');
    return new Promise((resolve, reject) => {
      let error = null
      // let obj = array[index]
      EbeiPlugins.onLog('isNull');
      if (utils.isNull(obj)) {
        error = new Error('图片object错误')
        console.log('图片object错误')
        reject(error)
      }

      if (utils.isNull(obj.bigimageUrl) || !utils.isString(obj.bigimageUrl)) {
        error = new Error('图片路径错误')
        console.log('图片路径错误')
        reject(error)
      }

      EbeiPlugins.onLog('formatTimeStamp');

      let fileId = uuid
      let file = obj.bigimageUrl
      EbeiPlugins.onLog('lrz' + file);
      EbeiPlugins.onLog(file);

      let time = TimeUtils.formatTimeStamp((new Date()).getTime(), 'DateTimeSecond')
      EbeiPlugins.onLog('lrz');



      // let params = {
      //   'type': 1,
      //   'fileid': uuid,
      //   'range': 0,
      //   'totalsize': file.length(),
      //   'filecreatetime': time,
      //   'filename': fileId+'.jpg'
      // }
      //
      // uploadFile(params, rst.formData).then(res=>{
      //
      // })

      lrz(file, {width: 800}).then((rst) => {
        EbeiPlugins.onLog('params');
        let params = {
          'type': 1,
          'fileid': uuid,
          'range': 0,
          'totalsize': rst.file.size,
          'filecreatetime': time,
          'filename': fileId+'.jpg'
        }
        EbeiPlugins.onLog(rst.file.size);
        return uploadFile(params, rst.file)
      }).then((res) => {
        console.log('上传结果: ', res.data)
        EbeiPlugins.onLog(res.data);
        if (res.data.indexOf("成功") > -1) {
          // console.log('上传成功111')
          resolve(fileId)
          EbeiPlugins.onLog('成功');
        } else {
          EbeiPlugins.onLog('失败');
          // let error = new Error('上传失败111')
          reject(error)
          // resolve(error, array, index)
        }
      }).catch((error) => {
        EbeiPlugins.onLog('ssss');
        EbeiPlugins.onLog(error);
        reject(error)
      })
    })
  },
}

export default fileUtils
