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 Line extends Line2 {
  constructor(startPoint, endPoint, endEdge, matLine) {
    let points;

    let g = new THREE.PlaneGeometry(0.1, 0.1);
    g.rotateX(-Math.PI / 2);
    let cross = new THREE.Mesh(g, m);

    if (endEdge.name === 0) {
      // top
      let a = startPoint.clone();
      let b = new THREE.Vector3(
        startPoint.x,
        startPoint.y,
        endEdge.y + endEdge.parent.parent.position.z - offset
      );
      let c = new THREE.Vector3(
        endPoint.x,
        endPoint.y,
        endEdge.y + endEdge.parent.parent.position.z - offset
      );
      let d = new THREE.Vector3(
        endPoint.x,
        endPoint.y,
        endEdge.y + endEdge.parent.parent.position.z
      );
      cross.position.copy(d);
      cross.rotation.y = -Math.PI / 2;
      cross.position.z -= 0.02;

      points = pointsToArray([a, b, c, d]);
    } else if (endEdge.name === 1) {
      //left

      let a = startPoint.clone();
      let b = new THREE.Vector3(
        (startPoint.x + endPoint.x) / 2,
        startPoint.y,
        startPoint.z
      );
      let c = new THREE.Vector3(
        (startPoint.x + endPoint.x) / 2,
        startPoint.y,
        endPoint.z
      );
      let d = new THREE.Vector3(
        endEdge.x + endEdge.parent.parent.parent.position.x,
        startPoint.y,
        endPoint.z
      );
      cross.position.copy(d);

      const diff = c.x < d.x;
      cross.rotation.y = diff ? 0 : -Math.PI;
      diff ? (cross.position.x -= 0.02) : (cross.position.x += 0.02);

      points = pointsToArray([a, b, c, d]);
    } else if (endEdge.name === 2) {
      //bottom
      let a = startPoint.clone();
      let b = new THREE.Vector3(
        startPoint.x,
        startPoint.y,
        endEdge.y + endEdge.parent.parent.position.z + offset
      );
      let c = new THREE.Vector3(
        endPoint.x,
        endPoint.y,
        endEdge.y + endEdge.parent.parent.position.z + offset
      );
      let d = new THREE.Vector3(
        endPoint.x,
        endPoint.y,
        endEdge.y + endEdge.parent.parent.position.z
      );
      cross.rotation.y = Math.PI / 2;
      cross.position.copy(d);
      cross.position.z += 0.02;
      points = pointsToArray([a, b, c, d]);
    } else {
      //right
      let a = startPoint.clone();
      let b = new THREE.Vector3(
        (startPoint.x + endPoint.x) / 2,
        startPoint.y,
        startPoint.z
      );
      let c = new THREE.Vector3(
        (startPoint.x + endPoint.x) / 2,
        startPoint.y,
        endPoint.z
      );
      let d = new THREE.Vector3(
        endEdge.x + endEdge.parent.parent.parent.position.x,
        startPoint.y,
        endPoint.z
      );
      const 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);

      points = pointsToArray([a, b, c, d]);
    }

    const geometry = new LineGeometry();

    cross.visible = false;
    geometry.setPositions(points);
    super(geometry, matLine);
    this.name = "line_" + this.uuid;
    this.userData = {
      dir: endEdge.name,
      points: points,
    };
    this.scale.set(1, 1, 1);
    this.position.y += 0.5;
    this.add(cross);
  }
}
