import { PointModel } from "../../point-component/models/point-model";
import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";

export class IntersectionModel extends PointModel {
  constructor(posX, posY, label, r, s) {
    super(posX, posY, label);
    this.r = r;
    this.s = s;
    super.setClass(ELEMENTS_CLASS.INTERSECTION_POINT);
  }

  update(aggregator, event) {
    const intersection = this.r.getIntersection(this.s);
    this.posX = parseFloat(intersection.posX.toFixed(2));
    this.posY = parseFloat(intersection.posY.toFixed(2));
    if (!this.r.insideSegment(this.posX, this.posY)) {
      this.posX = Number.MAX_SAFE_INTEGER;
      this.posY = Number.MAX_SAFE_INTEGER;
      this.visible = false;
      return;
    }

    if (!this.s.insideSegment(this.posX, this.posY)) {
      this.posX = Number.MAX_SAFE_INTEGER;
      this.posY = Number.MAX_SAFE_INTEGER;
      this.visible = false;
      return;
    }
    this.visible = true;
  }
}