import { world, system } from "@minecraft/server";

system.runInterval(() => {
  let entities = world
    .getDimension("overworld")
    .getEntities({ families: ["pb_dino_vehicle"] });
  entities = entities.concat(
    world.getDimension("nether").getEntities({ families: ["pb_dino_vehicle"] })
  );
  entities = entities.concat(
    world.getDimension("the_end").getEntities({ families: ["pb_dino_vehicle"] })
  );

  entities.forEach((vehicle) => {
    const riders = vehicle.getComponent("rideable").getRiders().length;
    const driver = vehicle.getComponent("rideable").getRiders()[0];
    const velocity = vehicle.getVelocity();
    // Check if the vehicle has a driver and is moving
    if (
      riders > 0 &&
      driver &&
      (velocity.x != 0 || velocity.z != 0 || velocity.y != 0)
    ) {
      const inv = vehicle.getComponent("inventory")?.container;
      const equip = inv?.getItem(0);
      // No item? stop
      if (!equip) return;
      // If has fuel, run code
      if (equip.typeId == "pb_dino:fuel_can_full") {
        // Jeep
        if (vehicle.typeId == "pb_dino:jeep") useFuel(vehicle);
        // Boat - also checks if in water
        if (vehicle.typeId == "pb_dino:boat" && vehicle.isInWater)
          useFuel(vehicle);
        if (vehicle.typeId == "pb_dino:plane") useFuel(vehicle);
      }
    }
    // Helicopter has a seperate check to see if in air - use fuel - this allows for fuel to be used when in hover mode
    if (
      riders &&
      driver &&
      vehicle.typeId == "pb_dino:helicopter" &&
      !vehicle.isOnGround
    )
      useFuel(vehicle);

    function useFuel() {
      const inv = vehicle.getComponent("inventory")?.container;
      const equip = inv?.getItem(0);
      if (!equip) return;
      if (equip.typeId != "pb_dino:fuel_can_full") return;
      const fuelQty = equip.getComponent("durability").damage;
      const maxFuel = equip.getComponent("durability").maxDurability;
      const driver = vehicle?.getComponent("rideable").getRiders()[0];

      if (fuelQty == (maxFuel / 10) * 7) {
        driver.runCommandAsync(
          `titleraw @s title {"rawtext":[{"translate":"action.hint.pb_dino:low_fuel_70"}]}`
        );
      }
      if (fuelQty == (maxFuel / 10) * 8) {
        driver.runCommandAsync(
          `titleraw @s title {"rawtext":[{"translate":"action.hint.pb_dino:low_fuel_80"}]}`
        );
      }
      if (fuelQty == (maxFuel / 10) * 9) {
        driver.runCommandAsync(
          `titleraw @s title {"rawtext":[{"translate":"action.hint.pb_dino:low_fuel_90"}]}`
        );
      }
      if (fuelQty != maxFuel) {
        vehicle.runCommandAsync(
          `replaceitem entity @s slot.inventory 0 pb_dino:fuel_can_full 1 ${
            fuelQty + 1
          }`
        );
      }
      if (fuelQty >= maxFuel) {
        vehicle.runCommandAsync(
          `replaceitem entity @s slot.inventory 0 pb_dino:fuel_can_empty`
        );
        driver.runCommandAsync(
          `titleraw @s title {"rawtext":[{"translate":"action.hint.pb_dino:empty_fuel"}]}`
        );
      }
    }
  });
}, 40);
