///////////////////////////////////////////////////////////////////////////////
// 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 { Object3D, Plane, Vector3 } from "three";
import { TransformControls } from "three/examples/jsm/controls/TransformControls.js";

import type { Viewer } from "../Viewer";
import { PlaneHelper } from "../helpers/PlaneHelper";
import { OrbitDragger } from "./OrbitDragger";

export class CuttingPlaneDragger extends OrbitDragger {
  protected plane: Plane;
  protected planeCenter: Object3D;
  protected planeHelper: PlaneHelper;
  protected transform: TransformControls;

  constructor(viewer: Viewer, normal: Vector3, color: number) {
    super(viewer);

    const size = viewer.extents.getSize(new Vector3()).length();
    const center = viewer.extents.getCenter(new Vector3());
    const constant = -center.dot(normal);

    this.plane = new Plane(normal, constant);

    if (!viewer.renderer.clippingPlanes) viewer.renderer.clippingPlanes = [];
    viewer.renderer.clippingPlanes.push(this.plane);

    this.planeHelper = new PlaneHelper(this.plane, size, color, center);
    this.viewer.helpers.add(this.planeHelper);

    this.planeCenter = new Object3D();
    this.planeCenter.position.copy(viewer.extents.getCenter(new Vector3()));
    this.viewer.helpers.add(this.planeCenter);

    this.transform = new TransformControls(viewer.camera, viewer.canvas);
    this.transform.showX = !!normal.x;
    this.transform.showY = !!normal.y;
    this.transform.showZ = !!normal.z;
    this.transform.attach(this.planeCenter);
    this.transform.addEventListener("change", this.transformChange);
    this.transform.addEventListener("dragging-changed", this.transformDrag);
    this.viewer.helpers.add(this.transform.getHelper());

    this.viewer.addEventListener("explode", this.updatePlaneSize);
    this.viewer.addEventListener("show", this.updatePlaneSize);
    this.viewer.addEventListener("showall", this.updatePlaneSize);
    this.viewer.addEventListener("changecameramode", this.updateTransformCamera);
    this.viewer.canvas.addEventListener("dblclick", this.onDoubleClick, true);
    this.viewer.update();
  }

  override dispose() {
    this.viewer.removeEventListener("explode", this.updatePlaneSize);
    this.viewer.removeEventListener("show", this.updatePlaneSize);
    this.viewer.removeEventListener("showall", this.updatePlaneSize);
    this.viewer.removeEventListener("changecameramode", this.updateTransformCamera);
    this.viewer.canvas.removeEventListener("dblclick", this.onDoubleClick, true);

    this.transform.removeEventListener("change", this.transformChange);
    this.transform.removeEventListener("dragging-changed", this.transformDrag);
    this.transform.getHelper().removeFromParent();
    this.transform.detach();
    this.transform.dispose();

    this.planeHelper.removeFromParent();
    this.planeHelper.dispose();

    this.planeCenter.removeFromParent();

    // this.viewer.renderer.clippingPlanes = this.viewer.renderer.clippingPlanes.filter((plane) => plane !== this.plane);
    // this.viewer.update();

    super.dispose();
  }

  transformChange = () => {
    this.plane.constant = -this.planeCenter.position.dot(this.plane.normal);
    this.viewer.update();
  };

  transformDrag = (event) => {
    this.orbit.enabled = !event.value;
  };

  updatePlaneSize = () => {
    this.planeHelper.size = this.viewer.extents.getSize(new Vector3()).length();
    this.viewer.update();
  };

  updateTransformCamera = () => {
    this.transform.camera = this.viewer.camera;
  };

  onDoubleClick = (event: PointerEvent) => {
    event.stopPropagation();

    this.plane.negate();
    this.viewer.update();
  };
}
