import { useEffect } from 'react';
import { useEditor } from '../context/EditorContext';
import { useUndoRedo } from '../context/UndoRedoContext';

interface UseKeyboardShortcutsOptions {
  /** Called when Ctrl+S is pressed. */
  readonly onSave: () => void;
}

export function useKeyboardShortcuts({ onSave }: UseKeyboardShortcutsOptions): void {
  const { deleteSelected, clearSelection, selectAll, setTool, copySelected, pasteClipboard } = useEditor();
  const { undo, redo } = useUndoRedo();

  useEffect(() => {
    function handleKeyDown(e: KeyboardEvent) {
      const target = e.target as HTMLElement;
      // Ignore shortcuts when typing in input fields
      if (
        target.tagName === 'INPUT' ||
        target.tagName === 'TEXTAREA' ||
        target.tagName === 'SELECT' ||
        target.isContentEditable
      ) {
        return;
      }

      const isCtrl = e.ctrlKey || e.metaKey;

      // Ctrl+Z / Ctrl+Shift+Z — undo/redo
      if (isCtrl && e.key === 'z' && !e.shiftKey) {
        e.preventDefault();
        undo();
        return;
      }
      if (isCtrl && e.key === 'z' && e.shiftKey) {
        e.preventDefault();
        redo();
        return;
      }
      // Ctrl+Y — redo (alternative)
      if (isCtrl && e.key === 'y') {
        e.preventDefault();
        redo();
        return;
      }

      // Ctrl+S — save
      if (isCtrl && e.key === 's') {
        e.preventDefault();
        onSave();
        return;
      }

      // Ctrl+C — copy selected
      if (isCtrl && e.key === 'c') {
        e.preventDefault();
        copySelected();
        return;
      }

      // Ctrl+V — paste clipboard
      if (isCtrl && e.key === 'v') {
        e.preventDefault();
        pasteClipboard();
        return;
      }

      // Ctrl+D — duplicate (copy + paste)
      if (isCtrl && e.key === 'd') {
        e.preventDefault();
        copySelected();
        pasteClipboard();
        return;
      }

      // Ctrl+A — select all
      if (isCtrl && e.key === 'a') {
        e.preventDefault();
        selectAll();
        return;
      }

      // Delete / Backspace — delete selected
      if (e.key === 'Delete' || e.key === 'Backspace') {
        e.preventDefault();
        deleteSelected();
        return;
      }

      // Escape — deselect / cancel tool
      if (e.key === 'Escape') {
        e.preventDefault();
        clearSelection();
        setTool('select');
        return;
      }

      // Tool shortcuts (single keys, no modifiers)
      if (!isCtrl && !e.altKey) {
        switch (e.key.toLowerCase()) {
          case 'v':
            setTool('select');
            break;
          case 'd':
            setTool('door');
            break;
          case 'w':
            setTool('window');
            break;
          case 'e':
            setTool('electrical');
            break;
          case 'f':
            setTool('furniture');
            break;
          case 'm':
            setTool('measure');
            break;
          case 't':
            setTool('annotate');
            break;
        }
      }
    }

    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [undo, redo, onSave, selectAll, deleteSelected, clearSelection, setTool, copySelected, pasteClipboard]);
}
