/**
 * Debug Utilities for AI Interactive Fiction
 * Provides debugging and testing tools
 */

class DebugUtils {
    /**
     * Test the text processing pipeline with sample text
     * @param {string} text - Test text to process
     */
    static testTextPipeline(text = "This is a test sentence. Let's see if it displays correctly!") {
        console.log("Debug: Testing text pipeline with:", text);
        
        // Find the text buffer
        const textBuffer = window.TextBuffer || window.moduleRegistry?.getModule('text-buffer');
        if (textBuffer) {
            textBuffer.addText(text);
            console.log("Debug: Text added to buffer");
            return true;
        } else {
            console.error("Debug: TextBuffer not found");
            return false;
        }
    }
    
    /**
     * Test the socket connection
     */
    static testSocketConnection() {
        console.log("Debug: Testing socket connection");
        
        // Find the socket client
        const socketClient = window.SocketClient || window.moduleRegistry?.getModule('socket-client');
        if (socketClient) {
            if (socketClient.isConnected) {
                console.log("Debug: Socket is connected");
                return true;
            } else {
                console.log("Debug: Socket is not connected, attempting connection");
                socketClient.connect();
                return false;
            }
        } else {
            console.error("Debug: SocketClient not found");
            return false;
        }
    }
    
    /**
     * Test the TTS system
     * @param {string} text - Test text to speak
     */
    static testTTS(text = "This is a test of the text to speech system.") {
        console.log("Debug: Testing TTS with:", text);
        
        // Find the TTS player
        const ttsPlayer = window.TTSPlayer || window.moduleRegistry?.getModule('tts');
        if (ttsPlayer) {
            const wasEnabled = ttsPlayer.isEnabled();
            
            // Enable TTS temporarily if it was disabled
            if (!wasEnabled && ttsPlayer.toggle) {
                ttsPlayer.toggle();
            }
            
            // Speak the text
            ttsPlayer.speak(text, (result) => {
                console.log("Debug: TTS completed with result:", result);
                
                // Restore previous enabled state
                if (!wasEnabled && ttsPlayer.toggle) {
                    ttsPlayer.toggle();
                }
            });
            
            return true;
        } else {
            console.error("Debug: TTSPlayer not found");
            return false;
        }
    }
    
    /**
     * Force all modules to reconnect
     */
    static forceReconnect() {
        console.log("Debug: Forcing module reconnection");
        
        // Get all modules
        const registry = window.moduleRegistry;
        if (!registry) {
            console.error("Debug: Module registry not found");
            return false;
        }
        
        const modules = registry.getAllModules();
        
        // UI Controller
        const uiController = modules['ui-controller'];
        if (uiController) {
            if (uiController.textBuffer === null) {
                uiController.textBuffer = modules['text-buffer'];
                console.log("Debug: Reconnected UI Controller to Text Buffer");
                
                // Reinitialize text buffer
                if (uiController.initializeTextBuffer) {
                    uiController.initializeTextBuffer();
                }
            }
            
            if (uiController.ttsHandler === null) {
                uiController.ttsHandler = modules['tts'];
                console.log("Debug: Reconnected UI Controller to TTS Player");
            }
        }
        
        // Socket Client
        const socketClient = modules['socket-client'];
        if (socketClient) {
            if (socketClient.textBuffer === null) {
                socketClient.textBuffer = modules['text-buffer'];
                console.log("Debug: Reconnected Socket Client to Text Buffer");
            }
        }
        
        // Game Loop
        const gameLoop = modules['game-loop'];
        if (gameLoop) {
            if (gameLoop.uiController === null) {
                gameLoop.uiController = modules['ui-controller'];
                console.log("Debug: Reconnected Game Loop to UI Controller");
            }
            
            if (gameLoop.socketClient === null) {
                gameLoop.socketClient = modules['socket-client'];
                console.log("Debug: Reconnected Game Loop to Socket Client");
            }
            
            if (gameLoop.textBuffer === null) {
                gameLoop.textBuffer = modules['text-buffer'];
                console.log("Debug: Reconnected Game Loop to Text Buffer");
            }
        }
        
        return true;
    }
}

// Export as global for easy console access
window.DebugUtils = DebugUtils;

export { DebugUtils };
