/**
 * @fileoverview Utility module for blocks.js
 * 
 * @module games.turtle.blocks.blocks
 */

"use strict";

import * as Blockly from "blockly/core";
import { javascriptGenerator } from "blockly/javascript";

const HUE_TARTARUGA = "#4CAF50";
const HUE_COR = "#9C27B0";
const HUE_REPETICAO = "#FF9800";
const HUE_CANETA = "#1982c4";

// Opções do Dropdowns
const MOVE_OPTIONS = [
  ["avançar", "forward"],
  ["recuar", "backward"],
];
const DISTANCE_OPTIONS = [
  ["20", "20"],
  ["50", "50"],
  ["100", "100"],
  ["150", "150"],
];
const TURN_OPTIONS = [
  [`virar a direita ↻`, "right"],
  [`virar a esquerda ↺`, "left"],
];
const ANGLE_OPTIONS = [
  ["1°", "1"],
  ["45°", "45"],
  ["72°", "72"],
  ["90°", "90"],
  ["120°", "120"],
  ["144°", "144"],
];
const PEN_OPTIONS = [
  ["abaixar caneta", "true"],
  ["levantar caneta", "false"],
];
const REPEAT_OPTIONS = [
  ["3", "3"],
  ["4", "4"],
  ["5", "5"],
  ["360", "360"],
];

const COLOUR_OPTIONS = [
  ["🔴 vermelho", "#ff0000"],
  ["🟠 laranja", "#ffa500"],
  ["🟡 amarelo", "#ffff00"],
  ["🟣 roxo", "#800080"],
  ["🟢 verde", "#008000"],
  ["🔵 azul", "#0000ff"],
  ["⚫ preto", "#000000"],
  ["⚪ branco", "#ffffff"],
];

/**
 * Registra todos os blocos personalizados do Turtle no Blockly.
 * Executa definição de blocos e geradores.
 * @returns {void}
 */
export const registerBlocks = () => {
  defineBlocks();
  defineGenerators();
};

const defineBlocks = () => {
  Blockly.Blocks["turtle_move_internal"] = {
    init: function () {
      this.jsonInit({
        type: "turtle_move_internal",
        message0: "%1 %2",
        args0: [
          {
            type: "field_dropdown",
            name: "DIR",
            options: MOVE_OPTIONS,
          },
          { type: "field_dropdown", name: "VALUE", options: DISTANCE_OPTIONS },
        ],
        previousStatement: true,
        nextStatement: true,
        colour: HUE_TARTARUGA,
        tooltip: "Move a tartaruga para frente ou para trás.",
      });
    },
  };

  Blockly.Blocks["turtle_turn_internal"] = {
    init: function () {
      this.jsonInit({
        type: "turtle_turn_internal",
        message0: "%1 %2",
        args0: [
          {
            type: "field_dropdown",
            name: "DIR",
            options: TURN_OPTIONS,
          },
          { type: "field_dropdown", name: "VALUE", options: ANGLE_OPTIONS },
        ],
        previousStatement: true,
        nextStatement: true,
        colour: "#ED0973",
        tooltip: "Vira a tartaruga para a esquerda ou direita.",
      });
    },
  };

  Blockly.Blocks["turtle_pen"] = {
    init: function () {
      this.jsonInit({
        type: "turtle_pen",
        message0: "%1",
        args0: [
          {
            type: "field_dropdown",
            name: "STATE",
            options: PEN_OPTIONS,
          },
        ],
        previousStatement: true,
        nextStatement: true,
        colour: HUE_CANETA,
        tooltip: "Levanta ou abaixa a caneta.",
      });
    },
  };

  Blockly.Blocks["turtle_repeat_internal"] = {
    init: function () {
      this.jsonInit({
        type: "turtle_repeat_internal",
        message0: "repita %1 vezes",
        args0: [
          {
            type: "field_dropdown",
            name: "TIMES",
            options: REPEAT_OPTIONS,
          },
        ],
        message1: "faça %1",
        args1: [
          {
            type: "input_statement",
            name: "DO",
          },
        ],
        previousStatement: true,
        nextStatement: true,
        colour: HUE_REPETICAO,
        tooltip: "Repete os comandos internos um número fixo de vezes.",
      });
    },
  };

  Blockly.Blocks["turtle_colour_internal"] = {
    init: function () {
      this.jsonInit({
        message0: "definir cor para %1",
        args0: [
          {
            type: "field_dropdown",
            name: "COLOUR",
            options: COLOUR_OPTIONS,
          },
        ],
        previousStatement: true,
        nextStatement: true,
        colour: HUE_COR,
        tooltip: "Define a cor da caneta.",
      });
    },
  };
};

/**
 * Gera uma toolbox dinâmica filtrada pelos blocos permitidos para a fase.
 * @param {string[]} [allowedBlocks=[]] - Lista de tipos de blocos permitidos.
 * @returns {Object} Estrutura JSON compatível com a toolbox do Blockly.
 */
