'use client';

import { useEffect, useCallback } from 'react';

interface ShortcutHandlers {
  onQuickSwitcher?: () => void;
  onSearch?: () => void;
  onBold?: () => void;
  onItalic?: () => void;
  onCode?: () => void;
  onStrike?: () => void;
  onLink?: () => void;
  onSend?: () => void;
  onNewLine?: () => void;
  onEscape?: () => void;
  onMarkAllRead?: () => void;
  onToggleSidebar?: () => void;
  onToggleHelp?: () => void;
  onNextChannel?: () => void;
  onPrevChannel?: () => void;
  onNextUnread?: () => void;
}

export function useKeyboardShortcuts(handlers: ShortcutHandlers) {
  const handleKeyDown = useCallback(
    (e: KeyboardEvent) => {
      const isMac = typeof navigator !== 'undefined' && navigator.platform.toLowerCase().includes('mac');
      const modKey = isMac ? e.metaKey : e.ctrlKey;

      // Cmd/Ctrl+K — Quick Switcher / Command Palette
      if (modKey && e.key === 'k') {
        e.preventDefault();
        handlers.onQuickSwitcher?.();
        return;
      }

      // Cmd/Ctrl+Shift+K — Clear unread (mark all read)
      if (modKey && e.shiftKey && e.key === 'K') {
        e.preventDefault();
        handlers.onMarkAllRead?.();
        return;
      }

      // Escape
      if (e.key === 'Escape') {
        handlers.onEscape?.();
        return;
      }

      // Alt+Up/Down — Navigate channels
      if (e.altKey && e.key === 'ArrowDown') {
        e.preventDefault();
        handlers.onNextChannel?.();
        return;
      }
      if (e.altKey && e.key === 'ArrowUp') {
        e.preventDefault();
        handlers.onPrevChannel?.();
        return;
      }

      // Alt+Shift+Down — Next unread channel
      if (e.altKey && e.shiftKey && e.key === 'ArrowDown') {
        e.preventDefault();
        handlers.onNextUnread?.();
        return;
      }

      // Cmd/Ctrl+/ — Toggle help
      if (modKey && e.key === '/') {
        e.preventDefault();
        handlers.onToggleHelp?.();
        return;
      }

      // The following shortcuts only work when typing in an input/textarea
      const target = e.target as HTMLElement;
      const isTyping = target.tagName === 'TEXTAREA' || target.tagName === 'INPUT';

      if (isTyping) {
        // Enter — Send
        if (e.key === 'Enter' && !e.shiftKey) {
          e.preventDefault();
          handlers.onSend?.();
          return;
        }

        // Shift+Enter — New line
        if (e.key === 'Enter' && e.shiftKey) {
          handlers.onNewLine?.();
          return;
        }

        // Cmd/Ctrl+B — Bold
        if (modKey && e.key === 'b') {
          e.preventDefault();
          handlers.onBold?.();
          return;
        }

        // Cmd/Ctrl+I — Italic
        if (modKey && e.key === 'i') {
          e.preventDefault();
          handlers.onItalic?.();
          return;
        }

        // Cmd/Ctrl+Shift+X — Strike
        if (modKey && e.shiftKey && (e.key === 'x' || e.key === 'X')) {
          e.preventDefault();
          handlers.onStrike?.();
          return;
        }

        // Cmd/Ctrl+E — Inline code
        if (modKey && e.key === 'e') {
          e.preventDefault();
          handlers.onCode?.();
          return;
        }

        // Cmd/Ctrl+Shift+L — Link
        if (modKey && e.shiftKey && (e.key === 'l' || e.key === 'L')) {
          e.preventDefault();
          handlers.onLink?.();
          return;
        }
      }
    },
    [handlers]
  );

  useEffect(() => {
    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [handleKeyDown]);
}

export const KEYBOARD_SHORTCUTS_LIST = [
  { keys: '⌘K / Ctrl+K', description: 'Abrir el cambiador rápido de canales' },
  { keys: '⌘Shift+K / Ctrl+Shift+K', description: 'Marcar todos como leídos' },
  { keys: 'Enter', description: 'Enviar mensaje' },
  { keys: 'Shift+Enter', description: 'Nueva línea' },
  { keys: '⌘B / Ctrl+B', description: 'Negrita' },
  { keys: '⌘I / Ctrl+I', description: 'Cursiva' },
  { keys: '⌘Shift+X / Ctrl+Shift+X', description: 'Tachado' },
  { keys: '⌘E / Ctrl+E', description: 'Código inline' },
  { keys: '⌘Shift+L / Ctrl+Shift+L', description: 'Insertar enlace' },
  { keys: 'Alt+↑/↓', description: 'Navegar canales' },
  { keys: 'Alt+Shift+↓', description: 'Ir al siguiente canal no leído' },
  { keys: '⌘/ / Ctrl+/', description: 'Mostrar atajos de teclado' },
  { keys: 'Esc', description: 'Cerrar / Cancelar' },
] as const;