/**
 * @fileoverview React component for PuzzleGame.jsx
 * 
 * @module games.puzzle.PuzzleGame
 */

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 { createGame } from "./game";
import { gameConfig } from "./config/config";
import { generateDynamicToolbox, registerBlocks } from "./blocks/blocks";
import { GameStateProvider, useGameState } from "../../../contexts/GameStateContext";
import { starterBlocks } from "./config/starterBlock";
import { usePuzzleTour } from "./hooks/usePuzzleTour";
import { debugSolutions } from "./config/debugSolutions";
import "shepherd.js/dist/css/shepherd.css";
import "../../../styles/shepherd-theme.css";

function PuzzleContent() {
  const { setFailureMessage, isDebugMode } = useGameState();
  usePuzzleTour();

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

  const toolboxGenerator = useMemo(() => {


    return (allowedBlocks) => generateDynamicToolbox(allowedBlocks);
  }, []);

  return (
    <GameBase
      gameFactory={createGame}
      gameConfig={gameConfig}
      customFailureHandler={setFailureMessage}
      failureHandler={setFailureMessage}
    >
      <GameEditor>
        <BlocklyEditor
          toolboxGenerator={toolboxGenerator}
          debugSolutions={isDebugMode ? debugSolutions : null}
          starterBlocks={starterBlocks}
        />
      </GameEditor>
    </GameBase>
  );
}

/**
 * Componente interno que monta a cena e o editor do jogo Puzzle.
 * Registra blocos, configura toolbox dinâmico e injeta o `gameFactory`.
 * @returns {JSX.Element} Conteúdo do jogo (editor + canvas)
 */


/**
 * Componente de página que fornece o contexto de estado do jogo Puzzle.
 * Envolve `PuzzleContent` com o `GameStateProvider` configurado.
 * @returns {JSX.Element} Página completa do jogo Puzzle
 */
export default function PuzzleGame() {
  return (
    <GameStateProvider gameConfig={gameConfig}>
      <PuzzleContent />
    </GameStateProvider>
  );
}

PuzzleContent.propTypes = {};
PuzzleGame.propTypes = {};
