import { system, world, ItemStack, StructureAnimationMode, StructureRotation, StructureSaveMode } from "@minecraft/server";

export function saveCampStructure(backpack, campSize) {
  //////////world.sendMessage("saveCampStructure");
  let pos1 = { x: backpack.location.x + campSize, y: backpack.location.y - 1, z: backpack.location.z + campSize };
  let pos2 = { x: backpack.location.x - campSize, y: backpack.location.y + campSize * 2, z: backpack.location.z - campSize };
  let structureUUID = "hb_cb:" + backpack.id.toString();
  world.structureManager.delete(structureUUID + "Camp");
  system.waitTicks(10).then(() => {
    world.structureManager.createFromWorld(structureUUID + "Camp", backpack.dimension, pos1, pos2, { saveMode: StructureSaveMode.World, includeEntities: false });
    backpack.setDynamicProperty("hb_cb_structure_name", structureUUID);
  });
  //   return structureUUID;
}
export function saveBackpackStructure(backpack, campSize) {
  //////////world.sendMessage("saveBackpackStructure");
  let structureUUID = "hb_cb:" + backpack.id.toString();
  world.structureManager.delete(structureUUID + "Backpack");
  system.waitTicks(5).then(() => {
    world.structureManager.createFromWorld(structureUUID + "Backpack", backpack.dimension, backpack.location, backpack.location, { saveMode: StructureSaveMode.World, includeEntities: true });
    backpack.setDynamicProperty("hb_cb_structure_name", structureUUID);
  });
  //   return structureUUID;
}

export function loadCampStructureFromEntity(backpack, campSize) {
  //////////world.sendMessage("loadCampStructureFromEntity");
  let replaceLoc = { x: backpack.location.x - campSize, y: backpack.location.y - 1, z: backpack.location.z - campSize };
  let campStructureName = backpack.getDynamicProperty("hb_cb_structure_name") ? backpack.getDynamicProperty("hb_cb_structure_name") : "TEST_EMPTY";

  if (campStructureName == "TEST EMPTY") {
    //////////world.sendMessage("EMPTY - LOAD PRESET");
    // saveOldTerrainToEntity(backpack, campSize);

    world.structureManager.place("hb/cb/test_camp", backpack.dimension, replaceLoc, {
      animationMode: StructureAnimationMode.Layers,
      // animationSeconds: 0.1,
      rotation: StructureRotation.None,
    });
    return;
  } else {
    //////////world.sendMessage("FILLED - LOAD OLD CAMP");
    //////////world.sendMessage(campStructureName.toString());
    // saveOldTerrainToEntity(backpack, campSize);
    ////////world.sendMessage("load camp: " + campStructureName + "Camp");
    world.structureManager.place(campStructureName + "Camp", backpack.dimension, replaceLoc, {
      animationMode: StructureAnimationMode.Layers,
      // animationSeconds: 0.1,
      rotation: StructureRotation.None,
    });
  }
}

export function loadBackpackStructure(backpack, campSize) {
  //////////world.sendMessage("loadBackpackStructure");
  let replaceLoc = { x: backpack.location.x - campSize, y: backpack.location.y, z: backpack.location.z - campSize };
  let campStructureName = backpack.getDynamicProperty("hb_cb_structure_name") ? backpack.getDynamicProperty("hb_cb_structure_name") : "TEST_EMPTY";

  world.structureManager.place(campStructureName + "Backpack", backpack.dimension, replaceLoc, {
    animationMode: StructureAnimationMode.Layers,
    // animationSeconds: 0.1,
    rotation: StructureRotation.None,
  });
}

export function saveOldTerrainToEntity(backpack, campSize) {
  //////////world.sendMessage("saveOldTerrainToEntity");
  let structureUUID = "hb_cb:" + backpack.id.toString() + "OldTerrain";
  world.structureManager.delete(structureUUID);
  backpack.setDynamicProperty("hb_cb_old_terrain", structureUUID);
  system.waitTicks(10).then(() => {
    let pos1 = { x: backpack.location.x + campSize, y: backpack.location.y - 1, z: backpack.location.z + campSize };
    let pos2 = { x: backpack.location.x - campSize, y: backpack.location.y + campSize * 2, z: backpack.location.z - campSize };
    world.structureManager.createFromWorld(structureUUID, backpack.dimension, pos1, pos2, { saveMode: StructureSaveMode.World, includeEntities: false });
    backpack.setDynamicProperty("hb_cb_old_terrain", structureUUID);
  });
}

export function loadOldTerrainFromEntity(backpack, campSize) {
  let replaceLoc = { x: backpack.location.x - campSize, y: backpack.location.y - 1, z: backpack.location.z - campSize };
  let oldTerrainStructureName = backpack.getDynamicProperty("hb_cb_old_terrain") ? backpack.getDynamicProperty("hb_cb_old_terrain") : "";

  //////////world.sendMessage("loadOldTerrainFromEntity: " + oldTerrainStructureName);
  world.structureManager.place(oldTerrainStructureName, backpack.dimension, replaceLoc, {
    animationMode: StructureAnimationMode.Layers,
    // animationSeconds: 0.1,
    rotation: StructureRotation.None,
  });
}

export function saveStructureNameToItemAndSpawnItem(backpack, newItemName) {
  //////////world.sendMessage("saveStructureNameToItem");
  let campStructureName = backpack.getDynamicProperty("hb_cb_structure_name");
  let spawnedItem = new ItemStack(newItemName);
  spawnedItem.setDynamicProperty("hb_cb_structure_name", campStructureName);
  ////////world.sendMessage("saved to item: " + campStructureName);
  world.getDimension(backpack.dimension.id).spawnItem(spawnedItem, backpack.location);
}
