#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
VWED.timer 模块 - 定时任务
"""

from typing import Callable
from ..script_registry_service import get_global_registry


class VWEDTimerModule:
    """VWED.timer 模块 - 定时任务"""
    
    def __init__(self, script_id: str):
        self.script_id = script_id
        self.registry = get_global_registry()
    
    def interval(self, seconds: int, handler: Callable, repeat: bool = True, delay: int = 0):
        """注册定时任务
        分离式模式：
        VWED.timer.interval(
            seconds=60,
            handler=system_monitor
        )
        """
        def decorator(func: Callable):
            timer_id = f"{self.script_id}_{func.__name__}_{id(func)}"
            self.registry.register_timer(
                timer_id=timer_id,
                interval=seconds,
                handler=func,
                script_id=self.script_id,
                repeat=repeat,
                delay=delay
            )
            return func
            
        return decorator(handler)
    
    def once(self, delay: int = 0, handler: Callable = None):
        """注册一次性定时任务
        分离式模式：
        VWED.timer.once(
            delay=10,
            handler=initialization_task
        )
        """
        return self.interval(0, handler=handler, repeat=False, delay=delay)