import * as THREE from 'three';

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

function DeletableHoveredPillar(
    state: Types.ThreeState,
    floorGroup: Types.RefGroup,
    hoveredDeletablePillar: Types.RefMesh
): void {

    ////////// Altering the color of the hovered Pillar during the Deletion time  //////////

    const intersects = state.raycaster.intersectObjects(floorGroup.current.children, true);
    const poleIntersect = intersects.find(intersect => intersect.object.name === "Pole");

    if (poleIntersect) {
        if (poleIntersect.object.name !== "Pole") {
            return;
        }
        if (hoveredDeletablePillar.current) {
            (hoveredDeletablePillar.current.material as THREE.MeshStandardMaterial).emissive = new THREE.Color("black");
            hoveredDeletablePillar.current = undefined;
        }
        hoveredDeletablePillar.current = poleIntersect.object as THREE.Mesh; // Type assertion
        (hoveredDeletablePillar.current.material as THREE.MeshStandardMaterial).emissive = new THREE.Color("red");
    } else {
        if (hoveredDeletablePillar.current) {
            (hoveredDeletablePillar.current.material as THREE.MeshStandardMaterial).emissive = new THREE.Color("black");
            hoveredDeletablePillar.current = undefined;
        }
    }
}

export default DeletableHoveredPillar;