<script lang="ts">
  import { onMount, onDestroy, createEventDispatcher } from 'svelte';
  import { activeRenderableLines, addToOutputHistory, addToInputHistory, navigateInputHistory, activeInputHistoryIndex, activeConnection, uiSettings, accessibilitySettings, activeInputHistory, activeProfileId, connectionStatus } from '$lib/stores/mudStore';
  import { tick } from 'svelte';
  import { AccessibilityManager } from '$lib/accessibility/AccessibilityManager';

  // Create safe defaults for reactivity
  $: safeRenderableLines = $activeRenderableLines || [];
  $: safeActiveProfileId = $activeProfileId || null;

  // Create event dispatcher
  const dispatch = createEventDispatcher();
  
  // Props
  export let autofocus = true;
  export let placeholder = 'Enter command...';
  export let aria_label = 'MUD Input';
  
  // Local state
  let terminalElement: HTMLDivElement;
  let inputElement: HTMLInputElement;
  let currentInput = '';
  let accessibilityManager: AccessibilityManager | null = null;
  
  // Message navigation state
  let currentFocusedMessageIndex: number = -1;
  let messageElements: HTMLElement[] = [];
  
  // Handle input submission
  async function handleSubmit(event: Event) {
    event.preventDefault();
    
    if (!currentInput.trim()) return;
    
    // Interrupt speech if enabled
    if ($accessibilitySettings.interruptSpeechOnEnter && accessibilityManager && accessibilityManager.isSpeaking()) {
      accessibilityManager.stopSpeech();
    }
    
    // Add to input history
    addToInputHistory(currentInput);
    
    // Show the command in the output (only if not password - for privacy)
    const isPassword = currentInput.startsWith('password') || currentInput.toLowerCase() === $activeInputHistory[$activeInputHistory.length - 2]?.toLowerCase().replace('username', 'password');
    if (!isPassword) {
      addToOutputHistory(`> ${currentInput}`, true);
    } else {
      addToOutputHistory(`> ********`, true);
    }

    // Get the currently active profile id
    const profileId = $activeProfileId;
    
    // Send the command if connected
    if (profileId) {
      // Get connection status for this profile
      const status = $connectionStatus[profileId];
      
      if (status === 'connected') {
        try {
          // Try using the activeConnection first
          if ($activeConnection) {
            $activeConnection.send(currentInput);
          } else {
            // If not available, use the ConnectionManager directly
            const { ConnectionManager } = await import('$lib/connection/ConnectionManager');
            const connectionManager = ConnectionManager.getInstance();
            connectionManager.send(profileId, currentInput);
          }
        } catch (error) {
          console.error('Error sending command:', error);
          addToOutputHistory(`Error sending command: ${error}`, false, [{ pattern: 'Error', color: '#ff5555', isRegex: false }]);
        }
      } else {
        addToOutputHistory(`Not connected to any MUD server. Status: ${status || 'disconnected'}`, false, [{ pattern: 'Not connected', color: '#ff5555', isRegex: false }]);
      }
    } else {
      addToOutputHistory('No profile selected.', false, [{ pattern: 'No profile', color: '#ff5555', isRegex: false }]);
    }
    
    // Clear the input
    currentInput = '';
    dispatch('input', { text: currentInput });
    
    // Focus the input again
    if (inputElement) {
      inputElement.focus();
    }
  }
  
  // Handle keyboard shortcuts
  function handleKeyDown(event: KeyboardEvent) {
    if (event.key === 'ArrowUp') {
      event.preventDefault();
      currentInput = navigateInputHistory('up', currentInput);
    } else if (event.key === 'ArrowDown') {
      event.preventDefault();
      currentInput = navigateInputHistory('down', currentInput);
    } else if (event.key === 'Escape') {
      event.preventDefault();
      currentInput = '';
      // No need to directly set inputHistoryIndex since we have per-profile indexes now
      // This is handled in navigateInputHistory
    }
  }
  
  // Scroll to bottom of terminal
  async function scrollToBottom() {
    await tick();
    if (terminalElement) {
      terminalElement.scrollTop = terminalElement.scrollHeight;
    }
  }
  
  // Handle keyboard navigation in output window
  function handleOutputKeyDown(event: KeyboardEvent) {
    if (event.key === 'PageUp') {
      event.preventDefault();
      if (terminalElement) {
        terminalElement.scrollTop -= terminalElement.clientHeight;
      }
    } else if (event.key === 'PageDown') {
      event.preventDefault();
      if (terminalElement) {
        terminalElement.scrollTop += terminalElement.clientHeight;
      }
    } else if (event.key === 'Home') {
      event.preventDefault();
      if (terminalElement) {
        terminalElement.scrollTop = 0;
        // Focus first message
        navigateToMessage(0);
      }
    } else if (event.key === 'End') {
      event.preventDefault();
      scrollToBottom();
      // Focus last message
      navigateToMessage(messageElements.length - 1);
    } else if (event.key === 'ArrowUp') {
      event.preventDefault();
      navigatePreviousMessage();
    } else if (event.key === 'ArrowDown') {
      event.preventDefault();
      navigateNextMessage();
    }
  }
  
  // Update message elements reference and prepare for navigation
  function updateMessageElements() {
    if (terminalElement) {
      // Get all line elements to make them individually navigable
      messageElements = Array.from(terminalElement.querySelectorAll('.mud-terminal-line'));
      
      // Add tabindex and aria attributes for accessibility
      messageElements.forEach((el, index) => {
        const element = el as HTMLElement;
        element.setAttribute('tabindex', '-1'); // Can be focused but not in tab order
        element.setAttribute('aria-posinset', (index + 1).toString());
        element.setAttribute('aria-setsize', messageElements.length.toString());
        
        // Don't add navigation instructions text to the elements
        // Let the screen reader just read the content
      });
    }
  }
  
  // Navigate to specific message by index
  function navigateToMessage(index: number) {
    if (index < 0 || index >= messageElements.length) return;
    
    // Remove focus from current message
    if (currentFocusedMessageIndex >= 0 && currentFocusedMessageIndex < messageElements.length) {
      messageElements[currentFocusedMessageIndex].classList.remove('focused-message');
    }
    
    // Set current message index
    currentFocusedMessageIndex = index;
    
    // Add focus class to current message
    const messageElement = messageElements[currentFocusedMessageIndex];
    messageElement.classList.add('focused-message');
    messageElement.focus();
    
    // Make sure the message is in view
    messageElement.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
    
    // Announce for screen readers - simplified and concise announcement
    const messageNumber = currentFocusedMessageIndex + 1;
    const totalMessages = messageElements.length;
    const messageContent = messageElement.textContent || '';
    
    // Only announce the message number and content, not terminal instructions
    const announcement = `${messageNumber} of ${totalMessages}: ${messageContent.substring(0, 100)}`;
    
    // Use aria-live region for announcement
    const announcementElement = document.getElementById('message-announcement');
    if (announcementElement) {
      announcementElement.textContent = announcement;
    }
  }
  
  // Navigate to previous message
  function navigatePreviousMessage() {
    if (currentFocusedMessageIndex <= 0) {
      // Already at the first message - go to the first one
      navigateToMessage(0);
    } else {
      navigateToMessage(currentFocusedMessageIndex - 1);
    }
  }
  
  // Navigate to next message
  function navigateNextMessage() {
    if (currentFocusedMessageIndex >= messageElements.length - 1) {
      // Already at the last message
      navigateToMessage(messageElements.length - 1);
    } else {
      navigateToMessage(currentFocusedMessageIndex + 1);
    }
  }
  
  // Watch for renderable lines changes to update display - optimized reactive statement
  $: if (safeRenderableLines.length > 0) {
    // Only scroll and update elements when new lines are added
    // Use a microtask to batch DOM updates
    Promise.resolve().then(() => {
      scrollToBottom();
      updateMessageElements();
    });
  }
  
  // Watch for active profile changes
  $: if ($activeProfileId) {
    console.log(`Active profile is now: ${$activeProfileId}, updating output display`);
    updateOutputDisplay();
  }
  
  // Function to update the displayed output based on the active profile
  async function updateOutputDisplay() {
    console.log('Updating output display...');
    await tick(); // Wait for Svelte to update
    scrollToBottom();
    updateMessageElements();
  }
  
  onMount(() => {
    console.log('MudTerminal component mounted for profile:', $activeProfileId);
    
    // Initialize accessibility manager
    accessibilityManager = new AccessibilityManager();
    
    // Focus input on mount if autofocus is true
    if (autofocus && inputElement) {
      inputElement.focus();
    }
    
    // Initial scroll to bottom
    scrollToBottom();
    
    // Initialize message elements for navigation
    updateMessageElements();
  });
  
  // Format timestamp
  function formatTimestamp(timestamp: number): string {
    const date = new Date(timestamp);
    return date.toLocaleTimeString();
  }
