#!/usr/bin/env python3
"""
进度报告器
"""

from .types import ProgressInfo
from ..jsonrpc import create_progress_reporter
from ..logger import logger

class ProgressReporter:
    """进度报告器"""
    
    def __init__(self, service_name: str):
        self.service_name = service_name
        self.rpc_progress_reporter = None
        self._setup_progress_reporter()
    
    def _setup_progress_reporter(self) -> None:
        """设置进度报告器"""
        try:
            self.rpc_progress_reporter = create_progress_reporter()
            logger.debug(f"Progress reporter initialized for {self.service_name}")
        except Exception as e:
            logger.warning(f"Failed to initialize progress reporter: {e}")
            self.rpc_progress_reporter = None
    
    def report_progress(self, progress: ProgressInfo) -> None:
        """报告进度"""
        if self.rpc_progress_reporter:
            # JSON-RPC进度报告
            self.rpc_progress_reporter.report(
                step=self.service_name,
                progress=progress.percentage / 100.0,  # 转换为0-1范围
                message=progress.message,
                details={
                    "current": progress.current,
                    "total": progress.total,
                    "elapsed_time": progress.elapsed_time,
                    "estimated_remaining": progress.estimated_remaining
                }
            )
        else:
            # 简单的控制台输出
            print(f"Progress: {progress.percentage:.1f}% - {progress.message}")
    
    def report_step(self, step_name: str, message: str) -> None:
        """报告步骤"""
        if self.rpc_progress_reporter:
            self.rpc_progress_reporter.step(step_name, message)
        else:
            print(f"Step: {step_name} - {message}")
    
    def report_complete(self, message: str = "完成") -> None:
        """报告完成"""
        if self.rpc_progress_reporter:
            self.rpc_progress_reporter.complete(message)
        else:
            print(f"Complete: {message}")