export const generateDynamicToolbox = (allowedBlocks = []) => {
  // Se não receber allowedBlocks, retornar toolbox completa
  if (
    !allowedBlocks ||
    !Array.isArray(allowedBlocks) ||
    allowedBlocks.length === 0
  ) {
    return turtleToolbox;
  }

  // Definições de todos os blocos disponíveis
  const blockDefinitions = {
    turtle_move_internal: {
      kind: "block",
      type: "turtle_move_internal",
    },
    turtle_turn_internal: {
      kind: "block",
      type: "turtle_turn_internal",
    },
    turtle_colour_internal: {
      kind: "block",
      type: "turtle_colour_internal",
    },
    turtle_pen: {
      kind: "block",
      type: "turtle_pen",
    },
    turtle_repeat_internal: {
      kind: "block",
      type: "turtle_repeat_internal",
    },
  };

  // Estrutura base da toolbox com todas as categorias
  const toolboxContents = {
    kind: "categoryToolbox",
    contents: [
      {
        kind: "category",
        name: "Movimento",
        colour: HUE_TARTARUGA,
        contents: [],
      },
      {
        kind: "category",
        name: "Repetição",
        colour: HUE_REPETICAO,
        contents: [],
      },
      {
        kind: "category",
        name: "Cor",
        colour: HUE_COR,
        contents: [],
      },
      {
        kind: "category",
        name: "Caneta",
        colour: HUE_CANETA,
        contents: [],
      },
    ],
  };

  allowedBlocks.forEach((blockId) => {
    const blockDef = blockDefinitions[blockId];

    if (!blockDef) {
      console.warn(`generateDynamicToolbox: Bloco desconhecido: ${blockId}`);
      return;
    }

    // Adicionar à categoria correta
    if (["turtle_move_internal", "turtle_turn_internal"].includes(blockId)) {
      toolboxContents.contents[0].contents.push(blockDef);
    } else if (blockId === "turtle_repeat_internal") {
      toolboxContents.contents[1].contents.push(blockDef);
    } else if (blockId === "turtle_colour_internal") {
      toolboxContents.contents[2].contents.push(blockDef);
    } else if (blockId === "turtle_pen") {
      toolboxContents.contents[3].contents.push(blockDef);
    }
  });

  toolboxContents.contents = toolboxContents.contents.filter(
    (category) => category.contents && category.contents.length > 0,
  );

  // Se não houver categorias válidas, retornar toolbox completa como fallback
  if (toolboxContents.contents.length === 0) {
    console.warn(
      "generateDynamicToolbox: Nenhum bloco válido encontrado, usando toolbox completa",
    );
    return turtleToolbox;
  }

  return toolboxContents;
};

// --- GERADORES DE CÓDIGO ---
/**
 * Define os geradores JavaScript associados aos blocos do Turtle.
 * @private
 * @returns {void}
 */
const defineGenerators = () => {
  javascriptGenerator.STATEMENT_PREFIX = "highlightBlock(%1);\n";
  javascriptGenerator.addReservedWords("highlightBlock");
  let loopCounter = 0;

  // Gera: move(100) ou move(-100)
  javascriptGenerator.forBlock["turtle_move_internal"] = (b) => {
    let value = b.getFieldValue("VALUE");
    if (b.getFieldValue("DIR") === "backward") {
      value = -value;
    }
    return `move(${value});\n`;
  };

  // Gera: turn(90) ou turn(-90)
  javascriptGenerator.forBlock["turtle_turn_internal"] = (b) => {
    let value = b.getFieldValue("VALUE");
    if (b.getFieldValue("DIR") === "right") {
      value = -value;
    }
    return `turn(${value});\n`;
  };

  // Gera: penDown(true) ou penDown(false)
  javascriptGenerator.forBlock["turtle_pen"] = (b) => {
    const state = b.getFieldValue("STATE");
    return `penDown(${state});\n`;
  };

  // Gera: penColour('#ff0000')
  javascriptGenerator.forBlock["turtle_colour_internal"] = (b) =>
    `penColour('${b.getFieldValue("COLOUR")}');\n`;

  // Gera o laço de repetição
  javascriptGenerator.forBlock["turtle_repeat_internal"] = (b) => {
    const r = b.getFieldValue("TIMES");
    const s = javascriptGenerator.statementToCode(b, "DO");
    const varName = `count${loopCounter++}`;
    return `var ${varName} = 0;\nwhile (${varName} < ${r}) {\n${s}  ${varName} = ${varName} + 1;\n}\n`;
  };
};

export const turtleToolbox = {
  kind: "categoryToolbox",
  contents: [
    {
      kind: "category",
      name: "Movimento",
      colour: HUE_TARTARUGA,
      contents: [
        { kind: "block", type: "turtle_move_internal" },
        { kind: "block", type: "turtle_turn_internal" },
      ],
    },
    {
      kind: "category",
      name: "Cor",
      colour: HUE_COR,
      contents: [{ kind: "block", type: "turtle_colour_internal" }],
    },
    {
      kind: "category",
      name: "Caneta",
      colour: HUE_CANETA,
      contents: [{ kind: "block", type: "turtle_pen" }],
    },
    {
      kind: "category",
      name: "Repetição",
      colour: HUE_REPETICAO,
      contents: [{ kind: "block", type: "turtle_repeat_internal" }],
    },
  ],
};
