/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import type { ThoughtSummary } from '@google/gemini-cli-core';
import type React from 'react';
import { Box, Text } from 'ink';
import { theme } from '../semantic-colors.js';
import { useStreamingContext } from '../contexts/StreamingContext.js';
import { StreamingState } from '../types.js';
import { GeminiRespondingSpinner } from './GeminiRespondingSpinner.js';
import { formatDuration } from '../utils/formatters.js';
import { useTerminalSize } from '../hooks/useTerminalSize.js';
import { isNarrowWidth } from '../utils/isNarrowWidth.js';
import { INTERACTIVE_SHELL_WAITING_PHRASE } from '../hooks/usePhraseCycler.js';

interface LoadingIndicatorProps {
  currentLoadingPhrase?: string;
  wittyPhrase?: string;
  showWit?: boolean;
  showTips?: boolean;
  errorVerbosity?: 'low' | 'full';
  elapsedTime: number;
  inline?: boolean;
  rightContent?: React.ReactNode;
  thought?: ThoughtSummary | null;
  thoughtLabel?: string;
  showCancelAndTimer?: boolean;
  forceRealStatusOnly?: boolean;
  spinnerIcon?: string;
  isHookActive?: boolean;
}

export const LoadingIndicator: React.FC<LoadingIndicatorProps> = ({
  currentLoadingPhrase,
  wittyPhrase,
  showWit = false,
  elapsedTime,
  inline = false,
  rightContent,
  thought,
  thoughtLabel,
  showCancelAndTimer = true,
  forceRealStatusOnly = false,
  spinnerIcon,
  isHookActive = false,
}) => {
  const streamingState = useStreamingContext();
  const { columns: terminalWidth } = useTerminalSize();
  const isNarrow = isNarrowWidth(terminalWidth);

  if (
    streamingState === StreamingState.Idle &&
    !currentLoadingPhrase &&
    !thought
  ) {
    return null;
  }

  // Prioritize the interactive shell waiting phrase over the thought subject
  // because it conveys an actionable state for the user (waiting for input).
  const primaryText =
    currentLoadingPhrase === INTERACTIVE_SHELL_WAITING_PHRASE
      ? currentLoadingPhrase
      : thought?.subject
        ? (thoughtLabel ?? thought.subject)
        : currentLoadingPhrase ||
          (streamingState === StreamingState.Responding
            ? 'Thinking...'
            : undefined);

  const cancelAndTimerContent =
    showCancelAndTimer && streamingState === StreamingState.Responding
      ? `(esc to cancel, ${elapsedTime < 60 ? `${elapsedTime}s` : formatDuration(elapsedTime * 1000)})`
      : null;

  const wittyPhraseNode =
    !forceRealStatusOnly &&
    showWit &&
    wittyPhrase &&
    primaryText === 'Thinking...' ? (
      <Box marginLeft={1}>
        <Text color={theme.text.secondary} dimColor italic>
          {wittyPhrase}
        </Text>
      </Box>
    ) : null;

  if (inline) {
    return (
      <Box>
        <Box marginRight={1}>
          <GeminiRespondingSpinner
            nonRespondingDisplay={
              spinnerIcon ??
              (streamingState === StreamingState.WaitingForConfirmation
                ? '⠏'
                : '')
            }
            isHookActive={isHookActive}
          />
        </Box>
        {primaryText && (
          <Box flexShrink={1}>
            <Text color={theme.text.primary} italic wrap="truncate-end">
              {primaryText}
            </Text>
            {primaryText === INTERACTIVE_SHELL_WAITING_PHRASE && (
              <Text color={theme.ui.active} italic>
                {' '}
                (press tab to focus)
              </Text>
            )}
          </Box>
        )}
        {cancelAndTimerContent && (
          <>
            <Box flexShrink={0} width={1} />
            <Text color={theme.text.secondary}>{cancelAndTimerContent}</Text>
          </>
        )}
        {wittyPhraseNode}
      </Box>
    );
  }

  return (
    <Box paddingLeft={0} flexDirection="column">
      {/* Main loading line */}
      <Box
        width="100%"
        flexDirection={isNarrow ? 'column' : 'row'}
        alignItems={isNarrow ? 'flex-start' : 'center'}
      >
        <Box>
          <Box marginRight={1}>
            <GeminiRespondingSpinner
              nonRespondingDisplay={
                spinnerIcon ??
                (streamingState === StreamingState.WaitingForConfirmation
                  ? '⠏'
                  : '')
              }
              isHookActive={isHookActive}
            />
          </Box>
          {primaryText && (
            <Box flexShrink={1}>
              <Text color={theme.text.primary} italic wrap="truncate-end">
                {primaryText}
              </Text>
              {primaryText === INTERACTIVE_SHELL_WAITING_PHRASE && (
                <Text color={theme.ui.active} italic>
                  {' '}
                  (press tab to focus)
                </Text>
              )}
            </Box>
          )}
          {!isNarrow && cancelAndTimerContent && (
            <>
              <Box flexShrink={0} width={1} />
              <Text color={theme.text.secondary}>{cancelAndTimerContent}</Text>
            </>
          )}
          {!isNarrow && wittyPhraseNode}
        </Box>
        {!isNarrow && <Box flexGrow={1}>{/* Spacer */}</Box>}
        {!isNarrow && rightContent && <Box>{rightContent}</Box>}
      </Box>
      {isNarrow && cancelAndTimerContent && (
        <Box>
          <Text color={theme.text.secondary}>{cancelAndTimerContent}</Text>
        </Box>
      )}
      {isNarrow && wittyPhraseNode}
      {isNarrow && rightContent && <Box>{rightContent}</Box>}
    </Box>
  );
};
