import { TestSceneC } from "../controllers/TestSceneC";
import { FollowCameraC } from "../controllers/FollowCameraC";
import { PlayerC } from "../controllers/PlayerC";
import { LootableC } from "../controllers/LootableC";
import { TriggerC } from "../controllers/TriggerC";
import { CombatC } from "../controllers/CombatC";
import { JoystickC, SoundC, Template } from "@hitplay/playable_template";
import { LootC } from "../controllers/LootC";
import { PayZoneC } from "../controllers/PayZoneC";
import { HudC } from "../controllers/HudC";
import { HealthBarC } from "../controllers/HealthBarC";
import { VfxManager } from "../resources/vfx/VfxManager";

export const afterResourcesLoadedCb: (() => void) | undefined = async () => {
  TestSceneC.init();
  SoundC.init();

  const joystickZone = document.getElementById("joystick_zone") as HTMLDivElement;
  if (!joystickZone) {
    console.warn("[JoystickC] #joystick_zone element not found in DOM");
  } else {
    JoystickC.init({
      zone: joystickZone,
      fadeTime: 0,
      mode: "dynamic",
      restJoystick: true,
      catchDistance: 1,
      restOpacity: 0,
      follow: false,
    });
  }

  // Player is a cannon body (walls handled by the physics world). Vertical
  // placement follows the ground via a downward ray against the environment,
  // so the character walks correctly on both the sand and the raised road.
  PlayerC.init(
    TestSceneC.characterObject,
    TestSceneC.environment ? [TestSceneC.environment] : [],
    TestSceneC.groundY,
  );

  FollowCameraC.init(TestSceneC.characterObject);

  // HUD: screen overlay (invasion timer, resource counters, weapon, avatar).
  // Built before LootC so the wood icon/count elements exist for the loot flight.
  HudC.init();

  // Crates: show one state + give each a solid collider.
  LootableC.init(TestSceneC.lootableGroup);

  // Health bars: floating bars above crates, cloned from the GLB "UI" prototype.
  HealthBarC.init(TestSceneC.healthBarPrototype);

  // Trigger system: start listening for player-vs-trigger overlaps.
  TriggerC.init(PlayerC.getBody());

  // Combat: proximity trigger around each crate → auto-attack & break it.
  CombatC.init();

  // Loot: spawn loot pieces when a crate breaks.
  LootC.init();

  // Pay zone: walk onto UI_Tool_Zone → spend collected wood into it, then it vanishes.
  PayZoneC.init();

  // VFX: set up the quark particle renderer.
  VfxManager.init();

  Template.disableLoader();
};
