import Demo from "../demo";
import EquipmentTag from "../utils/EquipmentTag";
/**
 *
 * @param {Mesh} targetPoint
 * @param {String} equType  "fan" "sensors"
 */
//formInfo需要的信息这里包括了
//equipmentType

function addEquipment(targetPoint, formInfo) {
  if (targetPoint.hasDevice) {
    return;
  }
  console.log("info", formInfo);
  switch (formInfo.equipmentType) {
    case "frequency":
      handleFanEqu.call(this, targetPoint, formInfo);
      break;
    // 还没有风压阀的模型，所以先用其他设备传感器实现
    // case "valve":
    //   handleValveEqu.call(this, targetPoint, formInfo.chooseEquipment);
    //   break;
    case "windPressure":
    case "sensor":
      handleOtherEqu.call(this, targetPoint, formInfo);
      break;
    default:
      break;
  }
  targetPoint.hasDevice = true;
  // 标识设备信息
  this.clearTagsObj();
}

// function handleValveEqu(targetPoint, equipmentInfo) {

// }
let allline = new Array
// 其他传感器
function handleOtherEqu(targetPoint, equipmentInfo) {
  const equMesh = this.equMap.get("equ_sensors").clone();
  const worldP = targetPoint.getWorldPosition(new this.THREE.Vector3());
  equMesh.position.copy(worldP);

  //设备添加标签
  const tag = EquipmentTag(equipmentInfo.equipmentName);
  equMesh.getObjectByName("tag").material = tag;
  if (/tr$/.test(targetPoint.name)) {
    equMesh.rotation.z = -Math.PI / 2;
    equMesh.rotation.y -= Math.PI / 4;
    equMesh.getObjectByName("tag").rotation.z += Math.PI; // 注意在原来的基础上加
  } else if (/tl$/.test(targetPoint.name)) {
    equMesh.rotation.z = Math.PI / 2;
    equMesh.rotation.y += Math.PI / 4;
    equMesh.getObjectByName("tag").rotation.z += Math.PI;
  } else if (/tc$/.test(targetPoint.name)) {
    equMesh.rotation.z = Math.PI;
    equMesh.getObjectByName("tag").rotation.z += Math.PI;
  } else if (/br$/.test(targetPoint.name)) {
    equMesh.rotation.z = -Math.PI / 2;
    equMesh.getObjectByName("tag").rotation.z += Math.PI;
  } else if (/bl$/.test(targetPoint.name)) {
    equMesh.rotation.z = Math.PI / 2;
    equMesh.getObjectByName("tag").rotation.z += Math.PI;
  }
  targetPoint.scale.set(0.03, 0.06, 0.05);
  this.scene.add(equMesh);
  targetPoint.visible = false;
  targetPoint.hasWall = true;
  //处理风压阀的代码片段
  let lineIndex = targetPoint.name.slice(7, 9)
  if (allline.includes(Number(lineIndex))) {
    allline.slice(array.indexOf(lineIndex), 1)
    console.log('dadad');
  } else {
    allline.push(Number(lineIndex))
  }
  console.log(allline);
  // console.log(allline);
  // allline.forEach((item, index, array) => {
  //   if (array.includes(item)) {
  //     array.slice(array.indexOf(item), 1)
  //     array.slice(array.indexOf(item), 1)
  //     console.log(array);
  //   }
  // })
  // console.log(this.scene);
  // 保存该设备模型id，后期直接从附附着点进行删除
  targetPoint.info = {
    id: equMesh.id,
    ...equipmentInfo,
  };
  //下面进行模拟分压阀的添加效果
  // 1.先取出当前点击添加设备锚点的行数
  // 2.再进行字符串拼接出墙的名字，使用scene.getObjectByName(墙名去获取)
  // 3.加上循环，固定到20结束就可以了，将点击的函数到20的函数的墙全部设为visable设为可见,还需要将中间的那行设备和附着点全部设为隐藏，且层级变低，无法选中
}

