from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict, List

if TYPE_CHECKING:
    from mockmesh.plugins.base import BasePlugin


class PluginRegistry:

    _instance: "PluginRegistry | None" = None

    def __init__(self) -> None:
        self._plugins: Dict[str, "BasePlugin"] = {}

    @classmethod
    def instance(cls) -> "PluginRegistry":
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

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

    def activate_all(self, config: dict, **kwargs: Any) -> None:
        for plugin in self._plugins.values():
            plugin.activate(config, **kwargs)

    def deactivate_all(self) -> None:
        for plugin in self._plugins.values():
            plugin.deactivate()

    @property
    def plugins(self) -> List["BasePlugin"]:
        return list(self._plugins.values())

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