import { world, ItemStack, MolangVariableMap, StructureAnimationMode, StructureRotation, StructureSaveMode } from "@minecraft/server";
export function getStringFromArray(arry, string) {
  return arry.find((item) => item.startsWith(string));
}

export function checkStringInArray(arr, string) {
  return arr.some((item) => item.startsWith(string));
}

/**
 * Gets the Gamemode of a player
 * @param {Player} player player to get
 * @param {GameMode} gm The gamemode.
 * @returns {GameMode}
 * @example if (getGamemode(player, 'creative')) return;
 */
export function getGamemode(player, gm) {
  return world.getPlayers({ name: player.name, gameMode: gm }).length;
}

export function getLocationbyFacingBlock(face, location) {
  let placeLoc;
  switch (face) {
    case "Up":
      placeLoc = { x: location.x + 0.5, y: location.y + 1, z: location.z + 0.5 };
      break;
    case "Down":
      placeLoc = { x: location.x + 0.5, y: location.y - 1, z: location.z + 0.5 };
      break;
    case "North":
      placeLoc = { x: location.x + 0.5, y: location.y, z: location.z - 0.5 };
      break;
    case "South":
      placeLoc = { x: location.x + 0.5, y: location.y, z: location.z + 1.5 };
      break;
    case "West":
      placeLoc = { x: location.x - 0.5, y: location.y, z: location.z + 0.5 };
      break;
    case "East":
      placeLoc = { x: location.x + 1.5, y: location.y, z: location.z + 0.5 };
      break;
    default:
      break;
  }
  return placeLoc;
}

export function checkLocation(checkLocation, player, entitylocation, blocksAbove) {
  let mobLightBlockAbove = { x: checkLocation.x, y: checkLocation.y + blocksAbove, z: checkLocation.z };
  let blockAbove = world.getDimension(player.dimension.id).getBlock(mobLightBlockAbove);
  if (blockAbove.typeId == "minecraft:air") {
    let closeEntity = world.getDimension(player.dimension.id).getEntities({
      location: entitylocation,
      maxDistance: 0.5,
      closest: 1,
    });
    if (closeEntity.length === 0 || closeEntity[0] == undefined) {
      return false;
    } else {
      player.runCommand('/tellraw @s {"rawtext":[{"translate":"hb_cb.place_entity_error"}]}');
      return true;
    }
  } else {
    player.runCommand('/tellraw @s {"rawtext":[{"translate":"hb_cb.block_above_error"}]}');
    return true;
  }
}

export function getEntityRotation(entity) {
  let entityRotation = entity.getRotation().y;
  if (entityRotation >= -22.5 && entityRotation <= 22.5) {
    // //////////world.sendMessage("0");
    //////////world.sendMessage("180");
    return 180;
  }
  if (entityRotation >= -67.5 && entityRotation <= -22.5) {
    // //////////world.sendMessage("0");
    //////////world.sendMessage("180");
    return 180;
  }
  if (entityRotation >= -112.5 && entityRotation <= -67.5) {
    // //////////world.sendMessage("90");
    //////////world.sendMessage("90");
    return 90;
  }
  if (entityRotation >= -157.5 && entityRotation <= -112.5) {
    // //////////world.sendMessage("35");
    //////////world.sendMessage("90");
    return 90;
  }
  if ((entityRotation >= 157.5 && entityRotation <= 180) || (entityRotation >= -180 && entityRotation <= -157.5)) {
    // //////////world.sendMessage("180");
    //////////world.sendMessage("0");
    return 0;
  }
  if (entityRotation >= 112.5 && entityRotation <= 157.5) {
    // //////////world.sendMessage("225");
    //////////world.sendMessage("270");
    return 270;
  }
  if (entityRotation >= 67.5 && entityRotation <= 112.5) {
    // //////////world.sendMessage("270");
    //////////world.sendMessage("270");
    return 270;
  }
  if (entityRotation >= 22.5 && entityRotation <= 67.5) {
    // //////////world.sendMessage("315");
    //////////world.sendMessage("180");
    return 180;
  }
}

export function getElementPosition(array, element) {
  const position = array.indexOf(element);
  if (position === -1) {
    return `${element} is not in the list`;
  }
  return position;
}

export function getElementPositionArrayWithObjects(array, patternValue) {
  const position = array.findIndex((element) => element.pattern === patternValue);
  if (position === -1) {
    return `${patternValue} is not in the list`;
  }
  return position;
}

export function stringBetween(str, start, end) {
  const startIndex = str.indexOf(start);
  const endIndex = str.indexOf(end, startIndex + start.length);

  if (startIndex === -1 || endIndex === -1) {
    return null; // If start or end strings are not found
  }

  return str.substring(startIndex + start.length, endIndex);
}

export function getStringBeforeasdsd(substring, fullString) {
  const index = fullString.indexOf(substring);
  if (index !== -1) {
    return fullString.substring(0, index);
  } else {
    return fullString; // Or return an empty string, depending on your needs
  }
}

