import { world, system } from "@minecraft/server";
import vehiclesValues from "./vehicleValues.js";

const vehicleTypes = [
  "pb_plane:plane_air_tractor",
  "pb_plane:plane_airtrike",
  "pb_plane:plane_biplane",
  "pb_plane:plane_skyhawk",
  "pb_plane:plane_cadanair",
  "pb_plane:plane_cruiser",
  "pb_plane:plane_ember_120",
  "pb_plane:plane_magister",
  "pb_plane:plane_gilbert",
  "pb_plane:plane_glider",
  "pb_plane:plane_galapagos",
  "pb_plane:plane_northstar",
  "pb_plane:plane_wooden",
  "pb_plane:plane_lightning",
  "pb_plane:plane_passenger_jet",
  "pb_plane:plane_ricarde",
  "pb_plane:plane_seaplane",
  "pb_plane:plane_marine",
  "pb_plane:plane_triplane",
  "pb_plane:plane_vampire",
  "pb_plane:heli_cargo",
  "pb_plane:heli_leopard",
  "pb_plane:heli_remedy",
  "pb_plane:drone_passenger",
  "pb_plane:blimp_zeppelin",
  // 1.1 Update vehicles
  "pb_plane:plane_flying_car",
  "pb_plane:balloon_1",
  "pb_plane:balloon_2",
  "pb_plane:balloon_3",
  "pb_plane:hover_tuktuk",
  "pb_plane:hover_tukvan",
  "pb_plane:ufo",
  "pb_plane:hover_bike",
  "pb_plane:hover_vespa",
];

// Track players' previous ride state
const playerRideState = new Map();

function setPlayerCamera(player, getRide) {
  if (!getRide || !player) return;

  const vehicle = Object.values(vehiclesValues).find(
    (v) => v.typeId === getRide.typeId
  );

  if (vehicle) {
    const z_direction_shift = vectorMultiply(
      player.getViewDirection(),
      vehicle.z_camera_shift
    );

    const camera_location = vectorAdd(z_direction_shift, {
      x: player.location.x,
      y: player.location.y + vehicle.y_camera_shift,
      z: player.location.z,
    });

    player.camera.setCamera("minecraft:free", {
      location: camera_location,
      facingEntity: { facingEntity: player },
      rotation: player.getRotation(),
      easeOptions: { easeTime: 0.8, easeType: "Linear" },
    });
  }
}

// Vector Math Functions
function vectorMultiply(vector, multiplier) {
  return {
    x: vector.x * multiplier,
    y: vector.y * multiplier,
    z: vector.z * multiplier,
  };
}

function vectorAdd(vector1, vector2) {
  return {
    x: vector1.x + vector2.x,
    y: vector1.y + vector2.y,
    z: vector1.z + vector2.z,
  };
}

// Run interval to check players
system.runInterval(() => {
  for (const player of world.getPlayers()) {
    const getRide = player.getComponent("minecraft:riding")?.entityRidingOn;
    const isInVehicle = getRide && vehicleTypes.includes(getRide.typeId);
    const wasInVehicle = playerRideState.get(player.name) || false;

    if (isInVehicle) {
      // Player is in a vehicle
      const riders = getRide.getComponent("minecraft:rideable").getRiders();
      const hasFuel = getRide.getProperty("pb_plane:fuel");
      const flying = getRide.getProperty("pb_plane:flying");

      if (!getRide.isOnGround && !flying) {
        if (
          getRide.typeId != "pb_plane:hover_tuktuk" &&
          getRide.typeId != "pb_plane:hover_tukvan"
        ) {
          player.playAnimation("animation.pb_plane.hide_player_legs", {
            blendOutTime: 0.5,
            stopExpression: "query.any_animation_finished",
          });
        }
      }
      if (flying) {
        player.playAnimation("animation.pb_plane.hide_player", {
          blendOutTime: 0.5,
          stopExpression: "query.any_animation_finished",
        });
      }

      if (
        (getRide.typeId == "pb_plane:heli_cargo" ||
          getRide.typeId == "pb_plane:heli_leopard" ||
          getRide.typeId == "pb_plane:heli_remedy" ||
          getRide.typeId == "pb_plane:drone_passenger") &&
        !getRide.isOnGround
      ) {
        player.playAnimation("animation.pb_plane.hide_player", {
          blendOutTime: 0.5,
          stopExpression: "query.is_on_ground",
        });
      }
      if (getRide.typeId === "pb_plane:hover_bike") {
        player.playAnimation("animation.pb_plane.riding_bike", {
          blendOutTime: 0.1,
          stopExpression: "!query.is_riding",
        });
      }
      if (getRide.typeId === "pb_plane:hover_vespa") {
        player.playAnimation("animation.pb_plane.riding_vespa", {
          blendOutTime: 0.1,
          stopExpression: "!query.is_riding",
        });
      }

      if (hasFuel || (!hasFuel && flying)) {
        for (const rider of riders) {
          setPlayerCamera(player, getRide);
        }
      }
    } else if (wasInVehicle) {
      // Player just exited a vehicle → Clear camera
      player.camera.clear();
    }

    // Update the state
    playerRideState.set(player.name, isInVehicle);
  }
});
