import { apiSaveEquipment, initSceneData } from "@/api/tunnelScene";
import { toRaw } from "vue";
let modelEquipmentList = [];
let originData = null;
// 初始化当前隧道数据并保存在本地
export const initData = (tunnelId, otherInfo) => {
  return new Promise((resolve, reject) => {
    initSceneData(tunnelId)
      .then((res) => {
        originData = toRaw(otherInfo);
        if (res.data.tunnelThreeConfig) {
          console.log("data", res.data);
          modelEquipmentList = JSON.parse(res.data.tunnelThreeConfig);
        }
        console.log("test", modelEquipmentList, originData);
        resolve(modelEquipmentList);
      })
      .catch((Error) => {
        ElMessage.error("场景初始化异常！");
        reject();
      });
  });
};

//保存数据并提交
export const saveEquipment = (equipmentInfo, pointGap) => {
  const {
    constructionLength,
    isDefault,
    remarks,
    serialNumber,
    siteId,
    totalLength,
    tunnelId,
    tunnelName,
  } = originData;
  let tempModelEquipmentList = [...modelEquipmentList]; //数组克隆
  tempModelEquipmentList.push(toRaw(equipmentInfo));
  return new Promise((resolve, reject) => {
    console.log("equId", equipmentInfo.equipmentId);
    hasEquipment(equipmentInfo.equipmentId)
      ? reject(3000)
      : apiSaveEquipment({
          constructionLength: autoComputeConstructionLength(
            tempModelEquipmentList,
            pointGap
          ),
          isDefault,
          modelEquipmentList: tempModelEquipmentList,
          remarks,
          serialNumber,
          tunnelId,
          tunnelLength: totalLength,
          tunnelName,
        })
          .then((res) => {
            if (res?.code === 1000) {
              modelEquipmentList.push(toRaw(equipmentInfo)); //存入本地
              resolve();
            }
            console.log("add", res);
            reject(res);
          })
          .catch((err) => {
            reject();
          });
  });
};

// 删除
export const deleteEquipment = (equipmentId, pointGap) => {
  let tempModelEquipmentList = [...modelEquipmentList]; //数组克隆
  tempModelEquipmentList = tempModelEquipmentList.filter(
    (item) => item.equipmentId != equipmentId
  );
  console.log(
    "del",
    modelEquipmentList,
    tempModelEquipmentList,
    "id",
    equipmentId
  );
  const {
    constructionLength,
    isDefault,
    remarks,
    serialNumber,
    siteId,
    totalLength,
    tunnelId,
    tunnelName,
  } = originData;
  return new Promise((resolve, reject) => {
    apiSaveEquipment({
      constructionLength: autoComputeConstructionLength(
        tempModelEquipmentList,
        pointGap
      ),
      isDefault,
      modelEquipmentList: tempModelEquipmentList,
      remarks,
      serialNumber,
      tunnelId,
      tunnelLength: totalLength,
      tunnelName,
    })
      .then((res) => {
        if (res?.code === 1000) {
          modelEquipmentList = modelEquipmentList.filter(
            (item) => item.equipmentId === equipmentId
          );
          resolve();
        }
        reject(res);
      })
      .catch((err) => {
        reject();
      });
  });
};

function autoComputeConstructionLength(modelEquipmentList, pointGap) {
  let maxLength = 0;
  modelEquipmentList.forEach((item) => {
    const length =
      Number(
        item.position.substring(
          item.position.indexOf("_") + 1,
          item.position.lastIndexOf("_")
        ) - 1
      ) * pointGap;
    if (length > maxLength) {
      maxLength = length;
    }
  });
  return maxLength;
}

function hasEquipment(equipmentId) {
  let result = false;
  modelEquipmentList.forEach((item) => {
    if (item.equipmentId === equipmentId) result = true;
  });
  return result;
}
