import store from "@/store";
import { API_lookFileOrDownFlie } from "@/store/action/layout";
import { baseURL } from "./http";

// 携带token，转换成文件流
export const urlChangeFu = async (
  url: string,
  flag: boolean,
  type?: "img" | "video" | "audio" | ".pdf" | ".txt" | ".docx" | ".xlsx",
  name?: string
) => {
  // flag true 为 生成 a标签 下载

  const res: any = await API_lookFileOrDownFlie(url);

  if (res) {
    const srcRes = window.URL.createObjectURL(res);

    if (flag) {
      // 创建a标签下载
      let link = document.createElement("a"); //创建a标签
      link.style.display = "none"; //使其隐藏
      link.href = srcRes; //赋予文件下载地址
      link.setAttribute("download", name!); //设置下载属性 以及文件名
      document.body.appendChild(link); //a标签插至页面中
      link.click(); //强制触发a标签事件
      document.body.removeChild(link);
    } else {
      if (type === "img") {
        store.dispatch({
          type: "layout/lookBigImg",
          payload: {
            url: srcRes,
            show: true,
          },
        });
      } else if (type === ".pdf" || type === ".txt") {
        window.open(srcRes);
      } else if (type === "audio" || type === "video") {
        store.dispatch({
          type: "layout/lookDom",
          payload: {
            src: srcRes,
            type,
            flag: true,
          },
        });
      } else if (type && name && [".docx", ".xlsx"].includes(type)) {
        store.dispatch({
          type: "layout/fileLookUrl",
          payload: { url: srcRes, name: type },
        });
      }
    }
  }
};

// 查看 权限 图片 /视频 、音频
export const authFilesLookFu = (
  name: string,
  url: string,
  // 不需要权限
  noAuth?: boolean
) => {
  let flag = false;

  const nameRes = name ? name : "";

  // pdf和txt 直接新窗口打开
  const arr0: (".pdf" | ".txt")[] = [".pdf", ".txt"];
  arr0.forEach((v) => {
    if (nameRes.toLowerCase().endsWith(v)) {
      if (url) {
        noAuth ? window.open(baseURL + url) : urlChangeFu(url, false, v);
      }
      flag = true;
    }
  });

  // 图片使用 antd的图片预览组件
  const arr1 = [".png", ".jpg", ".jpeg", ".gif"];
  arr1.forEach((v) => {
    if (nameRes.toLowerCase().endsWith(v)) {
      if (url) {
        noAuth
          ? store.dispatch({
              type: "layout/lookBigImg",
              payload: {
                url: baseURL + url,
                show: true,
              },
            })
          : urlChangeFu(url, false, "img");
      }

      flag = true;
    }
  });

  // 视频和音频 使用自己的封装的组件
  let type: "" | "video" | "audio" = "";
  const arr2 = [".mp3", ".wav"];
  arr2.forEach((v) => {
    if (nameRes.toLowerCase().endsWith(v)) {
      type = "audio";
      flag = true;
    }
  });

  if (nameRes.toLowerCase().endsWith(".mp4")) {
    type = "video";
    flag = true;
  }

  if (type && url) {
    noAuth
      ? store.dispatch({
          type: "layout/lookDom",
          payload: {
            src: url,
            type,
          },
        })
      : urlChangeFu(url, false, type);
  }

  // docx xlsx使用插件
  const arr3: (".docx" | ".xlsx")[] = [".docx", ".xlsx"];
  arr3.forEach((v) => {
    if (nameRes.toLowerCase().endsWith(v)) {
      if (url) {
        noAuth
          ? store.dispatch({
              type: "layout/fileLookUrl",
              payload: { url: baseURL + url, name: v },
            })
          : urlChangeFu(url, false, v, nameRes);
      }
      flag = true;
    }
  });

  return flag;
};
