from sideline.effects.types import EffectConfig, EffectPlugin


class EffectRegistry:
    def __init__(self):
        self._plugins: dict[str, EffectPlugin] = {}
        self._discovered: bool = False

    def register(self, plugin: EffectPlugin) -> None:
        self._plugins[plugin.name] = plugin

    def get(self, name: str) -> EffectPlugin | None:
        return self._plugins.get(name)

    def list_all(self) -> dict[str, EffectPlugin]:
        return self._plugins.copy()

    def list_enabled(self) -> list[EffectPlugin]:
        return [p for p in self._plugins.values() if p.config.enabled]

    def enable(self, name: str) -> bool:
        plugin = self._plugins.get(name)
        if plugin:
            plugin.config.enabled = True
            return True
        return False

    def disable(self, name: str) -> bool:
        plugin = self._plugins.get(name)
        if plugin:
            plugin.config.enabled = False
            return True
        return False

    def configure(self, name: str, config: EffectConfig) -> bool:
        plugin = self._plugins.get(name)
        if plugin:
            plugin.configure(config)
            return True
        return False

    def is_enabled(self, name: str) -> bool:
        plugin = self._plugins.get(name)
        return plugin.config.enabled if plugin else False


_registry: EffectRegistry | None = None


def get_registry() -> EffectRegistry:
    global _registry
    if _registry is None:
        _registry = EffectRegistry()
    return _registry


def set_registry(registry: EffectRegistry) -> None:
    global _registry
    _registry = registry
