import { system, world, GameMode, ItemStack } from "@minecraft/server";
import { unloadInventoryandSpawnItems } from "../utilities/helpers";

const boatTypes = ["hb_hr:blight_boat", "hb_hr:blight_boat_with_chest", "hb_hr:cursed_boat", "hb_hr:cursed_boat_with_chest", "hb_hr:black_boat", "hb_hr:black_boat_with_chest"];
const boatKillSources = ["void", "lightning", "fire", "fireTick", "lava", "entityExplosion", "blockExplosion", "soulCampfire", "campfire"];

export class boatHandler {
  static initialized = false;
  static playerBiomeMap = new Map(); // Speichert zuletzt erkannte Biome für jeden Spieler

  constructor() {
    this.initialize();
  }

  initialize() {
    if (!boatHandler.initialized) {
      boatHandler.initialized = true;
      this.processSubscriptions();
    }
  }

  processSubscriptions() {
    world.afterEvents.entityHitEntity.subscribe(this.handleBoat);
    world.afterEvents.entityDie.subscribe(this.handleBoatDie);
    world.afterEvents.itemUse.subscribe(this.handleBoatSpawn);
  }

  handleBoatSpawn(event) {
    const { source, itemStack } = event;
    if (itemStack.typeId.includes("hb_hr:") && itemStack.typeId.includes("boat")) {
      const hitBlock = source.getBlockFromViewDirection({ maxDistance: 9, includeLiquidBlocks: true });
      if (!hitBlock) return;
      if (!hitBlock.block) return;
      const hitPos = hitBlock.block.location;
      source.runCommand("/summon " + itemStack.typeId.replace("_item", "") + " " + (hitPos.x + 0.5) + " " + (hitPos.y + 0.5) + " " + (hitPos.z + 0.5) + " " + source.getRotation().y + " 0");

      if (source.getGameMode() === GameMode.creative) return;
      source.runCommand("/replaceitem entity @s slot.weapon.mainhand 0 air");
    }
  }
  handleBoatDie(event) {
    const source = event.damageSource;
    const entity = event.deadEntity;

    if (boatTypes.includes(entity.typeId) && entity.typeId.includes("chest")) {
      if (boatKillSources.includes(source.cause)) unloadInventoryandSpawnItems(entity);
    }
  }
  handleBoat(event) {
    const damager = event.damagingEntity;
    const hit_entity = event.hitEntity;
    if (!hit_entity.isValid()) {
      return;
    }
    if (damager.typeId == "minecraft:player") {
      // case for not holding any item aka air

      if (boatTypes.includes(hit_entity.typeId)) {
        // world.sendMessage(hit_entity.getProperty("hb_hr:despawn_tick").toString());
        const currentHits = hit_entity.getProperty("hb_hr:despawn_tick");
        if (currentHits < 4) {
          hit_entity.setProperty("hb_hr:despawn_tick", currentHits + 1);
          hit_entity.runCommand("/playanimation @s animation.hb_hr.boat.break");
        }
        if (currentHits >= 4) {
          if (hit_entity.typeId.includes("chest")) {
            //TODO spawn items from inventory, also on fire death
            unloadInventoryandSpawnItems(hit_entity);
          }

          let despawnItem = new ItemStack(hit_entity.typeId + "_item");
          world.getDimension(hit_entity.dimension.id).spawnItem(despawnItem, hit_entity.location);
          hit_entity.remove();
        }
      }
    }
  }
}
