import {
  CameraC_internal,
  CameraType,
  Physics_internal,
  Template,
  Template3d,
  ThreeC_internal,
  FilterScene,
  InstallBanner,
} from "@hitplay/playable_template";
import { CameraC } from "../controllers/CameraC";
import { ThreeC } from "../controllers/ThreeC";
import { Color } from "three";
import { Vec3 } from "cannon-es";
export const beforeResourcesLoadedCb = () => {
  FilterScene.init();
  InstallBanner.init();

  Template3d.init();
  CameraC_internal.init(CameraType.perspective);
  CameraC.setCamera(window.screenSize.portrait);
  ThreeC_internal.init();
  ThreeC.createBaseLights();
  ThreeC.setupDirectionalLight();

  Physics_internal.init(new Vec3(0, -9.81, 0));

  // The player is steered by setting its velocity directly every frame, so
  // ground friction would only fight the intended motion (it dropped the
  // effective speed to ~0.84 of 4 m/s). Disable it globally; boundary walls
  // still block the player via the normal contact constraint
  if (Physics_internal.physicsWorld) {
    Physics_internal.physicsWorld.defaultContactMaterial.friction = 0;
  }

  Template.updateVariableConfig.addDelegate(([category, variable, value]) => {
    if (category === "global") {
      switch (variable) {
        case "light_intensity":
          ThreeC_internal.defaultAmbientLight.intensity = Number(value);
          break;

        case "light_color":
          ThreeC_internal.defaultAmbientLight.color = new Color(value as any);
          break;

        default:
          if (
            [
              "camera_fov_p",
              "camera_position_p",
              "camera_rotation_p",
              "camera_fov_l",
              "camera_position_l",
              "camera_rotation_l",
            ].includes(variable)
          ) {
            CameraC.setCamera(window.screenSize.portrait);
          }
          break;
      }
      return;
    }
  });
};
