import { MarkerNodeEntityProcessor } from "./MarkerNodeEntityProcessor.js";
import { SurfacePoint3 } from "../../../../core/geom/3d/SurfacePoint3.js";
import { alignToVector } from "../../../../engine/ecs/terrain/ecs/cling/ClingToTerrainSystem.js";
import { obtainTerrain } from "../../../../engine/ecs/terrain/util/obtainTerrain.js";

const p = new SurfacePoint3();

export class MarkerNodeEntityProcessorClingToTerrain extends MarkerNodeEntityProcessor {

    /**
     * @private
     * @type {Terrain}
     */
    terrain = null;

    /**
     *
     * @type {boolean}
     */
    normalAlign = false;

    /**
     *
     * @param {boolean} [normalAlign=false]
     * @return {MarkerNodeEntityProcessorClingToTerrain}
     */
    static from({ normalAlign = false } = {}) {
        const r = new MarkerNodeEntityProcessorClingToTerrain();

        r.normalAlign = normalAlign;

        return r;
    }

    initialize(data, ecd) {
        this.terrain = obtainTerrain(ecd);
    }

    execute(entity, node, data, ecd) {
        const transform = node.transform;

        const contact_found = this.terrain.raycastVerticalFirstSync(p, transform.position.x, transform.position.z);

        if (contact_found) {
            transform.position.setY(p.position.y);

            if (this.normalAlign) {
                alignToVector(transform.rotation, p.normal, 1000);
            }

        }

    }
}
