"""
群体智能插件GUI界面
"""

from PyQt5.QtWidgets import (
    QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QSlider, QLabel, 
    QGroupBox, QCheckBox, QComboBox, QSpinBox, QDoubleSpinBox, QTabWidget,
    QFormLayout, QTextEdit
)
from PyQt5.QtCore import Qt, QTimer

class SwarmGUI(QWidget):
    """
    群体智能插件GUI界面
    提供完整的控制面板用于调节群体行为参数
    """
    
    def __init__(self, swarm_manager, config):
        super().__init__()
        self.swarm_manager = swarm_manager
        self.config = config
        self.init_ui()
        # 启动定时器更新状态信息
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_status)
        self.timer.start(1000)  # 每秒更新一次
        
    def init_ui(self):
        """
        初始化UI界面
        """
        self.setWindowTitle('群体智能控制面板')
        self.setGeometry(100, 100, 400, 600)
        
        # 创建标签页
        tab_widget = QTabWidget()
        
        # 基本控制标签页
        basic_tab = self.create_basic_tab()
        tab_widget.addTab(basic_tab, "基本控制")
        
        # Boids参数标签页
        boids_tab = self.create_boids_tab()
        tab_widget.addTab(boids_tab, "Boids参数")
        
        # 扩展行为标签页
        behavior_tab = self.create_behavior_tab()
        tab_widget.addTab(behavior_tab, "扩展行为")
        
        # 队形控制标签页
        formation_tab = self.create_formation_tab()
        tab_widget.addTab(formation_tab, "队形控制")
        
        # 学习控制标签页
        learning_tab = self.create_learning_tab()
        tab_widget.addTab(learning_tab, "学习控制")
        
        # 环境感知标签页
        environment_tab = self.create_environment_tab()
        tab_widget.addTab(environment_tab, "环境感知")
        
        # 状态信息标签页
        status_tab = self.create_status_tab()
        tab_widget.addTab(status_tab, "状态信息")
        
        # 主布局
        main_layout = QVBoxLayout()
        main_layout.addWidget(tab_widget)
        self.setLayout(main_layout)
        
    def create_environment_tab(self):
        """
        创建环境感知标签页
        """
        widget = QWidget()
        layout = QVBoxLayout()
        
        # 环境感知开关
        awareness_group = QGroupBox("环境感知控制")
        awareness_layout = QVBoxLayout()
        
        self.environment_awareness_checkbox = QCheckBox("启用环境感知")
        self.environment_awareness_checkbox.setChecked(self.config.get("environment_awareness_enabled", True))
        self.environment_awareness_checkbox.stateChanged.connect(self.toggle_environment_awareness)
        awareness_layout.addWidget(self.environment_awareness_checkbox)
        
        awareness_group.setLayout(awareness_layout)
        layout.addWidget(awareness_group)
        
        # 传感器参数
        sensor_group = QGroupBox("传感器参数")
        sensor_layout = QFormLayout()
        
        # 传感器范围
        self.sensor_range_spinbox = QDoubleSpinBox()
        self.sensor_range_spinbox.setRange(1.0, 50.0)
        self.sensor_range_spinbox.setSingleStep(1.0)
        self.sensor_range_spinbox.setValue(self.config.get("sensor_range", 15.0))
        self.sensor_range_spinbox.valueChanged.connect(self.update_sensor_range)
        sensor_layout.addRow("传感器范围:", self.sensor_range_spinbox)
        
        # 传感器精度
        self.sensor_accuracy_spinbox = QDoubleSpinBox()
        self.sensor_accuracy_spinbox.setRange(0.1, 1.0)
        self.sensor_accuracy_spinbox.setSingleStep(0.05)
        self.sensor_accuracy_spinbox.setValue(self.config.get("sensor_accuracy", 0.9))
        self.sensor_accuracy_spinbox.valueChanged.connect(self.update_sensor_accuracy)
        sensor_layout.addRow("传感器精度:", self.sensor_accuracy_spinbox)
        
        sensor_group.setLayout(sensor_layout)
        layout.addWidget(sensor_group)
        
        # 环境响应参数
        response_group = QGroupBox("环境响应参数")
        response_layout = QFormLayout()
        
        # 障碍物响应权重
        self.obstacle_response_weight_spinbox = QDoubleSpinBox()
        self.obstacle_response_weight_spinbox.setRange(0.0, 5.0)
        self.obstacle_response_weight_spinbox.setSingleStep(0.1)
        self.obstacle_response_weight_spinbox.setValue(self.config.get("obstacle_response_weight", 2.0))
        self.obstacle_response_weight_spinbox.valueChanged.connect(self.update_obstacle_response_weight)
        response_layout.addRow("障碍物响应权重:", self.obstacle_response_weight_spinbox)
        
        # 捕食者响应权重
        self.predator_response_weight_spinbox = QDoubleSpinBox()
        self.predator_response_weight_spinbox.setRange(0.0, 5.0)
        self.predator_response_weight_spinbox.setSingleStep(0.1)
        self.predator_response_weight_spinbox.setValue(self.config.get("predator_response_weight", 3.0))
        self.predator_response_weight_spinbox.valueChanged.connect(self.update_predator_response_weight)
        response_layout.addRow("捕食者响应权重:", self.predator_response_weight_spinbox)
        
        # 资源寻求权重
        self.resource_seek_weight_spinbox = QDoubleSpinBox()
        self.resource_seek_weight_spinbox.setRange(0.0, 2.0)
        self.resource_seek_weight_spinbox.setSingleStep(0.05)
        self.resource_seek_weight_spinbox.setValue(self.config.get("resource_seek_weight", 0.5))
        self.resource_seek_weight_spinbox.valueChanged.connect(self.update_resource_seek_weight)
        response_layout.addRow("资源寻求权重:", self.resource_seek_weight_spinbox)
        
        # 危险回避权重
        self.hazard_avoid_weight_spinbox = QDoubleSpinBox()
        self.hazard_avoid_weight_spinbox.setRange(0.0, 5.0)
        self.hazard_avoid_weight_spinbox.setSingleStep(0.1)
        self.hazard_avoid_weight_spinbox.setValue(self.config.get("hazard_avoid_weight", 3.0))
        self.hazard_avoid_weight_spinbox.valueChanged.connect(self.update_hazard_avoid_weight)
        response_layout.addRow("危险回避权重:", self.hazard_avoid_weight_spinbox)
        
        response_group.setLayout(response_layout)
        layout.addWidget(response_group)
        
        # 环境动态参数
        dynamics_group = QGroupBox("环境动态")
        dynamics_layout = QFormLayout()
        
        # 时间流逝速度
        self.time_flow_rate_spinbox = QDoubleSpinBox()
        self.time_flow_rate_spinbox.setRange(0.01, 2.0)
        self.time_flow_rate_spinbox.setSingleStep(0.05)
        self.time_flow_rate_spinbox.setValue(self.config.get("time_flow_rate", 0.1))
        self.time_flow_rate_spinbox.valueChanged.connect(self.update_time_flow_rate)
        dynamics_layout.addRow("时间流逝速度:", self.time_flow_rate_spinbox)
        
        dynamics_group.setLayout(dynamics_layout)
        layout.addWidget(dynamics_group)
        
        layout.addStretch()
        widget.setLayout(layout)
        return widget
        
    def create_basic_tab(self):
        """
        创建基本控制标签页
        """
        widget = QWidget()
        layout = QVBoxLayout()
        
        # 控制按钮组
        control_group = QGroupBox("基本控制")
        control_layout = QVBoxLayout()
        
        # 控制按钮
        button_layout = QHBoxLayout()
        self.pause_button = QPushButton("暂停/继续")
        self.pause_button.clicked.connect(self.toggle_pause)
        button_layout.addWidget(self.pause_button)
        
        reset_button = QPushButton("重置群体")
        reset_button.clicked.connect(self.reset_swarm)
        button_layout.addWidget(reset_button)
        
        control_layout.addLayout(button_layout)
        
        # 群体数量控制
        member_layout = QHBoxLayout()
        member_layout.addWidget(QLabel("群体数量:"))
        self.member_count_spinbox = QSpinBox()
        self.member_count_spinbox.setRange(1, 1000)
        self.member_count_spinbox.setValue(self.config.get("member_count", 20))
        self.member_count_spinbox.valueChanged.connect(self.update_member_count)
        member_layout.addWidget(self.member_count_spinbox)
        
        control_layout.addLayout(member_layout)
        
        control_group.setLayout(control_layout)
        layout.addWidget(control_group)
        
        # 群体类型选择
        type_group = QGroupBox("群体类型")
        type_layout = QVBoxLayout()
        
        self.type_combo = QComboBox()
        self.type_combo.addItems(["鸟类", "鱼类", "昆虫", "自定义"])
        self.type_combo.setCurrentText(self.config.get("swarm_type", "鸟类"))
        self.type_combo.currentTextChanged.connect(self.update_swarm_type)
        type_layout.addWidget(self.type_combo)
        
        type_group.setLayout(type_layout)
        layout.addWidget(type_group)
        
        # 目标控制
        target_group = QGroupBox("目标控制")
        target_layout = QVBoxLayout()
        
        self.seek_target_checkbox = QCheckBox("启用目标寻求")
        self.seek_target_checkbox.setChecked(self.config.get("seek_enabled", False))
        self.seek_target_checkbox.stateChanged.connect(self.toggle_seek_target)
        target_layout.addWidget(self.seek_target_checkbox)
        
        target_group.setLayout(target_layout)
        layout.addWidget(target_group)
        
        layout.addStretch()
        widget.setLayout(layout)
        return widget
        
    def create_boids_tab(self):
        """
        创建Boids参数标签页
        """
        widget = QWidget()
        layout = QVBoxLayout()
        
        # 基本Boids参数
        boids_group = QGroupBox("基本Boids参数")
        boids_layout = QFormLayout()
        
        # 聚集权重
        self.cohesion_spinbox = QDoubleSpinBox()
        self.cohesion_spinbox.setRange(0.0, 10.0)
        self.cohesion_spinbox.setSingleStep(0.1)
        self.cohesion_spinbox.setValue(self.config.get("cohesion_weight", 1.0))
        self.cohesion_spinbox.valueChanged.connect(self.update_cohesion_weight)
        boids_layout.addRow("聚集权重:", self.cohesion_spinbox)
        
        # 分离权重
        self.separation_spinbox = QDoubleSpinBox()
        self.separation_spinbox.setRange(0.0, 10.0)
        self.separation_spinbox.setSingleStep(0.1)
        self.separation_spinbox.setValue(self.config.get("separation_weight", 1.5))
        self.separation_spinbox.valueChanged.connect(self.update_separation_weight)
        boids_layout.addRow("分离权重:", self.separation_spinbox)
        
        # 对齐权重
        self.alignment_spinbox = QDoubleSpinBox()
        self.alignment_spinbox.setRange(0.0, 10.0)
        self.alignment_spinbox.setSingleStep(0.1)
        self.alignment_spinbox.setValue(self.config.get("alignment_weight", 1.0))
        self.alignment_spinbox.valueChanged.connect(self.update_alignment_weight)
        boids_layout.addRow("对齐权重:", self.alignment_spinbox)
        
        # 感知半径
        self.perception_spinbox = QDoubleSpinBox()
        self.perception_spinbox.setRange(0.1, 100.0)
        self.perception_spinbox.setSingleStep(0.5)
        self.perception_spinbox.setValue(self.config.get("perception_radius", 10.0))
        self.perception_spinbox.valueChanged.connect(self.update_perception_radius)
        boids_layout.addRow("感知半径:", self.perception_spinbox)
        
        boids_group.setLayout(boids_layout)
        layout.addWidget(boids_group)
        
        # 移动参数
        movement_group = QGroupBox("移动参数")
        movement_layout = QFormLayout()
        
        # 最大速度
        self.max_speed_spinbox = QDoubleSpinBox()
        self.max_speed_spinbox.setRange(0.1, 50.0)
        self.max_speed_spinbox.setSingleStep(0.5)
        self.max_speed_spinbox.setValue(self.config.get("max_speed", 5.0))
        self.max_speed_spinbox.valueChanged.connect(self.update_max_speed)
        movement_layout.addRow("最大速度:", self.max_speed_spinbox)
        
        # 最大加速度
        self.max_acceleration_spinbox = QDoubleSpinBox()
        self.max_acceleration_spinbox.setRange(0.01, 10.0)
        self.max_acceleration_spinbox.setSingleStep(0.05)
        self.max_acceleration_spinbox.setValue(self.config.get("max_acceleration", 1.0))
        self.max_acceleration_spinbox.valueChanged.connect(self.update_max_acceleration)
        movement_layout.addRow("最大加速度:", self.max_acceleration_spinbox)
        
        # 最大转向力
        self.max_force_spinbox = QDoubleSpinBox()
        self.max_force_spinbox.setRange(0.01, 5.0)
        self.max_force_spinbox.setSingleStep(0.05)
        self.max_force_spinbox.setValue(self.config.get("max_force", 0.5))
        self.max_force_spinbox.valueChanged.connect(self.update_max_force)
        movement_layout.addRow("最大转向力:", self.max_force_spinbox)
        
        movement_group.setLayout(movement_layout)
        layout.addWidget(movement_group)
        
        # 边界参数
        boundary_group = QGroupBox("边界参数")
        boundary_layout = QFormLayout()
        
        self.boundary_checkbox = QCheckBox("启用边界限制")
        self.boundary_checkbox.setChecked(self.config.get("boundary_enabled", True))
        self.boundary_checkbox.stateChanged.connect(self.toggle_boundary)
        boundary_layout.addRow("", self.boundary_checkbox)
        
        self.boundary_weight_spinbox = QDoubleSpinBox()
        self.boundary_weight_spinbox.setRange(0.0, 10.0)
        self.boundary_weight_spinbox.setSingleStep(0.1)
        self.boundary_weight_spinbox.setValue(self.config.get("boundary_weight", 2.0))
        self.boundary_weight_spinbox.valueChanged.connect(self.update_boundary_weight)
        boundary_layout.addRow("边界权重:", self.boundary_weight_spinbox)
        
        self.boundary_buffer_spinbox = QDoubleSpinBox()
        self.boundary_buffer_spinbox.setRange(0.1, 50.0)
        self.boundary_buffer_spinbox.setSingleStep(0.5)
        self.boundary_buffer_spinbox.setValue(self.config.get("boundary_buffer", 5.0))
        self.boundary_buffer_spinbox.valueChanged.connect(self.update_boundary_buffer)
        boundary_layout.addRow("边界缓冲:", self.boundary_buffer_spinbox)
        
        boundary_group.setLayout(boundary_layout)
        layout.addWidget(boundary_group)
        
        layout.addStretch()
        widget.setLayout(layout)
        return widget
        
    def create_behavior_tab(self):
        """
        创建扩展行为标签页
        """
        widget = QWidget()
        layout = QVBoxLayout()
        
        # 避障行为
        obstacle_group = QGroupBox("避障行为")
        obstacle_layout = QFormLayout()
        
        self.obstacle_checkbox = QCheckBox("启用避障")
        self.obstacle_checkbox.setChecked(self.config.get("obstacle_avoidance_enabled", True))
        self.obstacle_checkbox.stateChanged.connect(self.toggle_obstacle_avoidance)
        obstacle_layout.addRow("", self.obstacle_checkbox)
        
        self.obstacle_weight_spinbox = QDoubleSpinBox()
        self.obstacle_weight_spinbox.setRange(0.0, 10.0)
        self.obstacle_weight_spinbox.setSingleStep(0.1)
        self.obstacle_weight_spinbox.setValue(self.config.get("obstacle_weight", 2.0))
        self.obstacle_weight_spinbox.valueChanged.connect(self.update_obstacle_weight)
        obstacle_layout.addRow("避障权重:", self.obstacle_weight_spinbox)
        
        obstacle_group.setLayout(obstacle_layout)
        layout.addWidget(obstacle_group)
        
        # 游走行为
        wander_group = QGroupBox("游走行为")
        wander_layout = QFormLayout()
        
        self.wander_checkbox = QCheckBox("启用游走")
        self.wander_checkbox.setChecked(self.config.get("wander_enabled", False))
        self.wander_checkbox.stateChanged.connect(self.toggle_wander)
        wander_layout.addRow("", self.wander_checkbox)
        
        self.wander_weight_spinbox = QDoubleSpinBox()
        self.wander_weight_spinbox.setRange(0.0, 5.0)
        self.wander_weight_spinbox.setSingleStep(0.05)
        self.wander_weight_spinbox.setValue(self.config.get("wander_weight", 0.5))
        self.wander_weight_spinbox.valueChanged.connect(self.update_wander_weight)
        wander_layout.addRow("游走权重:", self.wander_weight_spinbox)
        
        self.wander_radius_spinbox = QDoubleSpinBox()
        self.wander_radius_spinbox.setRange(0.1, 50.0)
        self.wander_radius_spinbox.setSingleStep(0.5)
        self.wander_radius_spinbox.setValue(self.config.get("wander_radius", 5.0))
        self.wander_radius_spinbox.valueChanged.connect(self.update_wander_radius)
        wander_layout.addRow("游走半径:", self.wander_radius_spinbox)
        
        self.wander_jitter_spinbox = QDoubleSpinBox()
        self.wander_jitter_spinbox.setRange(0.0, 5.0)
        self.wander_jitter_spinbox.setSingleStep(0.05)
        self.wander_jitter_spinbox.setValue(self.config.get("wander_jitter", 1.0))
        self.wander_jitter_spinbox.valueChanged.connect(self.update_wander_jitter)
        wander_layout.addRow("游走抖动:", self.wander_jitter_spinbox)
        
        wander_group.setLayout(wander_layout)
        layout.addWidget(wander_group)
        
        # 躲避捕食者行为
        predator_group = QGroupBox("躲避捕食者")
        predator_layout = QFormLayout()
        
        self.predator_checkbox = QCheckBox("启用躲避捕食者")
        self.predator_checkbox.setChecked(self.config.get("predator_avoid_enabled", False))
        self.predator_checkbox.stateChanged.connect(self.toggle_predator_avoidance)
        predator_layout.addRow("", self.predator_checkbox)
        
        self.predator_weight_spinbox = QDoubleSpinBox()
        self.predator_weight_spinbox.setRange(0.0, 10.0)
        self.predator_weight_spinbox.setSingleStep(0.1)
        self.predator_weight_spinbox.setValue(self.config.get("predator_avoid_weight", 3.0))
        self.predator_weight_spinbox.valueChanged.connect(self.update_predator_weight)
        predator_layout.addRow("躲避权重:", self.predator_weight_spinbox)
        
        self.predator_radius_spinbox = QDoubleSpinBox()
        self.predator_radius_spinbox.setRange(1.0, 100.0)
        self.predator_radius_spinbox.setSingleStep(1.0)
        self.predator_radius_spinbox.setValue(self.config.get("predator_radius", 15.0))
        self.predator_radius_spinbox.valueChanged.connect(self.update_predator_radius)
        predator_layout.addRow("感知半径:", self.predator_radius_spinbox)
        
        predator_group.setLayout(predator_layout)
        layout.addWidget(predator_group)
        
        # 到达行为
        arrival_group = QGroupBox("到达行为")
        arrival_layout = QFormLayout()
        
        self.arrival_checkbox = QCheckBox("启用到达行为")
        self.arrival_checkbox.setChecked(self.config.get("arrival_enabled", False))
        self.arrival_checkbox.stateChanged.connect(self.toggle_arrival)
        arrival_layout.addRow("", self.arrival_checkbox)
        
        self.arrival_slow_radius_spinbox = QDoubleSpinBox()
        self.arrival_slow_radius_spinbox.setRange(0.1, 50.0)
        self.arrival_slow_radius_spinbox.setSingleStep(0.5)
        self.arrival_slow_radius_spinbox.setValue(self.config.get("arrival_slow_radius", 5.0))
        self.arrival_slow_radius_spinbox.valueChanged.connect(self.update_arrival_slow_radius)
        arrival_layout.addRow("减速半径:", self.arrival_slow_radius_spinbox)
        
        arrival_group.setLayout(arrival_layout)
        layout.addWidget(arrival_group)
        
        layout.addStretch()
        widget.setLayout(layout)
        return widget
        
    def create_formation_tab(self):
        """
        创建队形控制标签页
        """
        widget = QWidget()
        layout = QVBoxLayout()
        
        # 队形控制
        formation_group = QGroupBox("队形控制")
        formation_layout = QFormLayout()
        
        self.formation_checkbox = QCheckBox("启用队形保持")
        self.formation_checkbox.setChecked(self.config.get("formation_enabled", False))
        self.formation_checkbox.stateChanged.connect(self.toggle_formation)
        formation_layout.addRow("", self.formation_checkbox)
        
        self.formation_combo = QComboBox()
        self.formation_combo.addItems(["无", "V字形", "直线", "圆形", "楔形", "球形"])
        self.formation_combo.setCurrentText(self.config.get("formation_type", "无"))
        self.formation_combo.currentTextChanged.connect(self.update_formation_type)
        formation_layout.addRow("队形类型:", self.formation_combo)
        
        self.formation_weight_spinbox = QDoubleSpinBox()
        self.formation_weight_spinbox.setRange(0.0, 10.0)
        self.formation_weight_spinbox.setSingleStep(0.1)
        self.formation_weight_spinbox.setValue(self.config.get("formation_weight", 1.0))
        self.formation_weight_spinbox.valueChanged.connect(self.update_formation_weight)
        formation_layout.addRow("队形权重:", self.formation_weight_spinbox)
        
        formation_group.setLayout(formation_layout)
        layout.addWidget(formation_group)
        
        # 高级队形参数
        advanced_formation_group = QGroupBox("高级队形参数")
        advanced_formation_layout = QFormLayout()
        
        self.personal_space_spinbox = QDoubleSpinBox()
        self.personal_space_spinbox.setRange(0.1, 20.0)
        self.personal_space_spinbox.setSingleStep(0.1)
        self.personal_space_spinbox.setValue(self.config.get("personal_space", 2.0))
        self.personal_space_spinbox.valueChanged.connect(self.update_personal_space)
        advanced_formation_layout.addRow("个人空间:", self.personal_space_spinbox)
        
        advanced_formation_group.setLayout(advanced_formation_layout)
        layout.addWidget(advanced_formation_group)
        
        # 路径跟随
        path_group = QGroupBox("路径跟随")
        path_layout = QFormLayout()
        
        self.path_checkbox = QCheckBox("启用路径跟随")
        self.path_checkbox.setChecked(self.config.get("path_following_enabled", False))
        self.path_checkbox.stateChanged.connect(self.toggle_path_following)
        path_layout.addRow("", self.path_checkbox)
        
        self.path_weight_spinbox = QDoubleSpinBox()
        self.path_weight_spinbox.setRange(0.0, 10.0)
        self.path_weight_spinbox.setSingleStep(0.1)
        self.path_weight_spinbox.setValue(self.config.get("path_weight", 1.0))
        self.path_weight_spinbox.valueChanged.connect(self.update_path_weight)
        path_layout.addRow("路径权重:", self.path_weight_spinbox)
        
        self.path_radius_spinbox = QDoubleSpinBox()
        self.path_radius_spinbox.setRange(0.1, 50.0)
        self.path_radius_spinbox.setSingleStep(0.5)
        self.path_radius_spinbox.setValue(self.config.get("path_radius", 3.0))
        self.path_radius_spinbox.valueChanged.connect(self.update_path_radius)
        path_layout.addRow("路径半径:", self.path_radius_spinbox)
        
        path_group.setLayout(path_layout)
        layout.addWidget(path_group)
        
        # 预测参数
        prediction_group = QGroupBox("预测参数")
        prediction_layout = QFormLayout()
        
        self.prediction_distance_spinbox = QDoubleSpinBox()
        self.prediction_distance_spinbox.setRange(0.1, 100.0)
        self.prediction_distance_spinbox.setSingleStep(1.0)
        self.prediction_distance_spinbox.setValue(self.config.get("prediction_distance", 10.0))
        self.prediction_distance_spinbox.valueChanged.connect(self.update_prediction_distance)
        prediction_layout.addRow("预测距离:", self.prediction_distance_spinbox)
        
        prediction_group.setLayout(prediction_layout)
        layout.addWidget(prediction_group)
        
        # 物理参数
        physics_group = QGroupBox("物理参数")
        physics_layout = QFormLayout()
        
        self.gravity_checkbox = QCheckBox("启用重力")
        self.gravity_checkbox.setChecked(self.config.get("gravity_enabled", True))
        self.gravity_checkbox.stateChanged.connect(self.toggle_gravity)
        physics_layout.addRow("", self.gravity_checkbox)
        
        self.air_resistance_checkbox = QCheckBox("启用空气阻力")
        self.air_resistance_checkbox.setChecked(self.config.get("air_resistance_enabled", True))
        self.air_resistance_checkbox.stateChanged.connect(self.toggle_air_resistance)
        physics_layout.addRow("", self.air_resistance_checkbox)
        
        self.buoyancy_checkbox = QCheckBox("启用浮力")
        self.buoyancy_checkbox.setChecked(self.config.get("buoyancy_enabled", False))
        self.buoyancy_checkbox.stateChanged.connect(self.toggle_buoyancy)
        physics_layout.addRow("", self.buoyancy_checkbox)
        
        self.fluid_level_spinbox = QDoubleSpinBox()
        self.fluid_level_spinbox.setRange(-100.0, 100.0)
        self.fluid_level_spinbox.setSingleStep(1.0)
        self.fluid_level_spinbox.setValue(self.config.get("fluid_level", 0.0))
        self.fluid_level_spinbox.valueChanged.connect(self.update_fluid_level)
        physics_layout.addRow("流体水平面:", self.fluid_level_spinbox)
        
        physics_group.setLayout(physics_layout)
        layout.addWidget(physics_group)
        
        # 性能优化参数
        optimization_group = QGroupBox("性能优化")
        optimization_layout = QFormLayout()
        
        self.spatial_partitioning_checkbox = QCheckBox("启用空间分区")
        self.spatial_partitioning_checkbox.setChecked(self.config.get("spatial_partitioning", True))
        self.spatial_partitioning_checkbox.stateChanged.connect(self.toggle_spatial_partitioning)
        optimization_layout.addRow("", self.spatial_partitioning_checkbox)
        
        self.neighbor_optimization_checkbox = QCheckBox("启用邻居优化")
        self.neighbor_optimization_checkbox.setChecked(self.config.get("neighbor_search_optimization", True))
        self.neighbor_optimization_checkbox.stateChanged.connect(self.toggle_neighbor_optimization)
        optimization_layout.addRow("", self.neighbor_optimization_checkbox)
        
        self.parallel_computation_checkbox = QCheckBox("启用并行计算")
        self.parallel_computation_checkbox.setChecked(self.config.get("parallel_computation", True))
        self.parallel_computation_checkbox.stateChanged.connect(self.toggle_parallel_computation)
        optimization_layout.addRow("", self.parallel_computation_checkbox)
        
        optimization_group.setLayout(optimization_layout)
        layout.addWidget(optimization_group)
        
        layout.addStretch()
        widget.setLayout(layout)
        return widget
        
    def create_status_tab(self):
        """
        创建状态信息标签页
        """
        widget = QWidget()
        layout = QVBoxLayout()
        
        # 群体状态
        status_group = QGroupBox("群体状态")
        status_layout = QFormLayout()
        
        self.member_count_label = QLabel("0")
        status_layout.addRow("成员数量:", self.member_count_label)
        
        self.paused_label = QLabel("否")
        status_layout.addRow("是否暂停:", self.paused_label)
        
        self.fps_label = QLabel("0")
        status_layout.addRow("帧率:", self.fps_label)
        
        status_group.setLayout(status_layout)
        layout.addWidget(status_group)
        
        # 性能信息
        performance_group = QGroupBox("性能信息")
        performance_layout = QVBoxLayout()
        
        self.performance_text = QTextEdit()
        self.performance_text.setReadOnly(True)
        self.performance_text.setMaximumHeight(200)
        performance_layout.addWidget(self.performance_text)
        
        performance_group.setLayout(performance_layout)
        layout.addWidget(performance_group)
        
        # 日志信息
        log_group = QGroupBox("日志信息")
        log_layout = QVBoxLayout()
        
        self.log_text = QTextEdit()
        self.log_text.setReadOnly(True)
        log_layout.addWidget(self.log_text)
        
        log_group.setLayout(log_layout)
        layout.addWidget(log_group)
        
        layout.addStretch()
        widget.setLayout(layout)
        return widget
        
    def toggle_pause(self):
        """
        切换暂停状态
        """
        if self.swarm_manager:
            self.swarm_manager.toggle_pause()
            
    def reset_swarm(self):
        """
        重置群体
        """
        if self.swarm_manager:
            self.swarm_manager.cleanup()
            self.swarm_manager.create_example_swarm()
            self.update_status()
            
    def update_member_count(self, value):
        """
        更新群体数量
        """
        self.config.set("member_count", value)
        # 如果当前群体数量与设置不符，重新创建群体
        if self.swarm_manager and len(self.swarm_manager.members) != value:
            self.swarm_manager.cleanup()
            self.swarm_manager.create_example_swarm()
            
    def update_swarm_type(self, value):
        """
        更新群体类型
        """
        self.config.set("swarm_type", value)
        if self.swarm_manager:
            self.swarm_manager.swarm_type = value
            
    def toggle_seek_target(self, state):
        """
        切换目标寻求
        """
        enabled = state == Qt.Checked
        self.config.set("seek_enabled", enabled)
        
    def update_cohesion_weight(self, value):
        """
        更新聚集权重
        """
        self.config.set("cohesion_weight", value)
        if self.swarm_manager:
            self.swarm_manager.cohesion_weight = value
            
    def update_separation_weight(self, value):
        """
        更新分离权重
        """
        self.config.set("separation_weight", value)
        if self.swarm_manager:
            self.swarm_manager.separation_weight = value
            
    def update_alignment_weight(self, value):
        """
        更新对齐权重
        """
        self.config.set("alignment_weight", value)
        if self.swarm_manager:
            self.swarm_manager.alignment_weight = value
            
    def update_perception_radius(self, value):
        """
        更新感知半径
        """
        self.config.set("perception_radius", value)
        if self.swarm_manager:
            self.swarm_manager.perception_radius = value
            
    def update_max_speed(self, value):
        """
        更新最大速度
        """
        self.config.set("max_speed", value)
        if self.swarm_manager:
            self.swarm_manager.max_speed = value
            
    def update_max_acceleration(self, value):
        """
        更新最大加速度
        """
        self.config.set("max_acceleration", value)
        
    def update_max_force(self, value):
        """
        更新最大转向力
        """
        self.config.set("max_force", value)
        
    def toggle_boundary(self, state):
        """
        切换边界限制
        """
        enabled = state == Qt.Checked
        self.config.set("boundary_enabled", enabled)
        
    def update_boundary_weight(self, value):
        """
        更新边界权重
        """
        self.config.set("boundary_weight", value)
        
    def update_boundary_buffer(self, value):
        """
        更新边界缓冲
        """
        self.config.set("boundary_buffer", value)
        
    def toggle_obstacle_avoidance(self, state):
        """
        切换避障行为
        """
        enabled = state == Qt.Checked
        self.config.set("obstacle_avoidance_enabled", enabled)
        
    def update_obstacle_weight(self, value):
        """
        更新避障权重
        """
        self.config.set("obstacle_weight", value)
        
    def toggle_wander(self, state):
        """
        切换游走行为
        """
        enabled = state == Qt.Checked
        self.config.set("wander_enabled", enabled)
        
    def update_wander_weight(self, value):
        """
        更新游走权重
        """
        self.config.set("wander_weight", value)
        
    def update_wander_radius(self, value):
        """
        更新游走半径
        """
        self.config.set("wander_radius", value)
        
    def update_wander_jitter(self, value):
        """
        更新游走抖动
        """
        self.config.set("wander_jitter", value)
        
    def toggle_predator_avoidance(self, state):
        """
        切换躲避捕食者行为
        """
        enabled = state == Qt.Checked
        self.config.set("predator_avoid_enabled", enabled)
        
    def update_predator_weight(self, value):
        """
        更新躲避捕食者权重
        """
        self.config.set("predator_avoid_weight", value)
        
    def update_predator_radius(self, value):
        """
        更新捕食者感知半径
        """
        self.config.set("predator_radius", value)
        
    def toggle_arrival(self, state):
        """
        切换到达行为
        """
        enabled = state == Qt.Checked
        self.config.set("arrival_enabled", enabled)
        
    def update_arrival_slow_radius(self, value):
        """
        更新到达减速半径
        """
        self.config.set("arrival_slow_radius", value)
        
    def toggle_formation(self, state):
        """
        切换队形保持
        """
        enabled = state == Qt.Checked
        self.config.set("formation_enabled", enabled)
        
    def update_formation_type(self, value):
        """
        更新队形类型
        """
        self.config.set("formation_type", value)
        
    def update_formation_weight(self, value):
        """
        更新队形权重
        """
        self.config.set("formation_weight", value)
        
    def toggle_path_following(self, state):
        """
        切换路径跟随
        """
        enabled = state == Qt.Checked
        self.config.set("path_following_enabled", enabled)
        
    def update_path_weight(self, value):
        """
        更新路径权重
        """
        self.config.set("path_weight", value)
        
    def update_path_radius(self, value):
        """
        更新路径半径
        """
        self.config.set("path_radius", value)
        
    def update_prediction_distance(self, value):
        """
        更新预测距离
        """
        self.config.set("prediction_distance", value)
        
    def update_personal_space(self, value):
        """
        更新个人空间
        """
        self.config.set("personal_space", value)
        
    def toggle_gravity(self, state):
        """
        切换重力
        """
        enabled = state == Qt.Checked
        self.config.set("gravity_enabled", enabled)
        
    def toggle_air_resistance(self, state):
        """
        切换空气阻力
        """
        enabled = state == Qt.Checked
        self.config.set("air_resistance_enabled", enabled)
        
    def toggle_buoyancy(self, state):
        """
        切换浮力
        """
        enabled = state == Qt.Checked
        self.config.set("buoyancy_enabled", enabled)
        
    def update_fluid_level(self, value):
        """
        更新流体水平面
        """
        self.config.set("fluid_level", value)
        
    def toggle_spatial_partitioning(self, state):
        """
        切换空间分区
        """
        enabled = state == Qt.Checked
        self.config.set("spatial_partitioning", enabled)
        
    def toggle_neighbor_optimization(self, state):
        """
        切换邻居优化
        """
        enabled = state == Qt.Checked
        self.config.set("neighbor_search_optimization", enabled)
        
    def toggle_parallel_computation(self, state):
        """
        切换并行计算
        """
        enabled = state == Qt.Checked
        self.config.set("parallel_computation", enabled)
        
    def toggle_interaction(self, state):
        """
        切换群体间交互
        """
        enabled = state == Qt.Checked
        self.config.set("interaction_enabled", enabled)
        
    def toggle_learning(self, state):
        """
        切换学习能力
        """
        enabled = state == Qt.Checked
        self.config.set("learning_enabled", enabled)
        
    def toggle_environment_awareness(self, state):
        """
        切换环境感知
        """
        enabled = state == Qt.Checked
        self.config.set("environment_awareness_enabled", enabled)
        
    def update_sensor_range(self, value):
        """
        更新传感器范围
        """
        self.config.set("sensor_range", value)
        
    def update_sensor_accuracy(self, value):
        """
        更新传感器精度
        """
        self.config.set("sensor_accuracy", value)
        
    def update_obstacle_response_weight(self, value):
        """
        更新障碍物响应权重
        """
        self.config.set("obstacle_response_weight", value)
        
    def update_predator_response_weight(self, value):
        """
        更新捕食者响应权重
        """
        self.config.set("predator_response_weight", value)
        
    def update_resource_seek_weight(self, value):
        """
        更新资源寻求权重
        """
        self.config.set("resource_seek_weight", value)
        
    def update_hazard_avoid_weight(self, value):
        """
        更新危险回避权重
        """
        self.config.set("hazard_avoid_weight", value)
        
    def update_time_flow_rate(self, value):
        """
        更新时间流逝速度
        """
        self.config.set("time_flow_rate", value)
        
    def update_learning_rate(self, value):
        """
        更新学习率
        """
        self.config.set("learning_rate", value)
        if hasattr(self.swarm_manager, 'learning_manager'):
            self.swarm_manager.learning_manager.learning_rate = value
            
    def update_exploration_rate(self, value):
        """
        更新探索率
        """
        self.config.set("exploration_rate", value)
        if hasattr(self.swarm_manager, 'learning_manager'):
            self.swarm_manager.learning_manager.exploration_rate = value
            
    def update_memory_capacity(self, value):
        """
        更新记忆容量
        """
        self.config.set("memory_capacity", value)
        if hasattr(self.swarm_manager, 'learning_manager'):
            self.swarm_manager.learning_manager.memory_capacity = value
            
    def clear_learning_memory(self):
        """
        清空学习记忆
        """
        if hasattr(self.swarm_manager, 'learning_manager'):
            self.swarm_manager.learning_manager.clear_memory()
            
    def create_learning_tab(self):
        """
        创建学习控制标签页
        """
        widget = QWidget()
        layout = QVBoxLayout()
        
        # 学习开关
        learning_group = QGroupBox("学习控制")
        learning_layout = QVBoxLayout()
        
        self.learning_checkbox = QCheckBox("启用学习能力")
        self.learning_checkbox.setChecked(self.config.get("learning_enabled", False))
        self.learning_checkbox.stateChanged.connect(self.toggle_learning)
        learning_layout.addWidget(self.learning_checkbox)
        
        clear_memory_button = QPushButton("清空学习记忆")
        clear_memory_button.clicked.connect(self.clear_learning_memory)
        learning_layout.addWidget(clear_memory_button)
        
        learning_group.setLayout(learning_layout)
        layout.addWidget(learning_group)
        
        # 学习参数
        params_group = QGroupBox("学习参数")
        params_layout = QFormLayout()
        
        # 学习率
        self.learning_rate_spinbox = QDoubleSpinBox()
        self.learning_rate_spinbox.setRange(0.01, 1.0)
        self.learning_rate_spinbox.setSingleStep(0.01)
        self.learning_rate_spinbox.setValue(self.config.get("learning_rate", 0.1))
        self.learning_rate_spinbox.valueChanged.connect(self.update_learning_rate)
        params_layout.addRow("学习率:", self.learning_rate_spinbox)
        
        # 探索率
        self.exploration_rate_spinbox = QDoubleSpinBox()
        self.exploration_rate_spinbox.setRange(0.0, 1.0)
        self.exploration_rate_spinbox.setSingleStep(0.05)
        self.exploration_rate_spinbox.setValue(self.config.get("exploration_rate", 0.3))
        self.exploration_rate_spinbox.valueChanged.connect(self.update_exploration_rate)
        params_layout.addRow("探索率:", self.exploration_rate_spinbox)
        
        # 记忆容量
        self.memory_capacity_spinbox = QSpinBox()
        self.memory_capacity_spinbox.setRange(100, 10000)
        self.memory_capacity_spinbox.setSingleStep(100)
        self.memory_capacity_spinbox.setValue(self.config.get("memory_capacity", 1000))
        self.memory_capacity_spinbox.valueChanged.connect(self.update_memory_capacity)
        params_layout.addRow("记忆容量:", self.memory_capacity_spinbox)
        
        params_group.setLayout(params_layout)
        layout.addWidget(params_group)
        
        # 学习统计信息
        stats_group = QGroupBox("学习统计")
        stats_layout = QFormLayout()
        
        self.experience_count_label = QLabel("0")
        stats_layout.addRow("经验数量:", self.experience_count_label)
        
        self.policy_count_label = QLabel("0")
        stats_layout.addRow("策略数量:", self.policy_count_label)
        
        self.exploration_rate_label = QLabel("0.0")
        stats_layout.addRow("当前探索率:", self.exploration_rate_label)
        
        stats_group.setLayout(stats_layout)
        layout.addWidget(stats_group)
        
        layout.addStretch()
        widget.setLayout(layout)
        return widget
        
    def update_status(self):
        """
        更新状态信息
        """
        if self.swarm_manager:
            count = len(self.swarm_manager.members)
            self.member_count_label.setText(str(count))
            self.paused_label.setText("是" if self.swarm_manager.paused else "否")
            
            # 更新性能信息
            performance_info = f"成员数量: {count}\n"
            performance_info += f"是否暂停: {'是' if self.swarm_manager.paused else '否'}\n"
            performance_info += f"感知半径: {self.config.get('perception_radius', 10.0)}\n"
            performance_info += f"最大速度: {self.config.get('max_speed', 5.0)}\n"
            performance_info += f"成员数量: {self.config.get('member_count', 20)}\n"
            performance_info += f"并行计算: {'是' if self.config.get('parallel_computation', True) else '否'}\n"
            
            # 添加学习统计信息
            if hasattr(self.swarm_manager, 'learning_manager') and self.config.get("learning_enabled", False):
                learning_stats = self.swarm_manager.learning_manager.get_learning_stats()
                performance_info += f"经验数量: {learning_stats['experience_count']}\n"
                performance_info += f"策略数量: {learning_stats['policy_count']}\n"
                performance_info += f"探索率: {learning_stats['exploration_rate']:.2f}\n"
                performance_info += f"学习率: {learning_stats['learning_rate']:.2f}\n"
                
                # 更新学习统计标签
                if hasattr(self, 'experience_count_label'):
                    self.experience_count_label.setText(str(learning_stats['experience_count']))
                if hasattr(self, 'policy_count_label'):
                    self.policy_count_label.setText(str(learning_stats['policy_count']))
                if hasattr(self, 'exploration_rate_label'):
                    self.exploration_rate_label.setText(f"{learning_stats['exploration_rate']:.2f}")
                    
            self.performance_text.setPlainText(performance_info)