from typing import Callable

from .basket_handler import handle_basket_gen_task
from .cb_backtest_handler import handle_cb_backtest
from .cb_factor_analysis_handler import handle_cb_factor_analysis
from .cb_binning_handler import handle_cb_binning
from .cb_evalution_handler import handle_cb_evalution
from .cb_formula_compute_handler import handle_cb_formula_compute
from .cb_glance_handler import handle_cb_glance
from .cb_grid_backtest_handler import handle_cb_grid_backtest
from .cb_screen_handler import handle_cb_screen
from .etf_backtest_handler import handle_etf_backtest
from .etf_factor_analysis_handler import handle_etf_factor_analysis
from .etf_binning_handler import handle_etf_binning
from .etf_evalution_handler import handle_etf_evalution
from .etf_formula_compute_handler import handle_etf_formula_compute
from .etf_glance_handler import handle_etf_glance
from .etf_screen_handler import handle_etf_screen
from .finance_formula_compute_handler import handle_finance_formula_compute
from .hyperparameter_search_handler import handle_hyperparameter_search
from .strategy_correlation_handler import handle_strategy_correlation_analysis
from .stk_backtest_handler import handle_stk_backtest
from .stk_factor_analysis_handler import handle_stk_factor_analysis
from .stk_binning_handler import handle_stk_binning
from .stk_evalution_handler import handle_stk_evalution
from .stk_formula_compute_handler import handle_stk_formula_compute
from .stk_glance_handler import handle_stk_glance
from .stk_screen_handler import handle_stk_screen


class Handler:
    def __init__(self, name: str, handler: Callable, cpu_usage: int, gpu_usage: int):
        self.name = name
        self.handle = handler
        self.cpu_usage = cpu_usage
        self.gpu_usage = gpu_usage


class HandlerRegistry:
    """处理器注册表"""

    def __init__(self):
        self.handlers: dict[str, Handler] = {}

    def register(
        self, name: str, handler: Callable, cpu_usage: int = 1, gpu_usage: int = 0
    ) -> None:
        """注册处理器"""
        self.handlers[name] = Handler(name, handler, cpu_usage, gpu_usage)

    def get(self, name: str) -> Handler:
        """获取处理器"""
        return self.handlers[name]

    def all(self) -> list[Handler]:
        """获取所有处理器"""
        return list(self.handlers.values())


handler_registry = HandlerRegistry()
# 不区分品种的方法
handler_registry.register("basket", handle_basket_gen_task)
handler_registry.register("hyper_search", handle_hyperparameter_search, cpu_usage=10)
handler_registry.register("strategy_correlation_analysis", handle_strategy_correlation_analysis)
# 转债计算函数
handler_registry.register("cb_screen", handle_cb_screen)
handler_registry.register("cb_backtest", handle_cb_backtest)
handler_registry.register("cb_formula_compute", handle_cb_formula_compute)
handler_registry.register("cb_backtest_glance", handle_cb_glance)
handler_registry.register("cb_binning", handle_cb_binning)
handler_registry.register("cb_grid_backtest", handle_cb_grid_backtest)
handler_registry.register("cb_evalution", handle_cb_evalution)
handler_registry.register("cb_factor_analysis", handle_cb_factor_analysis, cpu_usage=2)
# 股票计算函数
handler_registry.register("stk_screen", handle_stk_screen)
handler_registry.register("stk_backtest", handle_stk_backtest)
handler_registry.register("stk_factor_analysis", handle_stk_factor_analysis, cpu_usage=2)
handler_registry.register("stk_formula_compute", handle_stk_formula_compute)
handler_registry.register("stk_backtest_glance", handle_stk_glance)
handler_registry.register("stk_binning", handle_stk_binning)
handler_registry.register("stk_evalution", handle_stk_evalution)
# ETF计算函数
handler_registry.register("etf_screen", handle_etf_screen)
handler_registry.register("etf_backtest", handle_etf_backtest)
handler_registry.register("etf_formula_compute", handle_etf_formula_compute)
handler_registry.register("etf_backtest_glance", handle_etf_glance)
handler_registry.register("etf_binning", handle_etf_binning)
handler_registry.register("etf_evalution", handle_etf_evalution)
handler_registry.register("etf_factor_analysis", handle_etf_factor_analysis, cpu_usage=2)
# 财报计算函数
handler_registry.register("finance_formula_compute", handle_finance_formula_compute)
