import { ref } from 'vue'
import type { TermSessionData } from './types'
import { isSessionWsOpen } from './termNativeStore'

const K_HIST = 'nexus_term_history'

function loadGlobalHistory(): string[] {
  try {
    return JSON.parse(localStorage.getItem(K_HIST) || '[]')
  } catch {
    return []
  }
}

export function useTerminalCmdBar(
  getActiveSession: () => TermSessionData | null,
  sendWsData: (sessionId: string, data: string) => boolean,
) {
  const cmdInput = ref('')

  function sendCmd() {
    const s = getActiveSession()
    if (!s || !isSessionWsOpen(s.id)) return
    const cmd = cmdInput.value
    if (!cmd) return
    s.cmdHistory.push(cmd)
    s.cmdIdx = -1
    if (!sendWsData(s.id, cmd + '\r')) return
    cmdInput.value = ''
    let hist: string[] = loadGlobalHistory()
    if (hist.length >= 200) hist.shift()
    hist.push(cmd)
    localStorage.setItem(K_HIST, JSON.stringify(hist))
  }

  function onCmdKeydown(e: KeyboardEvent) {
    if (e.key === 'Enter' && !e.shiftKey) {
      e.preventDefault()
      sendCmd()
      return
    }
    if (e.key === 'ArrowUp') {
      e.preventDefault()
      cmdHistoryUp()
      return
    }
    if (e.key === 'ArrowDown') {
      e.preventDefault()
      cmdHistoryDown()
    }
  }

  function cmdHistoryUp() {
    const s = getActiveSession()
    if (!s) return
    if (s.cmdIdx < s.cmdHistory.length - 1) {
      s.cmdIdx++
      cmdInput.value = s.cmdHistory[s.cmdHistory.length - 1 - s.cmdIdx]
    }
  }

  function cmdHistoryDown() {
    const s = getActiveSession()
    if (!s) return
    if (s.cmdIdx > 0) {
      s.cmdIdx--
      cmdInput.value = s.cmdHistory[s.cmdHistory.length - 1 - s.cmdIdx]
    } else {
      s.cmdIdx = -1
      cmdInput.value = ''
    }
  }

  function sendCtrl(key: string) {
    const s = getActiveSession()
    if (!s || !isSessionWsOpen(s.id)) return
    sendWsData(s.id, String.fromCharCode(key.toLowerCase().charCodeAt(0) - 96))
  }

  function sendKey(key: string) {
    const s = getActiveSession()
    if (!s || !isSessionWsOpen(s.id)) return
    sendWsData(s.id, key)
  }

  function loadHistoryForNewSession(): string[] {
    return [...loadGlobalHistory()]
  }

  return {
    cmdInput,
    sendCmd,
    onCmdKeydown,
    cmdHistoryUp,
    cmdHistoryDown,
    sendCtrl,
    sendKey,
    loadHistoryForNewSession,
  }
}

export type CmdInputExpose = {
  getInputEl: () => HTMLTextAreaElement | HTMLInputElement | null
  focusInput: () => void
  insertAtSelection: (text: string) => void
  getSelectionText: () => string
  selectAllInput: () => void
}
