///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2025, Open Design Alliance (the "Alliance").
// All rights reserved.
//
// This software and its documentation and related materials are owned by
// the Alliance. The software may only be incorporated into application
// programs owned by members of the Alliance, subject to a signed
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable
// trade secrets of the Alliance and its suppliers. The software is also
// protected by copyright law and international treaty provisions. Application
// programs incorporating this software must include the following statement
// with their copyright notices:
//
//   This application incorporates Open Design Alliance software pursuant to a
//   license agreement with Open Design Alliance.
//   Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance.
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////
import { Point3d } from "../Common/Geometry";

export class OdSelectionFrame {
  protected m_start: number[];
  protected m_end: number[];
  protected m_model: any;
  protected m_module: any;
  protected m_entity: any;
  protected m_frame: any;

  constructor() {
    this.m_start = [0, 0, 0]; // 0
    this.m_end = [0, 0, 0]; // 2

    this.m_model = null;
  }

  createPoint3d(): Point3d {
    return new this.m_module.Point3d();
  }

  init(instance: any, model: any): void {
    this.m_module = instance;
    this.m_model = model;
  }

  getViewer() {
    return this.m_module.getViewer();
  }

  setValue(value: number[]): void {
    this.m_end = value;
    this.draw();
  }

  setStartPoint(point: number[]): void {
    this.m_start = point;
    this.m_end = point;
    this.draw();
  }

  toDoubleArray(points: Point3d[]): number[] {
    const p: number[] = [];
    for (let i = 0; i < points.length; i++) {
      p.push(points[i].x);
      p.push(points[i].y);
      p.push(points[i].z);
    }
    return p;
  }

  draw(): void {
    const view = this.getViewer().activeView;
    const viewM = view.viewingMatrix;

    const points = [];
    points.push(this.m_start);
    points.push(this.createPoint3d());
    points.push(this.m_end);
    points.push(this.createPoint3d());

    const p0 = this.createPoint3d();
    p0.set(this.m_start[0], this.m_start[1], this.m_start[2]);

    const p2 = this.createPoint3d();
    p2.set(this.m_end[0], this.m_end[1], this.m_end[2]);

    p0.transformBy(viewM);
    p2.transformBy(viewM);

    points[1].x = p0.x;
    points[3].x = p2.x;
    points[1].y = p2.y;
    points[3].y = p0.y;
    points[1].z = points[3].z = p2.z;

    const eyeM = view.eyeToWorldMatrix;
    points[1].transformBy(eyeM);
    points[3].transformBy(eyeM);

    if (!this.m_entity) {
      this.m_entity = this.m_model.appendEntity("");
      const entityPtr = this.m_entity.openObject();

      entityPtr.setColor(112, 112, 112);
      entityPtr.setLineWeight(2);

      this.m_frame = entityPtr.appendPolygon(this.toDoubleArray(points));
      entityPtr.delete();
    } else {
      this.m_frame.openAsPolygon().setPoints(this.toDoubleArray(points));
    }
  }

  clear(): void {
    if (this.m_entity) {
      this.m_model.removeEntity(this.m_entity);
      this.m_entity = null;
    }
  }
}
