import { useRef } from "react";
import * as CONSTANTS from "../../../../types/world/worldConstants";
import * as Types from "../../../../types/world/worldTypes";
import { useLoadingProgress, useNavMesh, useTileDistance } from "../../../../store/builder/store";
import useModuleStore from "../../../../store/useModuleStore";
import PolygonGenerator from "./polygonGenerator";
import NavMeshDetails from "./navMeshDetails";

function NavMesh() {
    let groupRef = useRef() as Types.RefGroup;
    const { setNavMesh } = useNavMesh();
    const { loadingProgress } = useLoadingProgress();
    const { activeModule } = useModuleStore();
    const { planeValue, gridValue } = useTileDistance();

    return (
        <>
            {activeModule === 'simulation' && loadingProgress === 0 &&
                <>
                    <PolygonGenerator groupRef={groupRef} />
                    <NavMeshDetails setNavMesh={setNavMesh} groupRef={groupRef} />

                    <group ref={groupRef} visible={false} name="Meshes">
                        <mesh rotation-x={CONSTANTS.planeConfig.rotation} position={CONSTANTS.planeConfig.position3D} receiveShadow>
                            <planeGeometry args={[planeValue.width, planeValue.height]} />
                        </mesh>
                    </group>
                </>
            }
        </>
    )
}

export default NavMesh