# CelerisLab/body/registry.py
"""
BodyRegistry -- pure add/remove/query for simulation objects.
No GPU or LBM knowledge.
"""

from __future__ import annotations

from typing import Dict, List

from .objects import SimObject


class BodyRegistry:
    """Registry for SimObject instances.  No GPU or LBM knowledge."""

    def __init__(self) -> None:
        self._objects: Dict[int, SimObject] = {}
        self._next_id = 0

    def add(self, obj: SimObject) -> int:
        """Register a new object and return its assigned id."""
        obj.obj_id = self._next_id
        self._objects[self._next_id] = obj
        self._next_id += 1
        return obj.obj_id

    def remove(self, obj_id: int) -> None:
        """Unregister an object by id."""
        del self._objects[obj_id]

    def get(self, obj_id: int) -> SimObject:
        """Return the registered object by id."""
        return self._objects[obj_id]

    def clear(self) -> None:
        """Remove all objects and reset id counter."""
        self._objects.clear()
        self._next_id = 0

    @property
    def objects(self) -> List[SimObject]:
        """List of all registered objects in insertion order."""
        return list(self._objects.values())

    @property
    def count(self) -> int:
        """Number of registered objects."""
        return len(self._objects)
