import {Float32BufferAttribute} from "three/src/core/BufferAttribute";
import {BufferGeometry as BufferGeometry2} from "three/src/core/BufferGeometry";
import {TypedSopNode} from "./_Base";
import {ObjectType} from "../../../core/geometry/Constant";
import {NodeParamsConfig, ParamConfig} from "../utils/params/ParamsConfig";
class LineSopParamsConfig extends NodeParamsConfig {
  constructor() {
    super(...arguments);
    this.length = ParamConfig.FLOAT(1, {range: [0, 10]});
    this.pointsCount = ParamConfig.INTEGER(1, {
      range: [2, 100],
      rangeLocked: [true, false]
    });
    this.origin = ParamConfig.VECTOR3([0, 0, 0]);
    this.direction = ParamConfig.VECTOR3([0, 1, 0]);
  }
}
const ParamsConfig2 = new LineSopParamsConfig();
export class LineSopNode extends TypedSopNode {
  constructor() {
    super(...arguments);
    this.params_config = ParamsConfig2;
  }
  static type() {
    return "line";
  }
  initializeNode() {
  }
  cook() {
    const pointsCount = Math.max(2, this.pv.pointsCount);
    const positions = new Array(pointsCount * 3);
    const indices = new Array(pointsCount);
    const last_pt = this.pv.direction.clone().normalize().multiplyScalar(this.pv.length);
    for (let i = 0; i < pointsCount; i++) {
      const i_n = i / (pointsCount - 1);
      const point = last_pt.clone().multiplyScalar(i_n);
      point.add(this.pv.origin);
      point.toArray(positions, i * 3);
      if (i > 0) {
        indices[(i - 1) * 2] = i - 1;
        indices[(i - 1) * 2 + 1] = i;
      }
    }
    const geometry = new BufferGeometry2();
    geometry.setAttribute("position", new Float32BufferAttribute(positions, 3));
    geometry.setIndex(indices);
    this.setGeometry(geometry, ObjectType.LINE_SEGMENTS);
  }
}
