///////////////////////////////////////////////////////////////////////////////
// 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 { Point2d, Point3d } from "../Common/Geometry";
import { ViewParams } from "../Common/OdaGeAction";
import { Viewer } from "../../Viewer";

export class PanAction {
  private _m_module: any;
  private _subject: Viewer;
  private _m_start: Point3d;
  private _deltaScreenPosition: Point2d;
  private _beginInteractivity: () => void;
  private _endInteractivity: () => void;
  private _getViewParams: () => ViewParams;
  private _setViewParams: (params: ViewParams) => void;

  constructor(
    m_module: any,
    subject: Viewer,
    beginInteractivity: () => void,
    endInteractivity: () => void,
    getViewParams: () => ViewParams,
    setViewParams: (params: ViewParams) => void
  ) {
    this._m_module = m_module;
    this._subject = subject;
    this._beginInteractivity = beginInteractivity;
    this._endInteractivity = endInteractivity;
    this._getViewParams = getViewParams;
    this._setViewParams = setViewParams;
  }

  public beginAction(x: number, y: number, absoluteX: number, absoluteY: number) {
    this._m_start = this.screenToWorld(x, y);
    this._deltaScreenPosition = { x: absoluteX, y: absoluteY };
    this._beginInteractivity();
  }

  public action(x: number, y: number, absoluteX: number, absoluteY: number) {
    const { Vector3d } = this._m_module;
    const params = this._getViewParams();
    const pt = this.screenToWorld(x, y);

    const ptSub = this._m_start.sub(pt);
    const delta = ptSub.asVector();

    const target = Vector3d.createFromArray(params.target);
    const targetWithDelta = target.add(delta);
    params.target = targetWithDelta.toArray();

    const position = Vector3d.createFromArray(params.position);
    const positionWithDelta = position.add(delta);
    params.position = positionWithDelta.toArray();

    this._setViewParams(params);
    target.delete();

    targetWithDelta.delete();
    position.delete();
    positionWithDelta.delete();

    this._subject.activeDragger()?.updatePreview?.();
    this._subject.emitEvent({
      type: "pan",
      x: absoluteX,
      y: absoluteY,
      dX: absoluteX - this._deltaScreenPosition.x,
      dY: absoluteY - this._deltaScreenPosition.y,
    });

    this._deltaScreenPosition = { x: absoluteX, y: absoluteY };
  }

  public endAction() {
    this._endInteractivity();
  }

  private screenToWorld(x: number, y: number): Point3d {
    return this._m_module.Point3d.createFromArray(this._m_module.getViewer().screenToWorld(x, y));
  }
}
