var lineAB = new THREE.Vector3(0, 0, 0);
var lineBC = new THREE.Vector3(0, 0, 0);
var crossResult = new THREE.Vector3(0, 0, 0);

// update view
async function refreshClipMesh(plane, mesh, imageName, callback) {
  let points = LineIntersectionHelper.getLineIntersectionPoints(
    EdgesManager.getCubeEdges(),
    plane
  );
  if (points.length < 3) {
    return false;
  }

  //get clipPlaneData From WPF
  var clipPlaneData = await getClipPlaneData();
  if (clipPlaneData.ErrorCode != 1000) {
    console.error("get data error :" + clipPlaneData.ErrorCode);
    return false;
  } else {
    updateMesh();
    return true;
  }

  async function getClipPlaneData() {
    //convert points
    convertToVTKPoints(points);
    let mappingPoints = [];
    points.forEach((item) =>
      mappingPoints.push(
        new cefMappingPoint(
          Math.abs(item.x),
          Math.abs(item.y),
          Math.abs(item.z)
        )
      )
    );
    let inputData = {
      PointsList: mappingPoints,
      ClipImageName: imageName,
    };
    // let json = jsObj.GetClipPlaneData(
    //     OSHelper.convertCefParameter(inputData));
    let json = await Dart_GetClipPlaneData(JSON.stringify(inputData));
    var clipPlaneData=JSON.parse(json)
    return  clipPlaneData;
  }

  function updateMesh() {
    //get verticePoints
    convertBackToWorldPoints(clipPlaneData.WorldPoints);
    let verticesPoints = [];
    clipPlaneData.WorldPoints.forEach((item) =>
      verticesPoints.push(new THREE.Vector3(item.X, item.Y, item.Z))
    );

    //get imageVectors
    var imageVectors = [];
    clipPlaneData.ImagePoints.forEach((item) =>
      imageVectors.push(new THREE.Vector2(item.X, item.Y))
    );

    LoaderUtility.loadBase64Texture(clipPlaneData.ImageData, (texture) => {
      mesh.material.map.dispose();
      mesh.material.map = texture;
      updateMeshGeometry();
      if (callback != undefined) {
        callback();
      }
    });
    // LoaderUtility.loadUniqueTexture(clipPlaneData.ImageData + "//" + imageName,
    //     texture => {
    //         mesh.material.map.dispose();
    //         mesh.material.map = texture;
    //         updateMeshGeometry();
    //         if (callback != undefined) {
    //             callback();
    //         }
    //     });

    ///更新Mesh
    function updateMeshGeometry() {
      mesh.geometry.vertices = verticesPoints;
      mesh.geometry.faces = [];
      mesh.geometry.faceVertexUvs[0] = [];
      lineAB.subVectors(verticesPoints[1], verticesPoints[0]);
      lineBC.subVectors(verticesPoints[2], verticesPoints[1]);
      crossResult.crossVectors(lineAB, lineBC);
      let dotResult = crossResult.dot(plane.normal);
      let faceId = 0;
      for (let i = 2; i < verticesPoints.length; i++) {
        if (dotResult <= 0) {
          setMeshGeometryFace(0, i - 1, i);
        } else {
          setMeshGeometryFace(0, i, i - 1);
        }
      }
      mesh.material.needsUpdate = true;
      mesh.geometry.elementsNeedUpdate = true;
      mesh.geometry.computeBoundingSphere();
      ClipPlaneManager.drawActiveMeshEdges();
      function setMeshGeometryFace(x, y, z) {
        mesh.geometry.faces.push(new THREE.Face3(x, y, z));
        mesh.geometry.faceVertexUvs[0][faceId++] = [
          imageVectors[x],
          imageVectors[y],
          imageVectors[z],
        ];
      }
    }
  }
}
