import { expect } from "@esm-bundle/chai";
import * as THREE from "three";
import { GameLoop } from "../../../src/core/GameLoop.js";
import {
  createGameLoopSetup,
  cleanupGameLoop,
  createRunData,
  createMockGameStateManagerForCombat,
  setupCombatUnits,
  cleanupTurnSystem,
} from "./helpers.js";

describe.skip("Core: GameLoop - Combat Highlights CoA 5", function () {
  this.timeout(30000);

  let gameLoop;
  let container;
  let mockGameStateManager;
  let playerUnit;
  let enemyUnit;

  beforeEach(async () => {
    const setup = createGameLoopSetup();
    gameLoop = setup.gameLoop;
    container = setup.container;

    if (gameLoop.turnSystemAbortController) {
      gameLoop.turnSystemAbortController.abort();
    }
    gameLoop.stop();
    if (
      gameLoop.turnSystem &&
      typeof gameLoop.turnSystem.reset === "function"
    ) {
      gameLoop.turnSystem.reset();
    }

    gameLoop.init(container);
    mockGameStateManager = createMockGameStateManagerForCombat();
    gameLoop.gameStateManager = mockGameStateManager;

    const runData = createRunData({
      squad: [{ id: "u1", classId: "CLASS_VANGUARD" }],
    });
    await gameLoop.startLevel(runData, { startAnimation: false });

    const units = setupCombatUnits(gameLoop);
    playerUnit = units.playerUnit;
    enemyUnit = units.enemyUnit;
  });

  afterEach(async () => {
    gameLoop.clearMovementHighlights();
    gameLoop.clearSpawnZoneHighlights();
    cleanupTurnSystem(gameLoop);
    cleanupGameLoop(gameLoop, container);
    await new Promise((resolve) => setTimeout(resolve, 10));
  });

  it("CoA 5: should show movement highlights for player units in combat", () => {
    mockGameStateManager.getCombatState.returns({
      activeUnit: {
        id: playerUnit.id,
        name: playerUnit.name,
      },
      turnQueue: [],
    });

    gameLoop.updateMovementHighlights(playerUnit);

    expect(gameLoop.movementHighlights.size).to.be.greaterThan(0);
    const highlightArray = Array.from(gameLoop.movementHighlights);
    expect(highlightArray.length).to.be.greaterThan(0);
    expect(highlightArray[0]).to.be.instanceOf(THREE.Mesh);
  });
});
