import {TypedObjNode} from "./_Base";
import {Group as Group2} from "three/src/objects/Group";
import {FlagsControllerD} from "../utils/FlagsController";
import {AxesHelper as AxesHelper2} from "three/src/helpers/AxesHelper";
import {HierarchyController as HierarchyController2} from "./utils/HierarchyController";
import {Matrix4 as Matrix42} from "three/src/math/Matrix4";
import {Vector3 as Vector32} from "three/src/math/Vector3";
import {MathUtils as MathUtils2} from "three/src/math/MathUtils";
import {Quaternion as Quaternion2} from "three/src/math/Quaternion";
import {NodeParamsConfig, ParamConfig} from "../utils/params/ParamsConfig";
class PolarTransformObjParamConfig extends NodeParamsConfig {
  constructor() {
    super(...arguments);
    this.center = ParamConfig.VECTOR3([0, 0, 0]);
    this.longitude = ParamConfig.FLOAT(0, {
      range: [0, 360]
    });
    this.latitude = ParamConfig.FLOAT(0, {
      range: [-180, 180]
    });
    this.depth = ParamConfig.FLOAT(1, {
      range: [0, 10]
    });
  }
}
const ParamsConfig2 = new PolarTransformObjParamConfig();
const HOOK_NAME = "_cook_main_without_inputs_when_dirty";
const AXIS_VERTICAL = new Vector32(0, 1, 0);
const AXIS_HORIZONTAL = new Vector32(-1, 0, 0);
export class PolarTransformObjNode extends TypedObjNode {
  constructor() {
    super(...arguments);
    this.params_config = ParamsConfig2;
    this.hierarchy_controller = new HierarchyController2(this);
    this.flags = new FlagsControllerD(this);
    this._helper = new AxesHelper2(1);
    this._cook_main_without_inputs_when_dirty_bound = this._cook_main_without_inputs_when_dirty.bind(this);
    this._centerMatrix = new Matrix42();
    this._longitudeMatrix = new Matrix42();
    this._latitudeMatrix = new Matrix42();
    this._depthMatrix = new Matrix42();
    this._fullMatrix = new Matrix42();
    this._decomposed = {
      t: new Vector32(),
      q: new Quaternion2(),
      s: new Vector32()
    };
  }
  static type() {
    return "polarTransform";
  }
  create_object() {
    const group = new Group2();
    group.matrixAutoUpdate = false;
    return group;
  }
  initializeNode() {
    this.hierarchy_controller.initializeNode();
    if (!this.dirtyController.has_hook(HOOK_NAME)) {
      this.dirtyController.addPostDirtyHook(HOOK_NAME, this._cook_main_without_inputs_when_dirty_bound);
    }
    this._updateHelperHierarchy();
    this._helper.matrixAutoUpdate = false;
    this.flags.display.add_hook(() => {
      this._updateHelperHierarchy();
    });
  }
  _updateHelperHierarchy() {
    if (this.flags.display.active()) {
      this.object.add(this._helper);
    } else {
      this.object.remove(this._helper);
    }
  }
  async _cook_main_without_inputs_when_dirty() {
    await this.cookController.cook_main_without_inputs();
  }
  cook() {
    const object = this.object;
    this._centerMatrix.identity();
    this._longitudeMatrix.identity();
    this._latitudeMatrix.identity();
    this._depthMatrix.identity();
    this._centerMatrix.makeTranslation(this.pv.center.x, this.pv.center.y, this.pv.center.z);
    this._longitudeMatrix.makeRotationAxis(AXIS_VERTICAL, MathUtils2.degToRad(this.pv.longitude));
    this._latitudeMatrix.makeRotationAxis(AXIS_HORIZONTAL, MathUtils2.degToRad(this.pv.latitude));
    this._depthMatrix.makeTranslation(0, 0, this.pv.depth);
    this._fullMatrix.copy(this._centerMatrix).multiply(this._longitudeMatrix).multiply(this._latitudeMatrix).multiply(this._depthMatrix);
    this._fullMatrix.decompose(this._decomposed.t, this._decomposed.q, this._decomposed.s);
    object.position.copy(this._decomposed.t);
    object.quaternion.copy(this._decomposed.q);
    object.scale.copy(this._decomposed.s);
    object.updateMatrix();
    this.cookController.end_cook();
  }
}
