/**
 * @fileoverview React component for TurtleGame.jsx
 * 
 * @module games.turtle.TurtleGame
 */

/*
  MIGRATION: Wave 2 — Portuguese -> English identifier migration
  TODO: Replace Portuguese aliases (ex.: `currentPhase` -> `currentPhase`) in this file.
  See app/migrations/wave-2-candidates.txt for context.
*/
import React, { useEffect, useMemo } from "react";
import PropTypes from "prop-types";
import GameBase from "../../../components/game/GameBase";
import GameEditor from "../../../components/game/GameEditor";
import BlocklyEditor from "../../../components/game/editors/BlocklyEditor";
import CodeEditor from "../../../components/game/editors/CodeEditor";
import { createGame } from "./game";
import { gameConfig } from "./config/config";
import {
  registerBlocks,
  turtleToolbox,
  generateDynamicToolbox,
} from "./blocks/blocks";
import {
  GameStateProvider,
  useGameState,
} from "../../../contexts/GameStateContext";
import { useTurtleTour } from "./hooks/useTurtleTour";
import { debugSolutions } from "./config/debugSolutions";
import "shepherd.js/dist/css/shepherd.css";
import "../../../styles/shepherd-theme.css";

/**
 * Componente que monta a interface e o editor do jogo Tartaruga.
 * Registra blocos, seleciona toolbox por fase e injeta o editor correto.
 * @returns {JSX.Element} Elemento do jogo com editor Blockly/Code.
 */
function TurtleGameContent() {
  const { currentPhase, setEditorType, isDebugMode, setFailureMessage } =
    useGameState();
  const { startTour } = useTurtleTour();

  useEffect(() => {
    registerBlocks();
  }, []);

  useEffect(() => {
    if (currentPhase === 10) {
      setEditorType("code");
    } else {
      setEditorType("blockly");
    }
  }, [currentPhase, setEditorType]);

  const toolboxGenerator = useMemo(() => {
    const currentPhaseConfig = gameConfig.fases.find(
      (fase) => fase.id === currentPhase,
    );



    return () => {
      if (
        currentPhaseConfig?.allowedBlocks &&
        currentPhaseConfig.allowedBlocks.length > 0
      ) {
        return generateDynamicToolbox(currentPhaseConfig.allowedBlocks);
      }
      return turtleToolbox;
    };
  }, [currentPhase]);

  const renderEditor = () => {
    if (currentPhase === 10) {
      return <CodeEditor />;
    }
    return (
      <BlocklyEditor
        toolboxGenerator={toolboxGenerator}
        debugSolutions={isDebugMode ? debugSolutions : null}
      />
    );
  };

  return (
    <GameBase
      gameFactory={createGame}
      gameConfig={gameConfig}
      onHelpClick={startTour}
      helpHandler={startTour}
      customFailureHandler={setFailureMessage}
      failureHandler={setFailureMessage}
    >
      <GameEditor>{renderEditor()}</GameEditor>
    </GameBase>
  );
}

/**
 * Componente wrapper que provê `GameStateProvider` para o jogo Tartaruga.
 * @returns {JSX.Element} Componente de alto nível do jogo.
 */
export default function TurtleGame() {
  return (
    <GameStateProvider gameConfig={gameConfig}>
      <TurtleGameContent />
    </GameStateProvider>
  );
}

TurtleGameContent.propTypes = {};
TurtleGame.propTypes = {};
