import asyncio
import json
import os

# 假设这些是 Python 等效库
from lib.mysql import DBPool
from lib.redis import Cache
from lib.node import NEXARPCNode, GRSRPCNode, MONARPCNode, DGBRPCNode, RXDRPCNode, ENXNode, ALPHRPCNode

class Init:
    def __init__(self, coin, method):
        self.coin = coin
        self.method = method
        # 配置加载
        config_path = f"./config/{coin}.conf"
        with open(config_path, "r", encoding="utf-8") as f:
            config = json.load(f)

        self.master = config["master"]
        self.slave = config["slave"]
        self.redis_options = config["redis_options"]
        self.node_options = config["node_options"]
        self.distribution_conf = config["distribution_conf"]
        self.MAX_MATURE = config["MAX_MATURE"]
        self.REPORT_ADDRESS = config["REPORT_ADDRESS"]

        # 节点映射
        self.node_map = {
            "mona": MONARPCNode,
            "nexa": NEXARPCNode,
            "grs": GRSRPCNode,
            "dgbs": DGBRPCNode,
            "dgbq": DGBRPCNode,
            "dgbo": DGBRPCNode,
            "rxd": RXDRPCNode,
            "enx": ENXNode,
            "alph": ALPHRPCNode
        }

        # 初始化分支
        self._init_method(method)

    def _init_method(self, method):
        master = self.master
        slave = self.slave
        redis1 = self.redis_options["redis1"]
        node2 = self.node_options["node2"]
        POOL_FEE = self.distribution_conf.get("POOL_FEE", 0)

        if method == "hashrate":
            self.sharesdb = DBPool(self.coin, master["sharesdb"])
            self.sharesdb_slave = DBPool(self.coin, slave["sharesdb_slave"])
            self.pooldb = DBPool(self.coin, master["pooldb"])
            self.redis = Cache(redis1)
            self.hashratedb = DBPool(self.coin, master["hashrate"])

        elif method == "distribution":
            self.pooldb = DBPool(self.coin, master["pooldb"])
            self.pooldb_slave = DBPool(self.coin, slave["pooldb_slave"])
            self.hashratedb = DBPool(self.coin, master["hashrate"])
            self.distributiondb = DBPool(self.coin, master["distribution"])
            self.users_addresses = DBPool(self.coin, master["users_addresses"])
            self.node = self.node_map[self.coin](node2)
            self.REPORT_ADDRESS = self.REPORT_ADDRESS
            self.POOL_FEE = POOL_FEE
            print(f"当前手续费率为: {POOL_FEE}")

        elif method == "confirm":
            self.node = self.node_map[self.coin](node2)
            self.distribution = DBPool(self.coin, master["distribution"])
            self.pooldb = DBPool(self.coin, master["pooldb"])
            self.MAX_MATURE = self.MAX_MATURE
            self.REPORT_ADDRESS = self.REPORT_ADDRESS

        elif method == "balance":
            self.distribution = DBPool(self.coin, master["distribution"])
            self.balancedb = DBPool(self.coin, master["balance"])
            self.node = self.node_map[self.coin](node2)
            self.MAX_MATURE = self.MAX_MATURE

        elif method == "report":
            if self.coin == "enx":
                self.distribution = DBPool(self.coin, master["distribution"])
                self.pooldb = DBPool(self.coin, master["pooldb"])
            else:
                self.node = self.node_map[self.coin](node2)
                self.distribution = DBPool(self.coin, master["distribution"])
                self.redis = Cache(redis1)
                self.REPORT_ADDRESS = self.REPORT_ADDRESS

        elif method == "clear":
            self.pooldb = DBPool(self.coin, master["pooldb"])
            self.sharesdb = DBPool(self.coin, master["sharesdb"])
            self.hashratedb = DBPool(self.coin, master["hashrate"])

        elif method == "stats":
            self.pooldb = DBPool(self.coin, master["pooldb"])
            self.hashratedb = DBPool(self.coin, master["hashrate"])
            self.distribution = DBPool(self.coin, master["distribution"])

        elif method == "notice":
            self.distribution = DBPool(self.coin, master["distribution"])

        else:
            raise ValueError(f"暂不支持 {method} 方法 init")

    async def sleep(self, ms):
        """异步睡眠"""
        await asyncio.sleep(ms / 1000)

# 模块导出
__all__ = ['Init']