"""
输入编辑器模块
提供可视化界面用于配置和测试输入设备及映射
"""

import time
from typing import Dict, Any, List, Optional
from enum import Enum
import threading

class EditorMode(Enum):
    """编辑器模式枚举"""
    DEVICE_SETUP = "device_setup"
    MAPPING_EDITOR = "mapping_editor"
    GESTURE_TESTER = "gesture_tester"
    HAPTIC_DESIGNER = "haptic_designer"
    CONFIG_MANAGER = "config_manager"
    LIVE_MONITOR = "live_monitor"
    DEVICE_CALIBRATOR = "device_calibrator"  # 新增设备校准模式
    ADVANCED_INPUT = "advanced_input"        # 新增高级输入模式

class InputEditor:
    """
    输入编辑器
    提供可视化界面用于配置和测试输入设备及映射
    """
    
    def __init__(self, plugin):
        """
        初始化输入编辑器
        
        Args:
            plugin: 多平台输入支持插件实例
        """
        self.plugin = plugin
        self.enabled = False
        self.initialized = False
        
        # 编辑器配置
        self.editor_config = {
            'window_width': 1024,
            'window_height': 768,
            'refresh_rate': 30.0,
            'enable_preview': True,
            'enable_realtime_update': True,
            'show_debug_info': False,
            'theme': 'dark'
        }
        
        # 当前编辑器状态
        self.editor_state = {
            'current_mode': EditorMode.DEVICE_SETUP,
            'selected_device': None,
            'selected_mapping': None,
            'selected_gesture': None,
            'selected_effect': None,
            'is_recording': False,
            'is_testing': False,
            'preview_active': False
        }
        
        # UI组件
        self.ui_components = {
            'main_window': None,
            'toolbar': None,
            'property_panel': None,
            'preview_panel': None,
            'status_bar': None,
            'device_list': None,
            'mapping_editor': None,
            'gesture_visualizer': None,
            'haptic_designer': None,
            'calibration_wizard': None,     # 新增校准向导
            'advanced_input_panel': None    # 新增高级输入面板
        }
        
        # 编辑器数据
        self.editor_data = {
            'device_cache': {},
            'mapping_cache': {},
            'gesture_cache': {},
            'haptic_cache': {},
            'live_data': {},
            'calibration_data': {},         # 新增校准数据
            'advanced_patterns': {}         # 新增高级模式数据
        }
        
        # 编辑器操作历史
        self.operation_history = []
        self.history_pointer = -1
        self.max_history_size = 100
        
        # 实时监控数据
        self.monitor_data = {
            'input_events': [],
            'gesture_events': [],
            'haptic_events': [],
            'performance_metrics': {},
            'calibration_progress': {},     # 新增校准进度数据
            'advanced_input_events': []     # 新增高级输入事件数据
        }
        
        # 回调函数
        self.editor_callbacks = {
            'device_selected': [],
            'mapping_selected': [],
            'gesture_selected': [],
            'effect_selected': [],
            'mode_changed': [],
            'data_updated': [],
            'calibration_progress': [],     # 新增校准进度回调
            'pattern_matched': []           # 新增模式匹配回调
        }
        
        # 统计信息
        self.editor_stats = {
            'windows_opened': 0,
            'operations_performed': 0,
            'undo_operations': 0,
            'redo_operations': 0,
            'data_refreshed': 0,
            'preview_updates': 0
        }
        
        # 线程管理
        self.editor_thread = None
        self.monitor_thread = None
        self.running = False
        
        # 时间戳记录
        self.last_refresh_time = 0.0
        self.last_preview_update = 0.0
        
        print("✓ 输入编辑器已创建")
        
    def initialize(self) -> bool:
        """
        初始化输入编辑器
        
        Returns:
            是否初始化成功
        """
        try:
            # 初始化UI组件（模拟）
            self._initialize_ui_components()
            
            # 加载缓存数据
            self._load_editor_cache()
            
            # 注册设备校准回调
            if self.plugin.device_calibrator:
                self.plugin.device_calibrator.register_calibration_callback(
                    'calibration_progress', self._on_calibration_progress)
                self.plugin.device_calibrator.register_calibration_callback(
                    'calibration_completed', self._on_calibration_completed)
                    
            # 注册高级输入回调
            if self.plugin.advanced_input_processor:
                self.plugin.advanced_input_processor.register_advanced_callback(
                    'pattern_matched', self._on_pattern_matched)
                self.plugin.advanced_input_processor.register_advanced_callback(
                    'sequence_completed', self._on_sequence_completed)
            
            self.initialized = True
            print("✓ 输入编辑器初始化完成")
            return True
            
        except Exception as e:
            print(f"✗ 输入编辑器初始化失败: {e}")
            import traceback
            traceback.print_exc()
            return False
            
    def enable(self) -> bool:
        """
        启用输入编辑器
        
        Returns:
            是否启用成功
        """
        try:
            if not self.initialized:
                print("✗ 输入编辑器未初始化")
                return False
                
            self.enabled = True
            self.running = True
            
            # 启动编辑器线程
            self.editor_thread = threading.Thread(target=self._editor_update_loop, daemon=True)
            self.editor_thread.start()
            
            # 启动监控线程
            if self.editor_config['enable_realtime_update']:
                self.monitor_thread = threading.Thread(target=self._monitor_update_loop, daemon=True)
                self.monitor_thread.start()
            
            print("✓ 输入编辑器已启用")
            return True
            
        except Exception as e:
            print(f"✗ 输入编辑器启用失败: {e}")
            import traceback
            traceback.print_exc()
            return False
            
    def disable(self):
        """禁用输入编辑器"""
        try:
            self.enabled = False
            self.running = False
            
            # 等待线程结束
            if self.editor_thread and self.editor_thread.is_alive():
                self.editor_thread.join(timeout=2.0)
                
            if self.monitor_thread and self.monitor_thread.is_alive():
                self.monitor_thread.join(timeout=2.0)
                
            # 关闭UI组件
            self._close_ui_components()
            
            print("✓ 输入编辑器已禁用")
            
        except Exception as e:
            print(f"✗ 输入编辑器禁用失败: {e}")
            import traceback
            traceback.print_exc()
            
    def finalize(self):
        """清理输入编辑器资源"""
        try:
            self.disable()
            self.ui_components.clear()
            self.editor_data.clear()
            self.operation_history.clear()
            self.monitor_data.clear()
            self.editor_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
                
            # 更新统计信息
            pass
            
        except Exception as e:
            print(f"✗ 输入编辑器更新失败: {e}")
            import traceback
            traceback.print_exc()
            
    def _editor_update_loop(self):
        """编辑器更新循环"""
        try:
            refresh_interval = 1.0 / self.editor_config['refresh_rate']
            
            while self.running:
                if self.enabled:
                    self._update_editor_display()
                    
                time.sleep(refresh_interval)
                
        except Exception as e:
            print(f"✗ 编辑器更新循环失败: {e}")
            
    def _monitor_update_loop(self):
        """监控更新循环"""
        try:
            monitor_interval = 0.1  # 100ms
            
            while self.running:
                if self.enabled and self.editor_config['enable_realtime_update']:
                    self._update_monitor_data()
                    
                time.sleep(monitor_interval)
                
        except Exception as e:
            print(f"✗ 监控更新循环失败: {e}")
            
    def _initialize_ui_components(self):
        """初始化UI组件"""
        try:
            # 在实际实现中，这里会创建实际的UI组件
            # 由于这是模拟实现，我们只创建占位符
            self.ui_components['main_window'] = "MainWindow"
            self.ui_components['toolbar'] = "Toolbar"
            self.ui_components['property_panel'] = "PropertyPanel"
            self.ui_components['preview_panel'] = "PreviewPanel"
            self.ui_components['status_bar'] = "StatusBar"
            self.ui_components['device_list'] = "DeviceList"
            self.ui_components['mapping_editor'] = "MappingEditor"
            self.ui_components['gesture_visualizer'] = "GestureVisualizer"
            self.ui_components['haptic_designer'] = "HapticDesigner"
            self.ui_components['calibration_wizard'] = "CalibrationWizard"
            self.ui_components['advanced_input_panel'] = "AdvancedInputPanel"
            
            print("✓ UI组件初始化完成")
        except Exception as e:
            print(f"✗ UI组件初始化失败: {e}")
            
    def _close_ui_components(self):
        """关闭UI组件"""
        try:
            # 在实际实现中，这里会关闭实际的UI组件
            self.ui_components.clear()
            print("✓ UI组件已关闭")
        except Exception as e:
            print(f"✗ UI组件关闭失败: {e}")
            
    def _load_editor_cache(self):
        """加载编辑器缓存"""
        try:
            # 模拟加载缓存数据
            if self.plugin.device_manager:
                self.editor_data['device_cache'] = self.plugin.device_manager.get_all_input_devices()
                
            if self.plugin.input_mapper:
                self.editor_data['mapping_cache'] = self.plugin.input_mapper.get_all_mappings()
                
            if self.plugin.gesture_recognizer:
                self.editor_data['gesture_cache'] = self.plugin.gesture_recognizer.get_gesture_history()
                
            if self.plugin.haptic_manager:
                self.editor_data['haptic_cache'] = self.plugin.haptic_manager.get_all_haptic_effects()
                
            # 加载校准数据
            if self.plugin.device_calibrator:
                self.editor_data['calibration_data'] = self.plugin.device_calibrator.calibration_data
                
            # 加载高级输入模式数据
            if self.plugin.advanced_input_processor:
                self.editor_data['advanced_patterns'] = self.plugin.advanced_input_processor.get_input_patterns()
                
            print("✓ 编辑器缓存加载完成")
        except Exception as e:
            print(f"✗ 编辑器缓存加载失败: {e}")
            
    def _update_editor_display(self):
        """更新编辑器显示"""
        try:
            current_time = time.time()
            
            # 检查是否需要刷新
            if current_time - self.last_refresh_time >= 1.0 / self.editor_config['refresh_rate']:
                # 更新设备列表
                self._refresh_device_list()
                
                # 更新属性面板
                self._refresh_property_panel()
                
                # 更新预览面板
                if self.editor_config['enable_preview']:
                    self._update_preview_panel()
                    
                # 更新状态栏
                self._update_status_bar()
                
                self.last_refresh_time = current_time
                self.editor_stats['data_refreshed'] += 1
                
        except Exception as e:
            print(f"✗ 编辑器显示更新失败: {e}")
            
    def _update_monitor_data(self):
        """更新监控数据"""
        try:
            # 收集实时数据
            if self.plugin.input_manager:
                live_inputs = self.plugin.input_manager.get_stats()
                self.monitor_data['input_events'].append({
                    'timestamp': time.time(),
                    'data': live_inputs
                })
                
            if self.plugin.gesture_recognizer:
                gestures = self.plugin.gesture_recognizer.get_gesture_stats()
                self.monitor_data['gesture_events'].append({
                    'timestamp': time.time(),
                    'data': gestures
                })
                
            if self.plugin.haptic_manager:
                haptics = self.plugin.haptic_manager.get_haptic_stats()
                self.monitor_data['haptic_events'].append({
                    'timestamp': time.time(),
                    'data': haptics
                })
                
            # 保持历史记录大小
            max_history = 100
            for event_type in ['input_events', 'gesture_events', 'haptic_events', 'advanced_input_events']:
                if len(self.monitor_data[event_type]) > max_history:
                    self.monitor_data[event_type] = self.monitor_data[event_type][-max_history:]
                    
        except Exception as e:
            print(f"✗ 监控数据更新失败: {e}")
            
    def _refresh_device_list(self):
        """刷新设备列表"""
        try:
            if self.plugin.device_manager:
                devices = self.plugin.device_manager.get_all_input_devices()
                self.editor_data['device_cache'] = devices
                # 在实际实现中，这里会更新UI组件
        except Exception as e:
            print(f"✗ 设备列表刷新失败: {e}")
            
    def _refresh_property_panel(self):
        """刷新属性面板"""
        try:
            # 根据当前选择更新属性面板
            selected_device = self.editor_state['selected_device']
            selected_mapping = self.editor_state['selected_mapping']
            
            # 在实际实现中，这里会更新属性面板的显示内容
            pass
        except Exception as e:
            print(f"✗ 属性面板刷新失败: {e}")
            
    def _update_preview_panel(self):
        """更新预览面板"""
        try:
            current_time = time.time()
            if current_time - self.last_preview_update >= 0.1:  # 100ms
                # 更新预览数据
                self.editor_data['live_data'] = self._collect_live_data()
                
                # 在实际实现中，这里会更新预览面板的显示
                self.editor_stats['preview_updates'] += 1
                self.last_preview_update = current_time
                
        except Exception as e:
            print(f"✗ 预览面板更新失败: {e}")
            
    def _collect_live_data(self) -> Dict[str, Any]:
        """
        收集实时数据
        
        Returns:
            实时数据字典
        """
        try:
            live_data = {
                'timestamp': time.time(),
                'devices': {},
                'inputs': {},
                'gestures': {},
                'haptics': {}
            }
            
            # 收集设备数据
            if self.plugin.device_manager:
                live_data['devices'] = self.plugin.device_manager.get_device_stats()
                
            # 收集输入数据
            if self.plugin.input_manager:
                live_data['inputs'] = self.plugin.input_manager.get_stats()
                
            # 收集手势数据
            if self.plugin.gesture_recognizer:
                live_data['gestures'] = self.plugin.gesture_recognizer.get_gesture_stats()
                
            # 收集触觉数据
            if self.plugin.haptic_manager:
                live_data['haptics'] = self.plugin.haptic_manager.get_haptic_stats()
                
            return live_data
            
        except Exception as e:
            print(f"✗ 实时数据收集失败: {e}")
            return {}
            
    def _update_status_bar(self):
        """更新状态栏"""
        try:
            # 在实际实现中，这里会更新状态栏的显示内容
            pass
        except Exception as e:
            print(f"✗ 状态栏更新失败: {e}")
            
    def set_editor_mode(self, mode: EditorMode) -> bool:
        """
        设置编辑器模式
        
        Args:
            mode: 编辑器模式
            
        Returns:
            是否设置成功
        """
        try:
            old_mode = self.editor_state['current_mode']
            self.editor_state['current_mode'] = mode
            
            # 触发模式变更回调
            self._trigger_editor_callback('mode_changed', {
                'old_mode': old_mode.value,
                'new_mode': mode.value
            })
            
            print(f"✓ 编辑器模式已设置: {mode.value}")
            return True
            
        except Exception as e:
            print(f"✗ 编辑器模式设置失败: {e}")
            return False
            
    def select_device(self, device_id: int) -> bool:
        """
        选择设备
        
        Args:
            device_id: 设备ID
            
        Returns:
            是否选择成功
        """
        try:
            self.editor_state['selected_device'] = device_id
            
            # 触发设备选择回调
            self._trigger_editor_callback('device_selected', {
                'device_id': device_id
            })
            
            print(f"✓ 设备已选择: ID {device_id}")
            return True
            
        except Exception as e:
            print(f"✗ 设备选择失败: {e}")
            return False
            
    def select_mapping(self, mapping_id: int) -> bool:
        """
        选择映射
        
        Args:
            mapping_id: 映射ID
            
        Returns:
            是否选择成功
        """
        try:
            self.editor_state['selected_mapping'] = mapping_id
            
            # 触发映射选择回调
            self._trigger_editor_callback('mapping_selected', {
                'mapping_id': mapping_id
            })
            
            print(f"✓ 映射已选择: ID {mapping_id}")
            return True
            
        except Exception as e:
            print(f"✗ 映射选择失败: {e}")
            return False
            
    def select_gesture(self, gesture_id: int) -> bool:
        """
        选择手势
        
        Args:
            gesture_id: 手势ID
            
        Returns:
            是否选择成功
        """
        try:
            self.editor_state['selected_gesture'] = gesture_id
            
            # 触发手势选择回调
            self._trigger_editor_callback('gesture_selected', {
                'gesture_id': gesture_id
            })
            
            print(f"✓ 手势已选择: ID {gesture_id}")
            return True
            
        except Exception as e:
            print(f"✗ 手势选择失败: {e}")
            return False
            
    def select_effect(self, effect_id: int) -> bool:
        """
        选择触觉效果
        
        Args:
            effect_id: 效果ID
            
        Returns:
            是否选择成功
        """
        try:
            self.editor_state['selected_effect'] = effect_id
            
            # 触发效果选择回调
            self._trigger_editor_callback('effect_selected', {
                'effect_id': effect_id
            })
            
            print(f"✓ 触觉效果已选择: ID {effect_id}")
            return True
            
        except Exception as e:
            print(f"✗ 触觉效果选择失败: {e}")
            return False
            
    def start_recording(self) -> bool:
        """
        开始录制
        
        Returns:
            是否开始成功
        """
        try:
            self.editor_state['is_recording'] = True
            print("✓ 录制已开始")
            return True
            
        except Exception as e:
            print(f"✗ 录制开始失败: {e}")
            return False
            
    def stop_recording(self) -> bool:
        """
        停止录制
        
        Returns:
            是否停止成功
        """
        try:
            self.editor_state['is_recording'] = False
            print("✓ 录制已停止")
            return True
            
        except Exception as e:
            print(f"✗ 录制停止失败: {e}")
            return False
            
    def start_testing(self) -> bool:
        """
        开始测试
        
        Returns:
            是否开始成功
        """
        try:
            self.editor_state['is_testing'] = True
            print("✓ 测试已开始")
            return True
            
        except Exception as e:
            print(f"✗ 测试开始失败: {e}")
            return False
            
    def stop_testing(self) -> bool:
        """
        停止测试
        
        Returns:
            是否停止成功
        """
        try:
            self.editor_state['is_testing'] = False
            print("✓ 测试已停止")
            return True
            
        except Exception as e:
            print(f"✗ 测试停止失败: {e}")
            return False
            
    def toggle_preview(self, enable: bool = None) -> bool:
        """
        切换预览
        
        Args:
            enable: 是否启用预览（None表示切换）
            
        Returns:
            是否切换成功
        """
        try:
            if enable is None:
                enable = not self.editor_state['preview_active']
                
            self.editor_state['preview_active'] = enable
            self.editor_config['enable_preview'] = enable
            
            status = "启用" if enable else "禁用"
            print(f"✓ 预览已{status}")
            return True
            
        except Exception as e:
            print(f"✗ 预览切换失败: {e}")
            return False
            
    def undo_operation(self) -> bool:
        """
        撤销操作
        
        Returns:
            是否撤销成功
        """
        try:
            if self.history_pointer >= 0 and self.operation_history:
                # 执行撤销操作
                operation = self.operation_history[self.history_pointer]
                self.history_pointer -= 1
                self.editor_stats['undo_operations'] += 1
                
                print("✓ 操作已撤销")
                return True
            else:
                print("⚠ 没有可撤销的操作")
                return False
                
        except Exception as e:
            print(f"✗ 操作撤销失败: {e}")
            return False
            
    def redo_operation(self) -> bool:
        """
        重做操作
        
        Returns:
            是否重做成功
        """
        try:
            if self.history_pointer < len(self.operation_history) - 1:
                # 执行重做操作
                self.history_pointer += 1
                operation = self.operation_history[self.history_pointer]
                self.editor_stats['redo_operations'] += 1
                
                print("✓ 操作已重做")
                return True
            else:
                print("⚠ 没有可重做的操作")
                return False
                
        except Exception as e:
            print(f"✗ 操作重做失败: {e}")
            return False
            
    def add_operation_to_history(self, operation: Dict[str, Any]):
        """
        添加操作到历史
        
        Args:
            operation: 操作数据
        """
        try:
            # 添加到历史记录
            self.operation_history.append(operation)
            self.history_pointer = len(self.operation_history) - 1
            
            # 保持历史记录大小
            if len(self.operation_history) > self.max_history_size:
                remove_count = len(self.operation_history) - self.max_history_size
                self.operation_history = self.operation_history[remove_count:]
                self.history_pointer -= remove_count
                self.history_pointer = max(0, self.history_pointer)
                
            self.editor_stats['operations_performed'] += 1
            
        except Exception as e:
            print(f"✗ 操作添加到历史失败: {e}")
            
    def _trigger_editor_callback(self, callback_type: str, data: Dict[str, Any]):
        """
        触发编辑器回调
        
        Args:
            callback_type: 回调类型
            data: 回调数据
        """
        try:
            if callback_type in self.editor_callbacks:
                for callback in self.editor_callbacks[callback_type]:
                    try:
                        callback(data)
                    except Exception as e:
                        print(f"✗ 编辑器回调执行失败: {e}")
        except Exception as e:
            print(f"✗ 编辑器回调触发失败: {e}")
            
    def register_editor_callback(self, callback_type: str, callback: callable):
        """
        注册编辑器回调
        
        Args:
            callback_type: 回调类型
            callback: 回调函数
        """
        try:
            if callback_type in self.editor_callbacks:
                self.editor_callbacks[callback_type].append(callback)
                print(f"✓ 编辑器回调已注册: {callback_type}")
            else:
                print(f"✗ 无效的回调类型: {callback_type}")
        except Exception as e:
            print(f"✗ 编辑器回调注册失败: {e}")
            
    def unregister_editor_callback(self, callback_type: str, callback: callable):
        """
        注销编辑器回调
        
        Args:
            callback_type: 回调类型
            callback: 回调函数
        """
        try:
            if callback_type in self.editor_callbacks:
                if callback in self.editor_callbacks[callback_type]:
                    self.editor_callbacks[callback_type].remove(callback)
                    print(f"✓ 编辑器回调已注销: {callback_type}")
        except Exception as e:
            print(f"✗ 编辑器回调注销失败: {e}")
            
    def get_editor_state(self) -> Dict[str, Any]:
        """
        获取编辑器状态
        
        Returns:
            编辑器状态字典
        """
        return self.editor_state.copy()
        
    def get_editor_config(self) -> Dict[str, Any]:
        """
        获取编辑器配置
        
        Returns:
            编辑器配置字典
        """
        return self.editor_config.copy()
        
    def set_editor_config(self, config: Dict[str, Any]) -> bool:
        """
        设置编辑器配置
        
        Args:
            config: 编辑器配置字典
            
        Returns:
            是否设置成功
        """
        try:
            self.editor_config.update(config)
            print(f"✓ 编辑器配置已更新: {self.editor_config}")
            return True
        except Exception as e:
            print(f"✗ 编辑器配置设置失败: {e}")
            return False
            
    def get_editor_data(self, data_type: str) -> Optional[Dict[str, Any]]:
        """
        获取编辑器数据
        
        Args:
            data_type: 数据类型
            
        Returns:
            数据字典或None
        """
        return self.editor_data.get(data_type)
        
    def get_monitor_data(self) -> Dict[str, Any]:
        """
        获取监控数据
        
        Returns:
            监控数据字典
        """
        return self.monitor_data.copy()
        
    def get_editor_stats(self) -> Dict[str, int]:
        """
        获取编辑器统计信息
        
        Returns:
            编辑器统计字典
        """
        return self.editor_stats.copy()
        
    def reset_editor_stats(self):
        """重置编辑器统计信息"""
        try:
            self.editor_stats = {
                'windows_opened': 0,
                'operations_performed': 0,
                'undo_operations': 0,
                'redo_operations': 0,
                'data_refreshed': 0,
                'preview_updates': 0
            }
            print("✓ 编辑器统计信息已重置")
        except Exception as e:
            print(f"✗ 编辑器统计信息重置失败: {e}")
            
    def refresh_editor_data(self):
        """刷新编辑器数据"""
        try:
            self._load_editor_cache()
            self._update_editor_display()
            print("✓ 编辑器数据已刷新")
        except Exception as e:
            print(f"✗ 编辑器数据刷新失败: {e}")
            
    def show_editor_window(self) -> bool:
        """
        显示编辑器窗口
        
        Returns:
            是否显示成功
        """
        try:
            # 在实际实现中，这里会显示实际的编辑器窗口
            self.editor_stats['windows_opened'] += 1
            print("✓ 编辑器窗口已显示")
            return True
        except Exception as e:
            print(f"✗ 编辑器窗口显示失败: {e}")
            return False
            
    def hide_editor_window(self) -> bool:
        """
        隐藏编辑器窗口
        
        Returns:
            是否隐藏成功
        """
        try:
            # 在实际实现中，这里会隐藏实际的编辑器窗口
            print("✓ 编辑器窗口已隐藏")
            return True
        except Exception as e:
            print(f"✗ 编辑器窗口隐藏失败: {e}")
            return False
            
    def export_editor_data(self, file_path: str) -> bool:
        """
        导出编辑器数据
        
        Args:
            file_path: 导出文件路径
            
        Returns:
            是否导出成功
        """
        try:
            # 在实际实现中，这里会导出编辑器数据到文件
            print(f"✓ 编辑器数据已导出: {file_path}")
            return True
        except Exception as e:
            print(f"✗ 编辑器数据导出失败: {e}")
            return False
            
    def import_editor_data(self, file_path: str) -> bool:
        """
        导入编辑器数据
        
        Args:
            file_path: 导入文件路径
            
        Returns:
            是否导入成功
        """
        try:
            # 在实际实现中，这里会从文件导入编辑器数据
            print(f"✓ 编辑器数据已导入: {file_path}")
            return True
        except Exception as e:
            print(f"✗ 编辑器数据导入失败: {e}")
            return False
            
    # 新增的设备校准相关方法
    def start_device_calibration(self, device_id: int) -> bool:
        """
        开始设备校准
        
        Args:
            device_id: 设备ID
            
        Returns:
            是否开始成功
        """
        try:
            if self.plugin.device_calibrator:
                result = self.plugin.device_calibrator.start_calibration(device_id)
                if result:
                    print(f"✓ 设备校准已开始: ID {device_id}")
                return result
            else:
                print("✗ 设备校准器未初始化")
                return False
        except Exception as e:
            print(f"✗ 设备校准启动失败: {e}")
            return False
            
    def cancel_device_calibration(self) -> bool:
        """
        取消设备校准
        
        Returns:
            是否取消成功
        """
        try:
            if self.plugin.device_calibrator:
                result = self.plugin.device_calibrator.cancel_calibration()
                if result:
                    print("✓ 设备校准已取消")
                return result
            else:
                print("✗ 设备校准器未初始化")
                return False
        except Exception as e:
            print(f"✗ 设备校准取消失败: {e}")
            return False
            
    def get_calibration_status(self) -> Dict[str, Any]:
        """
        获取校准状态
        
        Returns:
            校准状态字典
        """
        try:
            if self.plugin.device_calibrator:
                return self.plugin.device_calibrator.get_calibration_status()
            else:
                return {}
        except Exception as e:
            print(f"✗ 校准状态获取失败: {e}")
            return {}
            
    def _on_calibration_progress(self, data: Dict[str, Any]):
        """
        校准进度回调
        
        Args:
            data: 进度数据
        """
        try:
            # 更新监控数据
            self.monitor_data['calibration_progress'] = data
            
            # 触发编辑器回调
            self._trigger_editor_callback('calibration_progress', data)
            
            print(f"✓ 校准进度更新: {data}")
        except Exception as e:
            print(f"✗ 校准进度处理失败: {e}")
            
    def _on_calibration_completed(self, data: Dict[str, Any]):
        """
        校准完成回调
        
        Args:
            data: 完成数据
        """
        try:
            print(f"✓ 设备校准完成: {data}")
            
            # 刷新设备列表以显示校准状态
            self._refresh_device_list()
        except Exception as e:
            print(f"✗ 校准完成处理失败: {e}")
            
    # 新增的高级输入处理相关方法
    def create_input_pattern(self, name: str, inputs: List[str], context: str,
                           description: str = "") -> int:
        """
        创建输入模式
        
        Args:
            name: 模式名称
            inputs: 输入序列
            context: 上下文
            description: 描述
            
        Returns:
            模式ID
        """
        try:
            if self.plugin.advanced_input_processor:
                from .core.advanced_input_processor import InputContext
                context_enum = getattr(InputContext, context.upper(), InputContext.GLOBAL)
                pattern_id = self.plugin.advanced_input_processor.create_input_pattern(
                    name, inputs, context_enum, description)
                if pattern_id >= 0:
                    print(f"✓ 输入模式已创建: {name} (ID: {pattern_id})")
                return pattern_id
            else:
                print("✗ 高级输入处理器未初始化")
                return -1
        except Exception as e:
            print(f"✗ 输入模式创建失败: {e}")
            return -1
            
    def _on_pattern_matched(self, data: Dict[str, Any]):
        """
        模式匹配回调
        
        Args:
            data: 匹配数据
        """
        try:
            # 添加到监控数据
            self.monitor_data['advanced_input_events'].append({
                'type': 'pattern_matched',
                'data': data,
                'timestamp': time.time()
            })
            
            # 触发编辑器回调
            self._trigger_editor_callback('pattern_matched', data)
            
            print(f"✓ 输入模式匹配: {data}")
        except Exception as e:
            print(f"✗ 模式匹配处理失败: {e}")
            
    def _on_sequence_completed(self, data: Dict[str, Any]):
        """
        序列完成回调
        
        Args:
            data: 完成数据
        """
        try:
            # 添加到监控数据
            self.monitor_data['advanced_input_events'].append({
                'type': 'sequence_completed',
                'data': data,
                'timestamp': time.time()
            })
            
            print(f"✓ 输入序列完成: {data}")
        except Exception as e:
            print(f"✗ 序列完成处理失败: {e}")