///////////////////////////////////////////////////////////////////////////////
// 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 { Viewer } from "../Viewer";
import { OdBaseDragger } from "./Common/OdBaseDragger";

export const MARKUP_ENTITY_LINE = "$MarkupTempEntity_Line";

export class OdaLineDragger extends OdBaseDragger {
  protected entity: any;
  protected points: any[];
  protected drawPoints: any[];

  constructor(subject: Viewer) {
    super(subject);
    this.press = false;
  }

  override dispose(): void {
    super.dispose();
    this.end();
    this.points = null;
    this.drawPoints = null;
  }

  override start(x: number, y: number): void {
    const point = this.getViewer().screenToWorld(x, y);
    this.drawPoints = [point[0], point[1], point[2]];
  }

  override drag(x: number, y: number): void {
    if (this.isDragging) {
      const point = this.getViewer().screenToWorld(x, y);
      this.drawPoints.push(point[0], point[1], point[2]);
      this._updateFrame();
    }
  }

  override end(): void {
    if (this.entity) {
      this.entity.delete();
      this.drawPoints = null;
      this.entity = null;
    }
  }

  private _updateFrame(): void {
    if (this.entity) {
      const model = this.getViewer().getMarkupModel();
      model.removeEntity(this.entity);
      model.delete();
      this.entity.delete();
    }

    this.entity = this.getActiveMarkupEntity(MARKUP_ENTITY_LINE);

    const entityPtr = this.entity.openObject();
    entityPtr.appendPolyline(this.drawPoints).delete();
    entityPtr.delete();
  }
}
