import axios from "axios";
import mockData from "../input-data/data3.js";

export function getWholeData(sceneCode) {
  let url = `${process.env.VUE_APP_API_URL_PREFIX}laser/route/${sceneCode}/getRouteInfo`;
  return axios.get(url).then((res) => {
    if (Array.isArray(res.data.data) && res.data.data.length > 0) {
      return res.data.data;
    } else {
      throw "getWholeData结果异常！";
    }
  });
}

export function uploadWholeData(sceneCode, wholeData) {
  let url = `${process.env.VUE_APP_API_URL_PREFIX}laser/route/${sceneCode}/editRouteInfo`;
  return axios
    .post(url, {
      list: wholeData,
    })
    .then((res) => {
      if (res?.data?.code === 200) {
        return res?.data?.msg;
      } else {
        throw res?.data?.msg || "操作失败";
      }
    });
}

export function resetWholeData(sceneCode) {
  let url = `${process.env.VUE_APP_API_URL_PREFIX}laser/route/resetRouteInfo/${sceneCode}`;
  return axios.get(url).then((res) => {
    if (res?.data?.code === 200) {
      return res?.data?.msg;
    } else {
      throw res?.data?.msg || "操作失败";
    }
  });
}
export function reportTagInfo(data) {
  console.error("reporting", data);

  const url = `https://test.4dkankan.com/openDevice/ingest/${data.num}`;

  return axios.post(url, data).then((res) => {
    if (res?.data?.code === 200 || res?.status === 200) {
      return res?.data?.msg || "report success";
    } else {
      throw res?.data?.msg || "report failed";
    }
  });
}
export function startReport(data) {
  const url = `https://test.4dkankan.com/openDevice/start`;

  return axios.post(url, data).then((res) => {
    if (res?.data?.code === 200 || res?.status === 200) {
      return res?.data?.msg || "start report success";
    } else {
      throw res?.data?.msg || "start report failed";
    }
  });
}
export function stopReport(data) {
  const url = `https://test.4dkankan.com/openDevice/end`;

  return axios.post(url, data).then((res) => {
    if (res?.data?.code === 200 || res?.status === 200) {
      return res?.data || "stop report success";
    } else {
      throw res?.data?.message || "stop report failed";
    }
  });
}
export function getDebugMessages(deviceId = "sn123") {
  const currentDeviceId = encodeURIComponent(deviceId || "sn123");
  const url = `https://test.4dkankan.com/openDevice/mqtt/debug/messages/${currentDeviceId}`;

  return axios.get(url).then((res) => res?.data);
}
export function getNewDebugMessages(deviceId = "sn123") {
  const currentDeviceId = encodeURIComponent(deviceId || "sn123");
  const url = `https://test.4dkankan.com/openDevice/mqtt/tagdebug/messages/${currentDeviceId}`;

  return axios.get(url).then((res) => res?.data);
}
export function newStartReport(data) {
  const url = `https://test.4dkankan.com/openDevice/mqtt/session/start`;

  return axios.post(url, data).then((res) => {
    if (res?.data?.code === 200 || res?.status === 200) {
      return res?.data || "stop report success";
    } else {
      throw res?.data?.message || "stop report failed";
    }
  });
}
export function newStopReport(data) {
  const url = `https://test.4dkankan.com/openDevice/mqtt/session/end`;

  return axios.post(url, data).then((res) => {
    if (res?.data?.code === 200 || res?.status === 200) {
      return res?.data || "stop report success";
    } else {
      throw res?.data?.message || "stop report failed";
    }
  });
}

export const newStoptReport = newStopReport;
