'use client';

import { useEffect } from 'react';
import { usePathname } from 'next/navigation';
import { useTheme } from '@/contexts/ThemeContext';

interface KeyboardShortcutsActions {
  onCreateTask?: () => void;
  onToggleFilters?: () => void;
  onToggleCompactView?: () => void;
  onToggleSwimlanes?: () => void;
  onToggleObjectives?: () => void;
  onToggleDueDateFilter?: () => void;
  onToggleFontSize?: () => void;
  onNavigatePrevious?: () => void;
  onNavigateNext?: () => void;
  onGoToToday?: () => void;
  onRefresh?: () => void;
  onExport?: () => void;
  onShowAnalytics?: () => void;
  onSave?: () => void;
  onBackup?: () => void;
  onOpenSearch?: () => void;
  onCycleBackground?: () => void;
}

export function useGlobalKeyboardShortcuts(
  actions: KeyboardShortcutsActions = {}
) {
  const pathname = usePathname();
  const { toggleTheme, cycleDarkThemes } = useTheme();

  useEffect(() => {
    const handleKeyDown = (event: KeyboardEvent) => {
      // Éviter les raccourcis si on est dans un input/textarea
      const target = event.target as HTMLElement;
      if (
        target.tagName === 'INPUT' ||
        target.tagName === 'TEXTAREA' ||
        target.contentEditable === 'true'
      ) {
        // Permettre seulement certains raccourcis dans les inputs
        if (event.key === 'Escape') {
          // Esc pour fermer les modales même dans les inputs
          return;
        }
        // Ignorer les autres raccourcis dans les inputs
        return;
      }

      // Raccourcis globaux (toutes les pages)
      if (event.shiftKey) {
        switch (event.key) {
          case 'Q':
            event.preventDefault();
            toggleTheme();
            return;
          case 'W':
            event.preventDefault();
            cycleDarkThemes();
            return;
          case 'K':
            event.preventDefault();
            actions.onOpenSearch?.();
            return;
          case 'B':
            event.preventDefault();
            actions.onCycleBackground?.();
            return;
        }
      }

      // Raccourcis spécifiques par page
      switch (pathname) {
        case '/kanban':
          handleKanbanShortcuts(event, actions);
          break;
        case '/daily':
          handleDailyShortcuts(event, actions);
          break;
        default:
          handleDefaultShortcuts(event, actions);
          break;
      }
    };

    // Fonctions de gestion des raccourcis par page
    const handleKanbanShortcuts = (
      event: KeyboardEvent,
      actions: KeyboardShortcutsActions
    ) => {
      if (event.shiftKey) {
        switch (event.key) {
          case 'N':
            event.preventDefault();
            actions.onCreateTask?.();
            break;
          case 'F':
            event.preventDefault();
            actions.onToggleFilters?.();
            break;
          case 'S':
            event.preventDefault();
            actions.onToggleSwimlanes?.();
            break;
          case 'O':
            event.preventDefault();
            actions.onToggleObjectives?.();
            break;
          case 'D':
            event.preventDefault();
            actions.onToggleDueDateFilter?.();
            break;
          case 'Z':
            event.preventDefault();
            actions.onToggleFontSize?.();
            break;
        }
      } else if (event.key === ' ') {
        event.preventDefault();
        actions.onToggleCompactView?.();
      }
    };

    const handleDailyShortcuts = (
      event: KeyboardEvent,
      actions: KeyboardShortcutsActions
    ) => {
      if (event.shiftKey && event.key === 'H') {
        event.preventDefault();
        actions.onGoToToday?.();
      }
    };

    const handleDefaultShortcuts = (
      event: KeyboardEvent,
      actions: KeyboardShortcutsActions
    ) => {
      switch (event.key) {
        case 'ArrowLeft':
          event.preventDefault();
          actions.onNavigatePrevious?.();
          break;
        case 'ArrowRight':
          event.preventDefault();
          actions.onNavigateNext?.();
          break;
      }
    };

    document.addEventListener('keydown', handleKeyDown);

    return () => {
      document.removeEventListener('keydown', handleKeyDown);
    };
  }, [pathname, actions, toggleTheme, cycleDarkThemes]);
}
