'use client';

import { useVisualization } from '@/lib/visualizations/use-visualization';
import type { AlgorithmDefinition } from '@/lib/visualizations/types';
import { VisualizationContainer } from '../core/visualization-container';
import { GridView } from '../data-structures/grid-view';
import { CalculationBubble } from '../primitives/calculation-bubble';

interface MatrixTraversalVisualizationProps {
  algorithm: AlgorithmDefinition;
  className?: string;
}

export function MatrixTraversalVisualization({
  algorithm,
  className,
}: MatrixTraversalVisualizationProps) {
  const {
    currentStep,
    currentStepIndex,
    totalSteps,
    playback,
    controls,
    currentPhase,
    progress,
  } = useVisualization(algorithm);

  const { dataState } = currentStep;
  const grid = dataState.grids?.[0] ?? null;
  const calculation = dataState.calculations[0] ?? null;
  const gridPointers = dataState.gridPointers ?? [];

  return (
    <VisualizationContainer
      title={algorithm.title}
      pattern={algorithm.pattern}
      code={algorithm.code}
      currentLine={currentStep.codeLine}
      highlightLines={currentStep.codeHighlightLines}
      explanation={currentStep.explanation}
      decision={currentStep.decision}
      phase={currentPhase}
      variables={dataState.variables}
      currentStepIndex={currentStepIndex}
      totalSteps={totalSteps}
      isPlaying={playback.isPlaying}
      speed={playback.speed}
      controls={controls}
      progress={progress}
      className={className}
    >
      <div className="flex flex-col items-center gap-4">
        {/* Calculation bubble */}
        <div className="flex h-8 items-center justify-center">
          <CalculationBubble calculation={calculation} />
        </div>

        {/* Island Grid */}
        {grid && (
          <GridView
            grid={grid}
            pointers={gridPointers}
            cellSize="md"
            showColLabels={true}
            showRowLabels={true}
          />
        )}
      </div>
    </VisualizationContainer>
  );
}
