import * as THREE from 'three';
import * as CONSTANTS from '../../../types/world/worldConstants';
import * as Types from "../../../types/world/worldTypes";

function DeletableLineorPoint(
    state: Types.ThreeState,
    plane: Types.RefMesh,
    floorPlanGroupLine: Types.RefGroup,
    floorPlanGroupPoint: Types.RefGroup,
    hoveredDeletableLine: Types.RefMesh,
    hoveredDeletablePoint: Types.RefMesh
): void {

    ////////// Altering the color of the hovered line or point during the deletion time //////////

    if (!plane.current) return;
    let intersects = state.raycaster.intersectObject(plane.current, true);

    let visibleIntersectLines;
    if (floorPlanGroupLine.current) { visibleIntersectLines = state.raycaster?.intersectObjects(floorPlanGroupLine.current.children, true); }
    const visibleIntersectLine = visibleIntersectLines?.find(intersect => intersect.object.visible) as THREE.Line | undefined || null;

    let visibleIntersectPoints;
    if (floorPlanGroupPoint.current) {
        visibleIntersectPoints = state.raycaster?.intersectObjects(floorPlanGroupPoint.current.children, true);
    }
    const visibleIntersectPoint = visibleIntersectPoints?.find(intersect => intersect.object.visible) as THREE.Mesh | undefined;

    function getLineColor(lineType: string | undefined): string {
        switch (lineType) {
            case CONSTANTS.lineConfig.wallName: return CONSTANTS.lineConfig.wallColor;
            case CONSTANTS.lineConfig.floorName: return CONSTANTS.lineConfig.floorColor;
            case CONSTANTS.lineConfig.aisleName: return CONSTANTS.lineConfig.aisleColor;
            default: return CONSTANTS.lineConfig.defaultColor;
        }
    }

    if (intersects.length > 0) {
        if (visibleIntersectPoint) {
            if (hoveredDeletableLine.current) {
                const lineType = hoveredDeletableLine.current.userData.linePoints[1]?.[3];
                const color = getLineColor(lineType);
                (hoveredDeletableLine.current.material as THREE.MeshBasicMaterial).color = new THREE.Color(color);
                hoveredDeletableLine.current = null;
            }

            hoveredDeletablePoint.current = (visibleIntersectPoint as any).object;
            (hoveredDeletablePoint.current as any).material.uniforms.uInnerColor.value.set(new THREE.Color("red"));
            (hoveredDeletablePoint.current as any).material.uniforms.uColor.value.set(new THREE.Color("red"));
            // (hoveredDeletablePoint.current as THREE.Mesh).scale.set(1.5, 1.5, 1.5);
        } else if (hoveredDeletablePoint.current) {
            (hoveredDeletablePoint.current as any).material.uniforms.uInnerColor.value.set(CONSTANTS.pointConfig.defaultInnerColor);
            (hoveredDeletablePoint.current as any).material.uniforms.uColor.value.set((hoveredDeletablePoint.current as any).userData.color);
            // hoveredDeletablePoint.current.scale.set(1, 1, 1);
            hoveredDeletablePoint.current = null;
        }

        if (visibleIntersectLine && !visibleIntersectPoint) {
            if (hoveredDeletableLine.current) {
                const lineType = hoveredDeletableLine.current.userData.linePoints[1]?.[3];
                const color = getLineColor(lineType);
                (hoveredDeletableLine.current.material as THREE.MeshBasicMaterial).color = new THREE.Color(color);
                hoveredDeletableLine.current = null;
            }

            if (hoveredDeletablePoint.current) {
                (hoveredDeletablePoint.current as any).material.uniforms.uInnerColor.value.set(CONSTANTS.pointConfig.defaultInnerColor);
                (hoveredDeletablePoint.current as any).material.uniforms.uColor.value.set((hoveredDeletablePoint.current as any).userData.color);
                // hoveredDeletablePoint.current.scale.set(1, 1, 1);
                hoveredDeletablePoint.current = null;
            }

            hoveredDeletableLine.current = (visibleIntersectLine as any).object;
            if (hoveredDeletableLine.current) {
                (hoveredDeletableLine.current.material as THREE.MeshBasicMaterial).color = new THREE.Color("red");
            }
        } else if (hoveredDeletableLine.current) {
            const lineType = hoveredDeletableLine.current.userData.linePoints[1]?.[3];
            const color = getLineColor(lineType);
            (hoveredDeletableLine.current.material as THREE.MeshBasicMaterial).color = new THREE.Color(color);
            hoveredDeletableLine.current = null;
        }
    }

}

export default DeletableLineorPoint;
