///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2026, 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-2026 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 { Point3d } from "./Common/Geometry";
import { OdBaseDragger } from "./Common/OdBaseDragger";

export class OdBaseCuttingPlaneDragger extends OdBaseDragger {
  protected m_size: number;
  protected m_center: Point3d;
  protected m_normal: number[];
  protected index: number;
  protected m_model: any;
  protected m_entity: any;
  protected m_planePreview: any;
  protected m_last: Point3d;

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

    //this.getViewer().getMarkupController().clear();

    const ext = this.getViewer().getActiveExtents();
    const min = ext.min();
    const max = ext.max();

    const sizeX = Math.abs(max[0] - min[0]);
    const sizeY = Math.abs(max[1] - min[1]);
    const sizeZ = Math.abs(max[2] - min[2]);

    this.m_size = Math.sqrt(sizeX * sizeX + sizeY * sizeY + sizeZ * sizeZ) || 1;

    this.m_center = this.toPoint(ext.center());
    this.m_normal = this.createNormal();

    const avp = this.getViewer().activeView;
    const plane = this.createPlane();
    plane.set(this.toGePoint(this.m_center), this.m_normal);

    avp.addCuttingPlane(plane);
    this.index = avp.numCuttingPlanes() - 1;
    this.subject.syncOptions(); // <- sync cutting plane fill options

    this.subject.emitEvent({ type: "changecuttingplanes" });

    this.m_model = this.getModel();
    this.createPreview();

    this.deleteAll([ext, avp, plane]);
  }

  override dispose(): void {
    super.dispose();

    if (this.m_entity) {
      this.m_model.removeEntity(this.m_entity);
      this.deleteAll([this.m_model, this.m_entity, this.m_planePreview, this.m_center]);
      this.m_entity = null;
      this.m_planePreview = null;
      this.subject.update();
    }
  }

  createNormal(): number[] {
    return [0, 0, 0];
  }

  handleDelta(delta: Point3d): Point3d {
    return delta;
  }

  getPlanePreviewCoordinate(): any[] {
    return [];
  }

  override start(x: number, y: number): void {
    this.press = true;
    this.m_last = this.screenToWorld(x, y);
  }

  override drag(x: number, y: number): void {
    if (this.press) {
      const point = this.screenToWorld(x, y);
      const delta = this.handleDelta(point.sub(this.m_last));

      const oldCenter = this.m_center;
      this.m_center = oldCenter.add(delta);

      const oldLast = this.m_last;
      this.m_last = point;

      const avp = this.getViewer().activeView;
      const plane = this.createPlane();
      plane.set(this.toGePoint(this.m_center), this.m_normal);
      avp.updateCuttingPlane(this.index, plane);

      this.subject.emitEvent({ type: "changecuttingplanes" });

      this.drawPreview();

      this.deleteAll([avp, plane, oldCenter, delta, oldLast]);

      const device = this.getViewer().getActiveDevice();
      device.invalidate(device.getSize());
    }
  }

  override end(x: number, y: number): void {
    this.press = false;
  }

  override dblclick(ev: MouseEvent): void {
    this.m_normal = [this.m_normal[0] * -1, this.m_normal[1] * -1, this.m_normal[2] * -1];

    const avp = this.getViewer().activeView;
    const plane = this.createPlane();
    plane.set(this.toGePoint(this.m_center), this.m_normal);
    avp.updateCuttingPlane(this.index, plane);

    this.subject.emitEvent({ type: "changecuttingplanes" });

    this.deleteAll([avp, plane, this.m_last]);

    const device = this.getViewer().getActiveDevice();
    device.invalidate(device.getSize());
  }

  createPreview(): void {
    this.m_entity = this.m_model.appendEntity("&CuttingPlanePreview");

    const GeometryTypes = this.m_module.GeometryTypes;

    const facesColorDef = new this.m_module.OdTvColorDef(0xf0, 0xf0, 0xf0);
    const facesTransparencyDef = new this.m_module.OdTvTransparencyDef(0.7);

    const edgesColorDef = new this.m_module.OdTvColorDef(0xd0, 0xd0, 0xd0);

    const entityPtr = this.m_entity.openObject();

    entityPtr.setColor(facesColorDef, GeometryTypes.kFaces);
    entityPtr.setTransparency(facesTransparencyDef, GeometryTypes.kFaces);

    entityPtr.setColor(edgesColorDef, GeometryTypes.kEdges);

    entityPtr.setLineWeight(2);

    this.m_planePreview = entityPtr.appendPolygon(this.getPlanePreviewCoordinate());
    const polygonPtr = this.m_planePreview.openAsPolygon();
    polygonPtr.setFilled(true);

    this.deleteAll([facesColorDef, facesTransparencyDef, edgesColorDef, entityPtr, polygonPtr, GeometryTypes]);
  }

  drawPreview(): void {
    const polygonPtr = this.m_planePreview.openAsPolygon();
    polygonPtr.setPoints(this.getPlanePreviewCoordinate());
    this.deleteAll([polygonPtr]);
  }
}
