"""
匹配规则管理器模块
管理匹配规则和参数
"""

import time
from typing import Dict, Any, List, Optional

class RuleManager:
    """
    匹配规则管理器
    管理匹配规则和参数
    """
    
    def __init__(self, plugin):
        """
        初始化匹配规则管理器
        
        Args:
            plugin: 匹配系统插件实例
        """
        self.plugin = plugin
        self.enabled = False
        self.initialized = False
        
        # 规则配置
        self.rule_config = {
            "default_rules": {
                "min_players": 2,
                "max_players": 8,
                "skill_range": 100,
                "max_wait_time": 120.0,
                "region_matching": True,
                "platform_matching": True,
                "language_matching": True
            },
            "game_mode_rules": {},  # 不同游戏模式的规则
            "custom_rules": {},     # 自定义规则
            "enable_rule_validation": True,
            "enable_dynamic_rules": True
        }
        
        # 规则状态
        self.rule_state = {
            "active_rules": "default",
            "total_rules": 1,  # 默认规则
            "custom_rules_count": 0,
            "rules_modified": 0
        }
        
        # 规则统计
        self.rule_stats = {
            "rules_applied": 0,
            "rules_violated": 0,
            "rule_errors": 0
        }
        
        # 回调函数
        self.rule_callbacks = {
            "rule_applied": [],
            "rule_violated": [],
            "rule_updated": [],
            "rule_error": []
        }
        
        # 时间戳记录
        self.last_rule_application = 0.0
        self.last_stats_reset = 0.0
        
        print("✓ 匹配规则管理器已创建")
        
    def initialize(self) -> bool:
        """
        初始化匹配规则管理器
        
        Returns:
            是否初始化成功
        """
        try:
            print("正在初始化匹配规则管理器...")
            
            self.initialized = True
            print("✓ 匹配规则管理器初始化完成")
            return True
            
        except Exception as e:
            print(f"✗ 匹配规则管理器初始化失败: {e}")
            self.rule_stats["rule_errors"] += 1
            import traceback
            traceback.print_exc()
            return False
            
    def enable(self) -> bool:
        """
        启用匹配规则管理器
        
        Returns:
            是否启用成功
        """
        try:
            if not self.initialized:
                print("✗ 匹配规则管理器未初始化")
                return False
                
            self.enabled = True
            print("✓ 匹配规则管理器已启用")
            return True
            
        except Exception as e:
            print(f"✗ 匹配规则管理器启用失败: {e}")
            self.rule_stats["rule_errors"] += 1
            import traceback
            traceback.print_exc()
            return False
            
    def disable(self):
        """禁用匹配规则管理器"""
        try:
            self.enabled = False
            print("✓ 匹配规则管理器已禁用")
            
        except Exception as e:
            print(f"✗ 匹配规则管理器禁用失败: {e}")
            self.rule_stats["rule_errors"] += 1
            import traceback
            traceback.print_exc()
            
    def finalize(self):
        """清理匹配规则管理器资源"""
        try:
            # 禁用匹配规则管理器
            if self.enabled:
                self.disable()
                
            # 清理回调
            self.rule_callbacks.clear()
            
            self.initialized = False
            print("✓ 匹配规则管理器资源已清理")
            
        except Exception as e:
            print(f"✗ 匹配规则管理器资源清理失败: {e}")
            import traceback
            traceback.print_exc()
            
    def update(self, dt: float):
        """
        更新匹配规则管理器状态
        
        Args:
            dt: 时间增量（秒）
        """
        try:
            if not self.enabled:
                return
                
            self.last_rule_application = time.time()
            
        except Exception as e:
            print(f"✗ 匹配规则管理器更新失败: {e}")
            self.rule_stats["rule_errors"] += 1
            import traceback
            traceback.print_exc()
            
    def apply_rules(self, players: List[Dict[str, Any]], game_mode: str = "default") -> bool:
        """
        应用匹配规则
        
        Args:
            players: 玩家列表
            game_mode: 游戏模式
            
        Returns:
            是否符合规则
        """
        try:
            if not self.enabled:
                return True  # 如果未启用，跳过规则检查
                
            self.rule_stats["rules_applied"] += 1
            
            # 获取适用的规则
            rules = self.get_active_rules(game_mode)
            
            # 检查玩家数量
            min_players = rules.get("min_players", 2)
            max_players = rules.get("max_players", 8)
            
            if len(players) < min_players:
                print(f"✗ 玩家数量不足: {len(players)} < {min_players}")
                self.rule_stats["rules_violated"] += 1
                
                # 触发规则违反回调
                self._trigger_rule_callback("rule_violated", {
                    "rule": "min_players",
                    "required": min_players,
                    "actual": len(players),
                    "timestamp": time.time()
                })
                
                return False
                
            if len(players) > max_players:
                print(f"✗ 玩家数量超限: {len(players)} > {max_players}")
                self.rule_stats["rules_violated"] += 1
                
                # 触发规则违反回调
                self._trigger_rule_callback("rule_violated", {
                    "rule": "max_players",
                    "required": max_players,
                    "actual": len(players),
                    "timestamp": time.time()
                })
                
                return False
                
            # 检查技能范围
            if rules.get("skill_range") is not None:
                skill_range = rules["skill_range"]
                skills = [p.get("skill_level", 0) for p in players]
                min_skill = min(skills)
                max_skill = max(skills)
                
                if max_skill - min_skill > skill_range:
                    print(f"✗ 技能范围超限: {max_skill - min_skill} > {skill_range}")
                    self.rule_stats["rules_violated"] += 1
                    
                    # 触发规则违反回调
                    self._trigger_rule_callback("rule_violated", {
                        "rule": "skill_range",
                        "required": skill_range,
                        "actual": max_skill - min_skill,
                        "timestamp": time.time()
                    })
                    
                    return False
                    
            # 检查区域匹配
            if rules.get("region_matching", False):
                regions = [p.get("region", "global") for p in players]
                if len(set(regions)) > 1:
                    print(f"✗ 区域不匹配: {set(regions)}")
                    self.rule_stats["rules_violated"] += 1
                    
                    # 触发规则违反回调
                    self._trigger_rule_callback("rule_violated", {
                        "rule": "region_matching",
                        "regions": list(set(regions)),
                        "timestamp": time.time()
                    })
                    
                    return False
                    
            # 检查平台匹配
            if rules.get("platform_matching", False):
                platforms = [p.get("platform", "pc") for p in players]
                if len(set(platforms)) > 1:
                    print(f"✗ 平台不匹配: {set(platforms)}")
                    self.rule_stats["rules_violated"] += 1
                    
                    # 触发规则违反回调
                    self._trigger_rule_callback("rule_violated", {
                        "rule": "platform_matching",
                        "platforms": list(set(platforms)),
                        "timestamp": time.time()
                    })
                    
                    return False
                    
            # 检查语言匹配
            if rules.get("language_matching", False):
                languages = [p.get("language", "en") for p in players]
                if len(set(languages)) > 1:
                    print(f"✗ 语言不匹配: {set(languages)}")
                    self.rule_stats["rules_violated"] += 1
                    
                    # 触发规则违反回调
                    self._trigger_rule_callback("rule_violated", {
                        "rule": "language_matching",
                        "languages": list(set(languages)),
                        "timestamp": time.time()
                    })
                    
                    return False
                    
            # 触发规则应用回调
            self._trigger_rule_callback("rule_applied", {
                "players": len(players),
                "game_mode": game_mode,
                "rules": rules,
                "timestamp": time.time()
            })
            
            print(f"✓ 规则检查通过: {len(players)}名玩家, 游戏模式: {game_mode}")
            return True
            
        except Exception as e:
            print(f"✗ 规则应用失败: {e}")
            self.rule_stats["rule_errors"] += 1
            return False
            
    def get_active_rules(self, game_mode: str = "default") -> Dict[str, Any]:
        """
        获取活动规则
        
        Args:
            game_mode: 游戏模式
            
        Returns:
            规则字典
        """
        try:
            # 检查是否有特定游戏模式的规则
            if game_mode in self.rule_config["game_mode_rules"]:
                return self.rule_config["game_mode_rules"][game_mode]
                
            # 检查是否有自定义规则
            if self.rule_state["active_rules"] in self.rule_config["custom_rules"]:
                return self.rule_config["custom_rules"][self.rule_state["active_rules"]]
                
            # 返回默认规则
            return self.rule_config["default_rules"]
            
        except Exception as e:
            print(f"✗ 获取活动规则失败: {e}")
            self.rule_stats["rule_errors"] += 1
            return self.rule_config["default_rules"]
            
    def set_active_rules(self, rule_set_name: str) -> bool:
        """
        设置活动规则集
        
        Args:
            rule_set_name: 规则集名称
            
        Returns:
            是否设置成功
        """
        try:
            # 验证规则集存在
            if (rule_set_name != "default" and 
                rule_set_name not in self.rule_config["custom_rules"] and
                rule_set_name not in self.rule_config["game_mode_rules"]):
                print(f"✗ 规则集不存在: {rule_set_name}")
                return False
                
            self.rule_state["active_rules"] = rule_set_name
            
            # 触发规则更新回调
            self._trigger_rule_callback("rule_updated", {
                "rule_set": rule_set_name,
                "timestamp": time.time()
            })
            
            print(f"✓ 活动规则集已设置为: {rule_set_name}")
            return True
            
        except Exception as e:
            print(f"✗ 活动规则集设置失败: {e}")
            self.rule_stats["rule_errors"] += 1
            return False
            
    def add_custom_rule_set(self, rule_set_name: str, rules: Dict[str, Any]) -> bool:
        """
        添加自定义规则集
        
        Args:
            rule_set_name: 规则集名称
            rules: 规则字典
            
        Returns:
            是否添加成功
        """
        try:
            if not self.enabled:
                return False
                
            # 验证规则
            if self.rule_config["enable_rule_validation"]:
                if not self._validate_rules(rules):
                    print(f"✗ 规则验证失败: {rule_set_name}")
                    self.rule_stats["rule_errors"] += 1
                    return False
                    
            self.rule_config["custom_rules"][rule_set_name] = rules
            self.rule_state["custom_rules_count"] = len(self.rule_config["custom_rules"])
            self.rule_state["total_rules"] += 1
            self.rule_state["rules_modified"] += 1
            
            print(f"✓ 自定义规则集已添加: {rule_set_name}")
            return True
            
        except Exception as e:
            print(f"✗ 自定义规则集添加失败: {e}")
            self.rule_stats["rule_errors"] += 1
            return False
            
    def remove_custom_rule_set(self, rule_set_name: str) -> bool:
        """
        移除自定义规则集
        
        Args:
            rule_set_name: 规则集名称
            
        Returns:
            是否移除成功
        """
        try:
            if rule_set_name in self.rule_config["custom_rules"]:
                del self.rule_config["custom_rules"][rule_set_name]
                self.rule_state["custom_rules_count"] = len(self.rule_config["custom_rules"])
                self.rule_state["total_rules"] -= 1
                self.rule_state["rules_modified"] += 1
                
                # 如果删除的是活动规则集，切换回默认规则
                if self.rule_state["active_rules"] == rule_set_name:
                    self.rule_state["active_rules"] = "default"
                    
                print(f"✓ 自定义规则集已移除: {rule_set_name}")
                return True
            else:
                print(f"✗ 自定义规则集不存在: {rule_set_name}")
                return False
                
        except Exception as e:
            print(f"✗ 自定义规则集移除失败: {e}")
            self.rule_stats["rule_errors"] += 1
            return False
            
    def _validate_rules(self, rules: Dict[str, Any]) -> bool:
        """
        验证规则
        
        Args:
            rules: 规则字典
            
        Returns:
            是否验证通过
        """
        try:
            # 检查基本规则参数
            if "min_players" in rules:
                if not isinstance(rules["min_players"], int) or rules["min_players"] < 1:
                    return False
                    
            if "max_players" in rules:
                if not isinstance(rules["max_players"], int) or rules["max_players"] < 1:
                    return False
                    
            if "min_players" in rules and "max_players" in rules:
                if rules["min_players"] > rules["max_players"]:
                    return False
                    
            if "skill_range" in rules:
                if not isinstance(rules["skill_range"], (int, float)) or rules["skill_range"] < 0:
                    return False
                    
            if "max_wait_time" in rules:
                if not isinstance(rules["max_wait_time"], (int, float)) or rules["max_wait_time"] < 0:
                    return False
                    
            return True
            
        except Exception as e:
            print(f"✗ 规则验证失败: {e}")
            self.rule_stats["rule_errors"] += 1
            return False
            
    def add_game_mode_rules(self, game_mode: str, rules: Dict[str, Any]) -> bool:
        """
        添加游戏模式规则
        
        Args:
            game_mode: 游戏模式
            rules: 规则字典
            
        Returns:
            是否添加成功
        """
        try:
            if not self.enabled:
                return False
                
            # 验证规则
            if self.rule_config["enable_rule_validation"]:
                if not self._validate_rules(rules):
                    print(f"✗ 游戏模式规则验证失败: {game_mode}")
                    self.rule_stats["rule_errors"] += 1
                    return False
                    
            self.rule_config["game_mode_rules"][game_mode] = rules
            self.rule_state["total_rules"] += 1
            self.rule_state["rules_modified"] += 1
            
            print(f"✓ 游戏模式规则已添加: {game_mode}")
            return True
            
        except Exception as e:
            print(f"✗ 游戏模式规则添加失败: {e}")
            self.rule_stats["rule_errors"] += 1
            return False
            
    def remove_game_mode_rules(self, game_mode: str) -> bool:
        """
        移除游戏模式规则
        
        Args:
            game_mode: 游戏模式
            
        Returns:
            是否移除成功
        """
        try:
            if game_mode in self.rule_config["game_mode_rules"]:
                del self.rule_config["game_mode_rules"][game_mode]
                self.rule_state["total_rules"] -= 1
                self.rule_state["rules_modified"] += 1
                
                print(f"✓ 游戏模式规则已移除: {game_mode}")
                return True
            else:
                print(f"✗ 游戏模式规则不存在: {game_mode}")
                return False
                
        except Exception as e:
            print(f"✗ 游戏模式规则移除失败: {e}")
            self.rule_stats["rule_errors"] += 1
            return False
            
    def get_rule_stats(self) -> Dict[str, Any]:
        """
        获取规则统计信息
        
        Returns:
            规则统计字典
        """
        return {
            "state": self.rule_state.copy(),
            "stats": self.rule_stats.copy(),
            "config": self.rule_config.copy()
        }
        
    def reset_stats(self):
        """重置规则统计信息"""
        try:
            self.rule_stats = {
                "rules_applied": 0,
                "rules_violated": 0,
                "rule_errors": 0
            }
            print("✓ 规则统计信息已重置")
        except Exception as e:
            print(f"✗ 规则统计信息重置失败: {e}")
            
    def set_rule_config(self, config: Dict[str, Any]) -> bool:
        """
        设置规则配置
        
        Args:
            config: 规则配置字典
            
        Returns:
            是否设置成功
        """
        try:
            self.rule_config.update(config)
            print(f"✓ 规则配置已更新: {self.rule_config}")
            return True
        except Exception as e:
            print(f"✗ 规则配置设置失败: {e}")
            return False
            
    def get_rule_config(self) -> Dict[str, Any]:
        """
        获取规则配置
        
        Returns:
            规则配置字典
        """
        return self.rule_config.copy()
        
    def _trigger_rule_callback(self, callback_type: str, data: Dict[str, Any]):
        """
        触发规则回调
        
        Args:
            callback_type: 回调类型
            data: 回调数据
        """
        try:
            if callback_type in self.rule_callbacks:
                for callback in self.rule_callbacks[callback_type]:
                    try:
                        callback(data)
                    except Exception as e:
                        print(f"✗ 规则回调执行失败: {callback_type} - {e}")
        except Exception as e:
            print(f"✗ 规则回调触发失败: {e}")
            
    def register_rule_callback(self, callback_type: str, callback: callable):
        """
        注册规则回调
        
        Args:
            callback_type: 回调类型
            callback: 回调函数
        """
        try:
            if callback_type in self.rule_callbacks:
                self.rule_callbacks[callback_type].append(callback)
                print(f"✓ 规则回调已注册: {callback_type}")
            else:
                print(f"✗ 无效的回调类型: {callback_type}")
        except Exception as e:
            print(f"✗ 规则回调注册失败: {e}")
            
    def unregister_rule_callback(self, callback_type: str, callback: callable):
        """
        注销规则回调
        
        Args:
            callback_type: 回调类型
            callback: 回调函数
        """
        try:
            if callback_type in self.rule_callbacks:
                if callback in self.rule_callbacks[callback_type]:
                    self.rule_callbacks[callback_type].remove(callback)
                    print(f"✓ 规则回调已注销: {callback_type}")
            else:
                print(f"✗ 无效的回调类型: {callback_type}")
        except Exception as e:
            print(f"✗ 规则回调注销失败: {e}")