import * as THREE from "three";
import { Line2 } from "three/examples/jsm/lines/Line2.js";
import { LineGeometry } from "three/examples/jsm/lines/LineGeometry.js";
import gotoPic from "@/assets/image/goto.png";
const offset = 0.25;

function pointsToArray(arr) {
  let res = [];
  arr.forEach((i) => {
    res = res.concat(i.toArray());
  });
  return res;
}
let m = new THREE.MeshBasicMaterial({
  map: new THREE.TextureLoader().load(gotoPic),
  color: 0x26559b,
  transparent: true,
});
export default class LinePoints extends Line2 {
  constructor(points, matLine, dir, imgId, id) {
    let g = new THREE.PlaneGeometry(0.1, 0.1);
    g.rotateX(-Math.PI / 2);
    let cross = new THREE.Mesh(g, m);
    let a = new THREE.Vector3(points[0], points[1], points[2]);
    let b = new THREE.Vector3(points[3], points[4], points[5]);
    let c = new THREE.Vector3(points[6], points[7], points[8]);
    let d = new THREE.Vector3(points[9], points[10], points[11]);
    let diff;
    switch (dir) {
      case 0:
        //top
        cross.rotation.y = -Math.PI / 2;
        cross.position.copy(d);
        break;
      case 1:
        //left
        diff = c.x < d.x;
        cross.rotation.y = diff ? 0 : -Math.PI;
        diff ? (cross.position.x -= 0.02) : (cross.position.x += 0.02);

        break;
      case 2:
        //bottom
        cross.position.copy(d);
        cross.rotation.y = Math.PI / 2;
        cross.position.z += 0.02;
        break;
      case 3:
        //right
        diff = c.x < d.x;
        cross.position.copy(d);
        cross.rotation.y = diff ? 0 : Math.PI;
        diff ? (cross.position.x -= 0.02) : (cross.position.x += 0.02);
        break;
    }
    const geometry = new LineGeometry();
    cross.visible = false;
    // console.log("points", points);
    geometry.setPositions(points);
    super(geometry, matLine);
    this.name = "line_point_" + this.uuid;
    this.scale.set(1, 1, 1);
    this.position.y += 0.5;
    if (id) {
      this.uuid = id;
    }
    this.userData = {
      id: id || this.uuid,
      dir: dir,
      points,
      imgId: imgId || null,
    };
    this.add(cross);
  }
}
