import { http } from '@/utils/http';
import store from '@/store';
import { app } from '@/store/mutation-types';

/**
 * @description 得到验证码图像
 */
export function GetVerificationCodeImage() {
  return http.get(`${process.env.VUE_APP_FileServer}?MethodName=login_control-GetVerificationCodeImage`);
}

/**
 * @description 得到系统标志
 * @param {object} data 请求数据
 */
export function GetLogo(data) {
  return http.post(`${process.env.VUE_APP_FileServer}?MethodName=sys_config_manager-GetLogo`, data);
}

/**
 * 得到文件 ID 与 URL 的映射
 */
export function GetFileURLMap(data) {
  return http.post(`${process.env.VUE_APP_FileServer}?MethodName=file_store_manager-GetFileURLMap`, data);
}

/**
 * 得到文件存储信息
 */
export function GetFileStoreInfo(data) {
  return http.post(`${process.env.VUE_APP_FileServer}?MethodName=file_store_manager-GetFileStoreInfo`, data);
}

/**
 * 得到用户能进入的子系统列表（电脑端）
 */
export function GetChildSysList_CanEnter_PC(data) {
  return http.post(`${process.env.VUE_APP_FileServer}?MethodName=login_control-GetChildSysList_CanEnter_PC`, data);
}

/**
 * 解开访问令牌
 * @param {object} data 请求数据
 * @param {string} data.access_token 访问令牌
 */
export function ParseAccessToken(data) {
  return http.post(`${process.env.VUE_APP_FileServer}?MethodName=login_control-ParseAccessToken`, data);
}

/**
 * 保存文件字节流的 Base64 编码文本信息
 */
export function SaveFileByteBase64Text(data) {
  return http.post(`${process.env.VUE_APP_FileServer}?MethodName=file_store_manager-SaveFileByteBase64Text`, data);
}

/**
 * 上传文件
 * @param {String} SecurityLevel 保密级别
 * @param {object} file 文件对象
 * @param {object} option 上传选项
 * @param {function} option.handleUploadProgress 上传进度回调
 * @param {boolean} option.isGlobalprogress 是否使用全局进度条
 */
export async function fileUpload(
  SecurityLevel,
  file,
  { handleUploadProgress, isGlobalprogress = false } = { isGlobalprogress: false },
) {
  let formData = null;
  if (file instanceof FormData) {
    formData = file;
  } else {
    formData = new FormData();
    formData.append(file.filename, file.file, file.file.name);
  }

  let onUploadProgress = handleUploadProgress || null;
  if (isGlobalprogress) {
    store.commit(`app/${app.SHOW_PROGRESS}`, true);
    onUploadProgress = ({ loaded, progress, total }) => {
      // 因为上传进度为 1 后，上传事件还会继续一段时间，所以这里将进度设置为 0.99
      let precent = progress >= 1 ? 0.99 : progress;
      store.commit(`app/${app.SET_UPLOAD_INFO}`, { loaded, progress: precent, total });
    };
  }

  const controller = new AbortController();
  store.commit(`app/${app.SET_UPLOAD_CONTROLLER}`, controller);

  try {
    const res = await http.postForm('/GCLSFileServer/WebFileUpload', formData, {
      params: {
        SecurityLevel,
      },
      signal: controller.signal,
      transformRequest: [(data) => data],
      onUploadProgress,
      timeout: 0,
    });
    store.commit(`app/${app.SET_UPLOAD_INFO}`, { progress: 1 });
    return res;
  } finally {
    store.commit(`app/${app.SET_UPLOAD_CONTROLLER}`, null);
  }
}

export function GetStaticResources(MethodName, data) {
  return http.post(`/GCLSFileServer/ServiceInterface?MethodName=${MethodName}`, data);
}
