"""
状态机示例
演示如何使用状态机插件创建和运行状态机
"""

from plugins.user.state_machine.core.state_machine import State

class IdleState(State):
    """空闲状态"""
    
    def enter(self, context):
        print("进入空闲状态")
        if context:
            context.set_data("state", "idle")
        
    def execute(self, context):
        print("执行空闲状态逻辑")
        # 检查是否需要切换到巡逻状态
        if context and context.get_data("need_patrol", False):
            return context.states["patrol"]
        return None
        
    def exit(self, context):
        print("退出空闲状态")

class PatrolState(State):
    """巡逻状态"""
    
    def enter(self, context):
        print("进入巡逻状态")
        if context:
            context.set_data("state", "patrol")
            
    def execute(self, context):
        print("执行巡逻状态逻辑")
        # 更新巡逻计数
        patrol_count = context.get_data("patrol_count", 0) if context else 0
        if context:
            context.set_data("patrol_count", patrol_count + 1)
            
        # 检查是否需要切换到战斗状态
        if context and context.get_data("need_combat", False):
            return context.states["combat"]
        # 检查是否巡逻次数足够，需要切换到休息状态
        elif context and context.get_data("patrol_count", 0) >= 3:
            return context.states["rest"]
        return None
        
    def exit(self, context):
        print("退出巡逻状态")

class CombatState(State):
    """战斗状态"""
    
    def enter(self, context):
        print("进入战斗状态")
        if context:
            context.set_data("state", "combat")
            
    def execute(self, context):
        print("执行战斗状态逻辑")
        # 检查是否需要切换到空闲状态
        if context and context.get_data("need_idle", False):
            return context.states["idle"]
        return None
        
    def exit(self, context):
        print("退出战斗状态")

class RestState(State):
    """休息状态"""
    
    def enter(self, context):
        print("进入休息状态")
        if context:
            context.set_data("state", "rest")
            
    def execute(self, context):
        print("执行休息状态逻辑")
        # 检查是否需要切换到空闲状态
        if context and context.get_data("need_idle", False):
            return context.states["idle"]
        return None
        
    def exit(self, context):
        print("退出休息状态")

class SimpleContext:
    """简单的上下文类"""
    
    def __init__(self):
        self.data = {}
        self.states = {
            "idle": IdleState(),
            "patrol": PatrolState(),
            "combat": CombatState(),
            "rest": RestState()
        }
        
    def get_data(self, key, default=None):
        return self.data.get(key, default)
        
    def set_data(self, key, value):
        self.data[key] = value
        
    def get_state(self, name):
        return self.states.get(name)

def create_ai_state_machine(plugin):
    """
    创建AI状态机示例
    """
    # 创建状态机
    state_machine = plugin.create_state_machine("ai_state_machine")
    
    # 创建上下文
    context = SimpleContext()
    
    # 设置状态机上下文
    state_machine.set_context(context)
    
    # 初始状态设置为空闲
    context.set_data("need_patrol", True)
    context.set_data("need_combat", False)
    context.set_data("need_idle", False)
    context.set_data("patrol_count", 0)
    
    # 切换到初始状态
    state_machine.change_state(context.states["idle"])
    
    return state_machine, context

def run_ai_example(plugin):
    """
    运行AI状态机示例
    """
    print("=== 状态机示例：AI状态管理 ===")
    
    # 创建状态机和上下文
    state_machine, context = create_ai_state_machine(plugin)
    
    # 运行状态机
    for i in range(8):
        print(f"\n--- 第 {i+1} 次更新 ---")
        print(f"当前状态: {state_machine.get_current_state().name}")
        
        # 更新状态机
        state_machine.update()
        
        # 模拟一些条件变化
        if i == 2:
            context.set_data("need_combat", True)
            print("设置需要战斗")
        elif i == 4:
            context.set_data("need_combat", False)
            context.set_data("need_idle", True)
            print("设置需要空闲")
        elif i == 6:
            context.set_data("need_idle", False)
            context.set_data("need_patrol", True)
            print("设置需要巡逻")