import { system, world, ItemStack, MolangVariableMap, StructureAnimationMode, StructureRotation, StructureSaveMode } from "@minecraft/server";
import { saveCampStructure, saveBackpackStructure, loadBackpackStructure, loadCampStructureFromEntity, saveOldTerrainToEntity, loadOldTerrainFromEntity, saveStructureNameToItemAndSpawnItem } from "../entity/structure_controller";

export function entityHurtController(event) {
  if (event.damageSource.damagingEntity === undefined) return;
}

// wann muss die camp structure gesaved werden:
// beim depsawnen, beim spawn muss sie vom item ins entity

export function entityHitEntityController(event) {
  // world.sendMessage("asdasda");
  const damager = event.damagingEntity;
  const hit_entity = event.hitEntity;
  if (hit_entity.typeId.includes("backpack")) {
    // let campSize = 3;
    // let campState = hit_entity.getProperty("hb_cb:camp_state");
    // let camp_state_trigger = hit_entity.getProperty("hb_cb:camp_state_trigger");
    // let camp_despawn_trigger = hit_entity.getProperty("hb_cb:camp_despawn_trigger");
    if (damager.typeId == "minecraft:player") {
      // world.sendMessage("campstate: " + campState);
      // world.sendMessage("camp_state_trigger: " + camp_state_trigger);
      // world.sendMessage("camp_despawn_trigger: " + camp_despawn_trigger);
      // if (damager.isSneaking) {
      //   // despawn
      //   if (!hit_entity.hasTag("hb_cb:sneak_first_hit")) {
      //     hit_entity.runCommand('/tellraw @a[r=10] {"rawtext": [{"translate":"hb_cb.despawn_camp_hint"}]}');
      //     hit_entity.addTag("hb_cb:sneak_first_hit");
      //     return;
      //   } else {
      //     hit_entity.removeTag("hb_cb:sneak_first_hit");
      //     if (campState == true) {
      //       hit_entity.setProperty("hb_cb:camp_state", false);
      //       saveBackpackStructure(hit_entity, campSize);
      //       saveCampStructure(hit_entity, campSize);
      //       system.waitTicks(10).then(() => {
      //         loadOldTerrainFromEntity(hit_entity, campSize);
      //         saveStructureNameToItemAndSpawnItem(hit_entity, "hb_cb:backpack");
      //         hit_entity.remove();
      //       });
      //       return;
      //     } else {
      //       hit_entity.setProperty("hb_cb:camp_state", false);
      //       saveBackpackStructure(hit_entity, campSize);
      //       system.waitTicks(10).then(() => {
      //         saveStructureNameToItemAndSpawnItem(hit_entity, "hb_cb:backpack");
      //         hit_entity.remove();
      //       });
      //       return;
      //     }
      //   }
      // } else {
      //   // open/close camp
      //   if (!hit_entity.hasTag("hb_cb:first_hit")) {
      //     hit_entity.addTag("hb_cb:first_hit");
      //     return;
      //   } else {
      //     hit_entity.removeTag("hb_cb:first_hit");
      //     if (campState == true) {
      //       saveCampStructure(hit_entity, campSize);
      //       saveBackpackStructure(hit_entity, campSize);
      //       system.waitTicks(10).then(() => {
      //         loadOldTerrainFromEntity(hit_entity, campSize);
      //       });
      //       hit_entity.setProperty("hb_cb:camp_state", false);
      //       return;
      //     } else {
      //       saveOldTerrainToEntity(hit_entity, campSize);
      //       // loadBackpackStructure(hit_entity, campSize);

      //       system.waitTicks(10).then(() => {
      //         loadCampStructureFromEntity(hit_entity, campSize);
      //       });
      //       hit_entity.setProperty("hb_cb:camp_state", true);
      //       return;
      //     }
      //   }
      // }
    }
  }
}
