export interface KeyboardActions {
  onMutate?: () => void;
  onRandom?: () => void;
  onProcess?: () => void;
  onDownload?: () => void;
  onDurationDecrease?: (large: boolean) => void;
  onDurationIncrease?: (large: boolean) => void;
  onVolumeDecrease?: (large: boolean) => void;
  onVolumeIncrease?: (large: boolean) => void;
  onEscape?: () => void;
  onUndo?: () => void;
  onPlayFromStart?: () => void;
}

export function createKeyboardHandler(actions: KeyboardActions) {
  return (event: KeyboardEvent) => {
    if (event.target instanceof HTMLInputElement) return;

    const key = event.key.toLowerCase();
    const isLargeAdjustment = event.shiftKey;

    switch (key) {
      case 'z':
        actions.onUndo?.();
        break;
      case 'm':
        actions.onMutate?.();
        break;
      case 'r':
        actions.onRandom?.();
        break;
      case 'p':
        actions.onProcess?.();
        break;
      case 'd':
        actions.onDownload?.();
        break;
      case ' ':
        event.preventDefault();
        actions.onPlayFromStart?.();
        break;
      case 'arrowleft':
        event.preventDefault();
        actions.onDurationDecrease?.(isLargeAdjustment);
        break;
      case 'arrowright':
        event.preventDefault();
        actions.onDurationIncrease?.(isLargeAdjustment);
        break;
      case 'arrowdown':
        event.preventDefault();
        actions.onVolumeDecrease?.(isLargeAdjustment);
        break;
      case 'arrowup':
        event.preventDefault();
        actions.onVolumeIncrease?.(isLargeAdjustment);
        break;
      case 'escape':
        actions.onEscape?.();
        break;
    }
  };
}
