import { defineStore } from "pinia";
import { ref, toRaw } from "vue";
import { apiSaveEquipment, initSceneData } from "../api/tunnelScene";

export const useModelSceneStore = defineStore("modelSceneData", () => {
  let equipmentList = ref([]);
  let requestOtherConfig = ref({});

  function initModelData(tunnelId, otherConfig) {
    return new Promise((resolve, reject) => {
      handleSettingOtherConfig(otherConfig, requestOtherConfig);
      initSceneData(tunnelId)
        .then((res) => {
          if (res.data.tunnelThreeConfig) {
            equipmentList.value = JSON.parse(res.data.tunnelThreeConfig);
          } else {
            equipmentList.value = [];
          }

          resolve(toRaw(equipmentList.value));
        })
        .catch((error) => {
          reject();
        });
    });
  }

  function initModelDataPreview(tunnelId) {
    if (tunnelId == 0) {
      return new Promise((resolve, reject) => {
        reject()
      })
    } else {
      return new Promise((resolve, reject) => {
        initSceneData(tunnelId)
          .then((res) => {
            if (res.data.tunnelThreeConfig) {
              equipmentList.value = JSON.parse(res.data.tunnelThreeConfig);
            } else {
              equipmentList.value = [];
            }
            resolve(toRaw(equipmentList.value));
          })
          .catch((error) => {
            reject();
          });
      });
    }
  }

  function addEquipment(equipmentInfo, pointGap) {
    return new Promise((resolve, reject) => {
      if (hasEquipment(equipmentList.value, equipmentInfo.equipmentId)) {
        reject(3000);
        return;
      }
      equipmentList.value.push(equipmentInfo);
      apiSaveEquipment({
        constructionLength: autoComputeConstructionLength(
          equipmentList.value,
          pointGap
        ),
        modelEquipmentList: toRaw(equipmentList.value),
        ...requestOtherConfig,
      })
        .then((res) => {
          console.log("res", res);
          resolve();
        })
        .catch((err) => {
          console.log("res", err);
          equipmentList.value.pop();
          reject();
        });
    });
  }
  //   删除设备
  function deleteEquipment(equipmentId, pointGap) {
    let equipment = null;
    equipmentList.value = equipmentList.value.filter((item) => {
      if (item.equipmentId === equipmentId) {
        equipment = item;
      }
      return item.equipmentId != equipmentId;
    });

    return new Promise((resolve, reject) => {
      apiSaveEquipment({
        constructionLength: autoComputeConstructionLength(
          equipmentList.value,
          pointGap
        ),
        modelEquipmentList: toRaw(equipmentList.value),
        ...requestOtherConfig,
      })
        .then((res) => {
          resolve();
        })
        .catch((err) => {
          equipmentList.value.push(equipment);
          reject();
        });
    });
  }

  return {
    initModelData,
    addEquipment,
    deleteEquipment,
    initModelDataPreview
  };
});

function handleSettingOtherConfig(otherConfig, requestOtherConfig) {
  requestOtherConfig.isDefault = otherConfig.isDefault;
  requestOtherConfig.remarks = otherConfig.remarks;
  requestOtherConfig.serialNumber = otherConfig.serialNumber;
  requestOtherConfig.tunnelId = otherConfig.tunnelId;
  requestOtherConfig.tunnelLength = otherConfig.totalLength;
  requestOtherConfig.tunnelName = otherConfig.tunnelName;
}

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(equipmentList, equipmentId) {
  let result = false;
  equipmentList.forEach((item) => {
    if (item.equipmentId === equipmentId) result = true;
  });
  return result;
}