export function getStringAfter(substring, fullString) {
  const index = fullString.indexOf(substring);
  if (index !== -1) {
    return fullString.substring(index + substring.length);
  } else {
    return ""; // Or return the fullString, depending on your needs
  }
}

export function getStringBefore(str, searchString) {
  const index = str.indexOf(searchString);
  return index === -1 ? str : str.substring(0, index);
}

export function checkBelowEntityDespawn(player, entityTypeId) {
  const nearbyEntities = world.getDimension(player.dimension.id).getEntities({
    type: "hb_cb:" + entityTypeId + "",
    location: player.location,
    maxDistance: 200,
  });
  nearbyEntities.forEach((entity) => {
    if (!entity.isValid()) return;
    else {
      let entityBlockBelow = { x: entity.location.x, y: entity.location.y - 0.5, z: entity.location.z };
      let blockBelow = world.getDimension(entity.dimension.id).getBlock(entityBlockBelow);
      if (!blockBelow) return;
      if (blockBelow.isAir || blockBelow.isLiquid) {
        if (!entity.hasTag("hb_cb:start_block_despawn")) {
          // entity.teleport({ x: entity.location.x, y: entity.location.y + 0.5, z: entity.location.z });
          entity.addTag("hb_cb:start_block_despawn");
        }
        entity.runCommand("/scriptevent hb_cb:" + entityTypeId + "_despawn");
        return;
      }
    }
  });
}

export function checkBelowEntityDespawnRoof(player, familyType) {
  const nearbyEntities = world.getDimension(player.dimension.id).getEntities({
    families: [familyType],
    location: player.location,
    maxDistance: 200,
  });
  nearbyEntities.forEach((entity) => {
    if (!entity.isValid()) return;
    else {
      let entityBlockBelow = { x: entity.location.x, y: entity.location.y - 0.5, z: entity.location.z };
      let blockBelow = world.getDimension(entity.dimension.id).getBlock(entityBlockBelow);
      if (!blockBelow) return;
      if (blockBelow.isAir || blockBelow.isLiquid) {
        if (!entity.hasTag("hb_cb:start_block_despawn")) {
          // entity.teleport({ x: entity.location.x, y: entity.location.y + 0.5, z: entity.location.z });
          entity.addTag("hb_cb:start_block_despawn");
        }
        entity.runCommand("/scriptevent hb_cb:roof_despawn");
        return;
      }
    }
  });
}

export function getSideBlockLocations(face, middleLocation) {
  let leftBlock, rightBlock;
  switch (face) {
    case "north":
      leftBlock = { x: middleLocation.x - 1, y: middleLocation.y, z: middleLocation.z };
      rightBlock = { x: middleLocation.x + 1, y: middleLocation.y, z: middleLocation.z };
      break;
    case "south":
      leftBlock = { x: middleLocation.x + 1, y: middleLocation.y, z: middleLocation.z };
      rightBlock = { x: middleLocation.x - 1, y: middleLocation.y, z: middleLocation.z };
      break;
    case "west":
      leftBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z + 1 };
      rightBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z - 1 };
      break;
    case "east":
      leftBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z - 1 };
      rightBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z + 1 };
      break;
    default:
      console.error("Unexpected face direction: " + face);
      return null;
  }

  return { leftBlock, rightBlock };
}

export function getSideBlockLocations2Blocks(face, middleLocation) {
  let leftBlock, rightBlock, nextLeftBlock, nextRightBlock;

  switch (face) {
    case "north":
      leftBlock = { x: middleLocation.x - 1, y: middleLocation.y, z: middleLocation.z };
      rightBlock = { x: middleLocation.x + 1, y: middleLocation.y, z: middleLocation.z };
      nextLeftBlock = { x: middleLocation.x - 2, y: middleLocation.y, z: middleLocation.z };
      nextRightBlock = { x: middleLocation.x + 2, y: middleLocation.y, z: middleLocation.z };
      break;
    case "south":
      leftBlock = { x: middleLocation.x + 1, y: middleLocation.y, z: middleLocation.z };
      rightBlock = { x: middleLocation.x - 1, y: middleLocation.y, z: middleLocation.z };
      nextLeftBlock = { x: middleLocation.x + 2, y: middleLocation.y, z: middleLocation.z };
      nextRightBlock = { x: middleLocation.x - 2, y: middleLocation.y, z: middleLocation.z };
      break;
    case "west":
      leftBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z + 1 };
      rightBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z - 1 };
      nextLeftBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z + 2 };
      nextRightBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z - 2 };
      break;
    case "east":
      leftBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z - 1 };
      rightBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z + 1 };
      nextLeftBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z - 2 };
      nextRightBlock = { x: middleLocation.x, y: middleLocation.y, z: middleLocation.z + 2 };
      break;
    default:
      console.error("Unexpected face direction: " + face);
      return null;
  }

  return { leftBlock, rightBlock, nextLeftBlock, nextRightBlock };
}