</script>

<div class="mud-terminal-container" 
     class:dark-mode={$uiSettings.isDarkMode} 
     class:high-contrast={$accessibilitySettings.highContrast}
     role="region"
     aria-label="MUD Terminal"
     tabindex="-1">
  
  <!-- Screen reader announcements -->
  <div id="message-announcement" class="sr-only" aria-live="polite"></div>
  
  <div class="mud-terminal-output" 
       bind:this={terminalElement} 
       role="log" 
       aria-live="polite"
       aria-atomic="false"
       aria-relevant="additions"
       aria-label="MUD output"
       tabindex="0"
       on:keydown={handleOutputKeyDown}
       style="font-family: {$uiSettings.font}; font-size: {$accessibilitySettings.fontSize}px; line-height: {$accessibilitySettings.lineSpacing};">
    
    <!-- Optimized rendering using pre-processed lines -->
    {#each safeRenderableLines as line (line.id)}
      <div class="mud-terminal-line" 
           class:mud-input-line={line.isInput} 
           class:mud-terminal-subline={line.isSubline} 
           tabindex="-1">
        {#if $uiSettings.showTimestamps && line.lineIndex === 0}
          <span class="mud-timestamp" aria-hidden="true">[{formatTimestamp(line.timestamp)}]</span>
        {/if}
        <div class="mud-terminal-content">
          {@html line.content}
        </div>
      </div>
    {/each}
  </div>
  
  <form class="mud-terminal-input-form" on:submit={handleSubmit} aria-label="MUD command input form">
    <input
      type="text"
      class="mud-terminal-input"
      bind:this={inputElement}
      bind:value={currentInput}
      on:keydown={handleKeyDown}
      placeholder={placeholder}
      aria-label={aria_label}
      autocomplete="off"
      spellcheck="false"
      style="font-family: {$uiSettings.font}; font-size: {$accessibilitySettings.fontSize}px;"
    />
    <button type="submit" class="mud-terminal-submit-button" aria-label="Send command">Send</button>
  </form>
</div>

<style>
  .mud-terminal-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%; /* Ensure full width */
    background-color: #f8f8f8;
    border: 1px solid #ddd;
    border-radius: 4px;
    overflow: hidden;
    position: relative; /* Ensure proper stacking */
    z-index: 1; /* Ensure proper stacking */
  }
  
  .mud-terminal-container.dark-mode {
    background-color: #282a36;
    border-color: #44475a;
    color: #f8f8f2;
  }
  
  .mud-terminal-container.high-contrast {
    background-color: #000;
    color: #fff;
    border-color: #fff;
  }
  
  .mud-terminal-output {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    font-family: monospace;
    white-space: pre-wrap;
    word-break: break-word;
  }
  
  .mud-terminal-output:focus {
    outline: 2px solid var(--color-primary, #2196f3);
    outline-offset: -2px;
  }
  
  .mud-terminal-output:focus:not(:focus-visible) {
    outline: none;
  }
  
  .mud-terminal-line {
    margin-bottom: 4px;
    overflow-wrap: break-word;
    padding: 2px;
    border-radius: 2px;
  }
  
  .mud-terminal-subline {
    margin-bottom: 1px; /* Less space between split lines */
    margin-left: 10px; /* Indent sublines to show they belong together */
    padding: 1px;
  }
  
  .mud-terminal-content {
    display: inline-block;
    width: 100%;
    white-space: pre-wrap;
  }
  
  /* Ensure <br> tags are properly rendered */
  :global(.mud-terminal-content br) {
    display: block;
    content: "";
    margin-top: 0.25em;
  }
  
  .mud-input-line {
    color: #6272a4;
    font-weight: bold;
  }
  
  .dark-mode .mud-input-line {
    color: #8be9fd;
  }
  
  .high-contrast .mud-input-line {
    color: #ff0;
  }
  
  .mud-timestamp {
    color: #999;
    margin-right: 8px;
    font-size: 0.9em;
    display: inline-block;
    vertical-align: top;
  }
  
  .dark-mode .mud-timestamp {
    color: #6272a4;
  }
  
  .high-contrast .mud-timestamp {
    color: #0f0;
  }
  
  .mud-terminal-input-form {
    display: flex;
    padding: 10px;
    border-top: 1px solid #ddd;
  }
  
  .dark-mode .mud-terminal-input-form {
    border-top-color: #44475a;
  }
  
  .high-contrast .mud-terminal-input-form {
    border-top-color: #fff;
  }
  
  .mud-terminal-input {
    flex: 1;
    padding: 8px;
    font-family: monospace;
    border: 1px solid #ddd;
    border-radius: 4px;
  }
  
  .dark-mode .mud-terminal-input {
    background-color: #383a59;
    color: #f8f8f2;
    border-color: #44475a;
  }
  
  .high-contrast .mud-terminal-input {
    background-color: #000;
    color: #fff;
    border-color: #fff;
  }
  
  .mud-terminal-submit-button {
    margin-left: 10px;
    padding: 8px 16px;
    background-color: #6272a4;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
  }
  
  .dark-mode .mud-terminal-submit-button {
    background-color: #ff79c6;
  }
  
  .high-contrast .mud-terminal-submit-button {
    background-color: #fff;
    color: #000;
    border: 2px solid #fff;
  }
  
  .mud-terminal-submit-button:hover {
    opacity: 0.9;
  }
  
  .mud-terminal-submit-button:active {
    opacity: 0.8;
  }
  
  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
  }
  
  :global(.focused-message) {
    outline: 2px solid var(--color-primary, #2196f3);
    background-color: rgba(33, 150, 243, 0.1);
  }
  
  .dark-mode :global(.focused-message) {
    background-color: rgba(255, 121, 198, 0.2); /* Use a pink highlight for dark mode */
  }
  
  .high-contrast :global(.focused-message) {
    outline: 3px solid #fff;
    background-color: #444;
  }
</style>