// make requests to that backend api
//fetch api
import fetch from "isomorphic-fetch";
import request from "../reportUtils/request";

import { getIotUrl } from "../reportUtils/getQueryData";
const {
  config: {
    mesUrl,
    equipservice,
    edgeQueryUrl,
    edgeUrl,
    refreshInterval,
    ImgUrl,
    edgewsUrl,
  } = {},
} = global;
export const interval = refreshInterval;

export const imgUrl = mesUrl + ImgUrl;
export const getWsUrl = async () => {
  const token = window.localStorage.getItem("token");
  let workstation = window.localStorage.getItem("workCenterCode");
  return getIotUrl(workstation, sdkWsUrl);
};

export const applicationUrl = "";

export const iotUrl = "iotUrl";
export const sdkUrl = "sdkUrl";
export const sdkWsUrl = "sdkWsUrl";
// export const changeEdgeUrl = function (workstation) {
//  return [ getIotUrl(workstation,iotUrl),  getIotUrl(workstation,sdkWsUrl)]
// };
export const doPost = function (url, requestbody) {
  const token = window.localStorage.getItem("token");
  return fetch(url, {
    method: "post",
    headers: {
      Accept: "*/*",
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
      "Access-Control-Allow-Headers":
        "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization",
      "Content-Type": "application/json",
      Authorization: token,
      Connection: "keep-alive",
      "User-Agent": "PostmanRuntime/7.29.0",
    },
    body: JSON.stringify(requestbody),
  });
};
export const doIotPost = function (url, requestbody, workstation) {
  const token = window.localStorage.getItem("token");
  const wiotUrl = window.localStorage.getItem(workstation+"iotUrl");
  if (wiotUrl&&wiotUrl.length > 0) {
    return request({ url: wiotUrl, bodys: requestbody });
  } else {
    return getIotUrl(workstation, iotUrl).then((ioturl) => {
      return new Promise((resolve, reject) => {
        wiotUrl = ioturl + url;
        window.localStorage.setItem(workstation+"iotUrl",wiotUrl);

        request({ url: wiotUrl, bodys: requestbody }).then(
          (response = {}) => {
            if (response.success) {
              resolve(response);
            }
            resolve([]);
          }
        );
      });
    });
  }
};
export default {
  fetchFactory: {
    title: "获取工厂信息",
    url: mesUrl + `/mdgeneric/md/bmWorkCenter/findAllWithPage`,
  },
  fetchType: {
    title: "获取类型信息",
    url: mesUrl + `/mdgeneric/md/bmEquipmentType/findAllWithPage`,
  },
  fetchEquipList: {
    title: "获取设备信息",
    url: mesUrl + equipservice + `findallequipment`,
  },
  fetchEquipFieldsList: {
    title: "获取设备信息带设备字段",
    url: mesUrl + `/mdgeneric/md/bmEquipment/` + `getEquipDataByWorkCenterCode`,
  },
  fetchEquipFields: {
    title: "获取设备字段信息",
    url: mesUrl + equipservice + `findequipmentbycode`,
  },
  fetchEquipImg: {
    title: "获取设备图片信息",
    url: mesUrl + `/console/fs/file/getall?fileGroupId=`,
  },
  fetchLastFieldsValue: {
    title: "获取设备状态信息值",
    url: `/follow/last`,
  },
  fetchLastEqFieldsValue: {
    title: "获取设备字段信息值",
    url: `/equip/lastbyEqs`,
  },
  fetchAlertList: {
    title: "获取设备告警信息",
    url: edgeUrl + `/alert/list`,
  },
};
