import CameraControls from 'camera-controls';
import { Vector3, MathUtils } from 'three';

const _v3A = new Vector3();
export default class MyCameraControls extends CameraControls {
  public truckYDisabled = false;

  // @ts-ignore 重写 CameraControls 方法
  _truckInternal = (deltaX, deltaY, dragToOffset) => {
    const _this = this;
    const offset = _v3A.copy(_this._camera.position).sub(_this._target);
    // @ts-ignore
    const fov = _this._camera.getEffectiveFOV() * MathUtils.DEG2RAD;
    const targetDistance = offset.length() * Math.tan(fov * 0.5);

    const truckX =
      (_this.truckSpeed * deltaX * targetDistance) / _this._elementRect.width;
    const pedestalY =
      (_this.truckSpeed * deltaY * targetDistance) / _this._elementRect.width;
    if (_this.verticalDragToForward) {
      dragToOffset
        ? _this.setFocalOffset(
            _this._focalOffsetEnd.x + truckX,
            _this._focalOffsetEnd.y,
            _this._focalOffsetEnd.z,
            true
          )
        : _this.truck(truckX, 0, true);
      _this.forward(-pedestalY, true);
    } else {
      dragToOffset
        ? _this.setFocalOffset(
            _this._focalOffsetEnd.x + truckX,
            _this._focalOffsetEnd.y + pedestalY,
            _this._focalOffsetEnd.z,
            true
          )
        : _this.truck(this.truckYDisabled ? 0 : truckX, pedestalY, true);
    }

    // const offset = _v3A.copy(this._camera.position).sub(this._target);
    // // half of the fov is center to top of screen
    // // @ts-ignore
    // const fov = this._camera.getEffectiveFOV() * MathUtils.DEG2RAD;
    // const targetDistance = offset.length() * Math.tan(fov * 0.5);

    // const truckX =
    //   (this.truckSpeed * deltaX * targetDistance) / this._elementRect.w;
    // const truckY =
    //   (this.truckSpeed * deltaY * targetDistance) / this._elementRect.w;

    // this.truck(this.truckYDisabled ? 0 : truckX, truckY, true);
  };
}
