import asyncio

from common.consts import PUSH_TYPE
from db.database import get_db
from entity.push_config import PushConfig
from services.push_config_service import PushConfigService


class GlobalConfig:
    _instance = None
    _lock = asyncio.Lock()

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)

        return cls._instance

    def __init__(self):
        if not hasattr(self, "initialized"):  # 确保只初始化一次
            self.initialized = True
            self.config_service = None
            self.gas_push_config = None
            self.algo_result_push_config = None
            self.alarm_push_config = None
            self.harmful_gas_push_config = None
            self._init_done = False

    async def _initialize(self):
        await self.init_config()  # 调用异步初始化

    async def init_config(self):
        # 确保只初始化一次
        if not self._init_done:
            async with self._lock:
                if not self._init_done:  # 双重检查锁，避免多次初始化
                    async for db in get_db():
                        self.config_service = PushConfigService(db)
                        self.config_service.register_change_callback(self.on_config_change)
                        self.gas_push_config = await self.config_service.get_push_config(PUSH_TYPE.GAS)
                        self.algo_result_push_config = await self.config_service.get_push_config(PUSH_TYPE.ALGO_RESULT)
                        self.alarm_push_config = await self.config_service.get_push_config(PUSH_TYPE.ALARM)
                        self.harmful_gas_push_config = await self.config_service.get_push_config(PUSH_TYPE.HARMFUL_GAS)
                self._init_done = True

    async def on_config_change(self, config: PushConfig):
        if config.push_type == PUSH_TYPE.GAS:
            await self.set_gas_push_config(config)
        elif config.push_type == PUSH_TYPE.ALGO_RESULT:
            await self.set_algo_result_push_config(config)
        elif config.push_type == PUSH_TYPE.ALARM:
            await self.set_alarm_push_config(config)
        elif config.push_type == PUSH_TYPE.HARMFUL_GAS:
            await self.set_harmful_gas_push_config(config)

    def get_gas_push_config(self):
        """获取 gas_push_config 配置"""
        return self.gas_push_config

    async def set_gas_push_config(self, config):
        """设置 gas_push_config 配置"""
        if config:
            async with self._lock:
                self.gas_push_config = config

    def get_algo_result_push_config(self):
        """获取 algo_result_push_config 配置"""
        return self.algo_result_push_config

    async def set_algo_result_push_config(self, config):
        """设置 algo_result_push_config 配置"""
        if config:
            async with self._lock:
                self.algo_result_push_config = config

    def get_alarm_push_config(self):
        """获取 algo_result_push_config 配置"""
        return self.alarm_push_config

    async def set_alarm_push_config(self, config):
        """设置 algo_result_push_config 配置"""
        if config:
            async with self._lock:
                self.alarm_push_config = config

    def get_harmful_gas_push_config(self):
        """获取 algo_result_push_config 配置"""
        return self.harmful_gas_push_config

    async def set_harmful_gas_push_config(self, config):
        """设置 algo_result_push_config 配置"""
        if config:
            async with self._lock:
                self.harmful_gas_push_config = config

