import abc
from typing import Type, Any

from ldnode.runner.config import Settings
from .worker import Worker


class BaseWorkerState(abc.ABC):
    """ 工作进程状态基类 """
    @abc.abstractmethod
    async def send(self, data: Any) -> None:
        """ 发送数据到工作进程 """
        ...
        
    @abc.abstractmethod
    async def recv(self) -> Any:
        """ 接收数据从工作进程 """
        ...
    
    @abc.abstractmethod
    def get_worker_id(self) -> str:
        """ 工作进程ID """
        ...
        
    @abc.abstractmethod
    async def close(self) -> None:
        """ 关闭工作进程 """
        ...

    @abc.abstractmethod
    def is_alive(self) -> bool:
        """ 检查工作进程是否存活 """
        ...
        
    @abc.abstractmethod
    def is_used(self) -> bool:
        """ 检查工作进程是否被使用 """
        ...


class BaseContext(abc.ABC):
    """ 上下文基类 """
    @abc.abstractmethod
    async def register_worker(self, worker_name: str, worker_id: int, worker_cls: Type[Worker]) -> BaseWorkerState:
        """ 注册工作进程 """
        ...
        
    @abc.abstractmethod
    async def submit_event(self, source: str, event: str, data: Any) -> None:
        """ 提交事件 """
        ...


class Service(abc.ABC):
    """ 服务基类 """
    def __init__(self, settings: Settings, context: BaseContext):
        self.settings = settings
        self.context = context
    
    @abc.abstractmethod
    async def start(self) -> None:
        """ 启动服务 """
        ...
    
    @abc.abstractmethod
    async def shutdown(self) -> None:
        """ 关闭服务 """
        ...
