import {Loading} from 'quasar'
  
interface itemParams {
    [propName: string]: any
}
/**
 * 配置相关
 */
export function CommonConfig() {
    return {
      FileConfig: {
        FileUrl: 'http://192.168.10.214:8130', //本地服务器文件预览地址
        UploadUrl: "http://192.168.10.214:8120", //本地上传文件地址
      }
    }
  }
  
  /**
   * 上传文件 
   */
  export function UploadSelfFile(path, file, callback, configObj) {
    //用户登录缓存
    const cacheInfo:itemParams = JSON.parse(window.localStorage["loginUserInfo"]);
    //上传配置
    let uploadConfig:itemParams={};
    if (cacheInfo && cacheInfo.data && cacheInfo.data.UploadConfig) {
      uploadConfig = cacheInfo.data.UploadConfig;
    }
    //获取文件扩展名
    // const index:string = file.name.lastIndexOf(".");
    // let suffix:string = file.name.substr(index);
    // const MyDate:any = new Date()
    // var timestamp1 = Date.parse(MyDate) + "_" + (Math.ceil(Math.random() * 1000));
    const str = '/Test';
    const newPath = "/EduSystem" + str + '/Upload/' + path;
    const uploadLoadding = Loading;
    uploadLoadding.show({
      message: '正在上传文件,请稍后...'
    })
    if (uploadConfig) {    
       switch (uploadConfig.StoreType) {
        //上传文件到腾讯云
        // case 1:
        //   newPath += "/" + timestamp1 + "" + suffix;
        //   UploadFileToTencent(uploadConfig, newPath, file, uploadLoadding, callback);
        //   break;
        //   //上传文件到阿里云
        // case 2:
        //   newPath += "/" + timestamp1 + "" + suffix;
        //   UploadFileToALi(uploadConfig, newPath, file, uploadLoadding, callback);
        //   break;
          //上传文件到自己文件服务器
        case 3:
          UploadFileToSystem(uploadConfig, newPath, file, uploadLoadding, callback, configObj);
          break;
      }
    }
  }
  
  /**
   * 上传文件到本地文件系统
   */
  export function UploadFileToSystem(uploadConfig, fileFullPath, fileObj, uploadLoadding, successCall, configObj) {
    const fileConfig:itemParams = CommonConfig();
    const locationName:string = window.location.hostname;
    if (locationName.indexOf('localhost') !== -1) {
      uploadConfig.UploadDomain = fileConfig.FileConfig.UploadUrl;
      uploadConfig.CustomDomain = fileConfig.FileConfig.FileUrl;
    }
    let url = uploadConfig.UploadDomain + "/Upload?filePath=" + fileFullPath;
    if (configObj) {
      //是否转换图片
      if (configObj.isTrans && configObj.isTrans == 1) {
        url += "&isTrans=1"
      }
      if (configObj.isCreateCover && configObj.isCreateCover == 1) {
        url += "&isCreateCover=1"
      }
    }
    const formData = new FormData()
    formData.append('myfile', fileObj)
    const xhr = new XMLHttpRequest()
    xhr.onload = function () {
      uploadLoadding.hide();
      const jsonObj:itemParams = JSON.parse(xhr.responseText);
      if (jsonObj.StatusCode === 1 && successCall) {
        const tempArray:any = [];
        if (jsonObj.OtherFile && jsonObj.OtherFile.length > 0) {
          jsonObj.OtherFile.forEach(item => {
            tempArray.push(uploadConfig.CustomDomain + item);
          })
        }
        const uploadResult:itemParams = {
          Code: 1,
          FileName: fileObj.name,
          FileUrl: uploadConfig.CustomDomain + jsonObj.FilePath,
          VideoCoverImg: uploadConfig.CustomDomain + jsonObj.VideoCoverImg,
          ExtFile: tempArray
        }
        if (successCall) {
  
          successCall(uploadResult);
        }
      }
    }
    xhr.open('post', url, true)
    xhr.send(formData)
  }
  
//   export function UploadFileToTencent(){
  
//   }
//   export function UploadFileToALi(){
  
//   }