import * as THREE from 'three';

import * as Types from "../../../../types/world/worldTypes";

function updateReferencePolesheight(
    intersectionPoint: Types.Vector3,
    distance: Types.Number,
    referencePole: Types.RefMesh,
    floorGroup: Types.RefGroup
): void {

    //////////  Add a Reference Pillar and update its position and scale based on the pointer interaction  //////////

    if (referencePole.current) {
        (<any>referencePole.current.material).dispose();
        (<any>referencePole.current.geometry).dispose();
        floorGroup.current.remove(referencePole.current);
        referencePole.current.geometry.dispose();
    }

    const shape = new THREE.Shape();
    shape.moveTo(0.5, 0);
    shape.absarc(0, 0, 0.5, 0, 2 * Math.PI, false);

    const extrudeSettings = {
        depth: distance,
        bevelEnabled: false,
    };

    const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
    const material = new THREE.MeshBasicMaterial({ color: "green", transparent: true, opacity: 0.5 });
    referencePole.current = new THREE.Mesh(geometry, material);
    referencePole.current.rotateX(Math.PI / 2);
    referencePole.current.position.set(intersectionPoint.x, intersectionPoint.y - 0.01, intersectionPoint.z);
    referencePole.current.userData = { geometry: geometry, distance: distance, position: { x: intersectionPoint.x, y: intersectionPoint.y - 0.01, z: intersectionPoint.z } };

    floorGroup.current.add(referencePole.current);
}

export default updateReferencePolesheight;