// 风机
function handleFanEqu(
  targetPoint,
  equipmentInfo,
  speed = Math.random().toFixed(1) * 1000
) {
  // 由于风机比较多，每个风机转速不一直，保存在一个数中遍历
  if (!this.fanSpinArray) {
    this.fanSpinArray = [];
  }
  const equMesh = this.equMap.get("equ_fan").clone();
  const worldP = targetPoint.getWorldPosition(new this.THREE.Vector3());
  equMesh.position.copy(worldP);
  equMesh.translateY(-0.6);
  this.scene.add(equMesh);
  targetPoint.visible = false;
  // 放大附着点方便选中风机的范围
  targetPoint.scale.set(0.06, 0.06, 0.06);
  // 保存该设备id，后期直接从附附着点进行删除
  targetPoint.info = {
    id: equMesh.id, //模型在场景id
    ...equipmentInfo,
  };

  // 定义风机旋转
  const fanLeaf = equMesh.getObjectByName("fan_leafs");
  const fanSpin = new this.TWEEN.Tween({ rotation: 0 });
  fanSpin.to({ rotation: 2 * Math.PI }, speed);
  fanSpin.repeat(Infinity);
  fanSpin.start();
  fanSpin.onUpdate((obj) => {
    fanLeaf.rotation.z = obj.rotation;
  });
  this.fanSpinArray.push(fanSpin);
}

// 删除传感器
function removeEquipment(targetPoint) {
  //删除设备这里需要再进行处理，就是根据附着点（附着点一定保存了当前添加设备的信息了）判断是不是分压阀，然后继续一样的隐藏
  if (!targetPoint.hasDevice) return;
  // if (targetPoint.hasWall == true) {
  //   let lineIndex = targetPoint.name.slice(7, 9);
  //   for (let line = Number(lineIndex); line <= 20; line++) {
  //     if (line < 10) {
  //       let wall = "wall_" + "0" + line;
  //       this.scene.getObjectByName(wall).visible = false;
  //       let point = "point_" + "00" + line + "_tc";
  //       console.log(this.scene.getObjectByName(point));
  //       this.scene.getObjectByName(point).visible = true;
  //       this.scene.getObjectByName(point).layers.set(0);
  //       if (this.scene.getObjectByName(point).hasDevice == true) {
  //         this.scene.getObjectByName(point).visible = false;
  //         let EquID = this.scene.getObjectByName(point).info.id;
  //         this.scene.getObjectById(EquID).visible = true;
  //       }
  //     } else if (line >= 10) {
  //       let wall = "wall_" + line;
  //       this.scene.getObjectByName(wall).visible = false;
  //       let point = "point_" + "0" + line + "_tc";
  //       this.scene.getObjectByName(point).visible = true;
  //       this.scene.getObjectByName(point).layers.set(0);
  //       if (this.scene.getObjectByName(point).hasDevice == true) {
  //         this.scene.getObjectByName(point).visible = false;
  //         let EquID = this.scene.getObjectByName(point).info.id;
  //         this.scene.getObjectById(EquID).visible = true;
  //       }
  //     }
  //     //处理21行的设备和附着点
  //     let point = "point_" + "0" + 21 + "_tc";
  //     this.scene.getObjectByName(point).visible = true;
  //     this.scene.getObjectByName(point).layers.set(0);
  //     if (this.scene.getObjectByName(point).hasDevice == true) {
  //       let EquID = this.scene.getObjectByName(point).info.id;
  //       this.scene.getObjectById(EquID).visible = true;
  //     }
  //   }
  // }
  console.log("测试是不是删除了");
  const mesh = this.scene.getObjectById(targetPoint.info.id);
  this.scene.remove(mesh);
  targetPoint.visible = true;
  targetPoint.hasDevice = false;
  targetPoint.scale.set(0.01, 0.01, 0.01);
  delete targetPoint.info; // 清空设备信息
}
export { addEquipment, removeEquipment };
