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

export const buddyHeads = [
  "arcedge_bap:axolotl_head",
  "arcedge_bap:dolphin_head",
  "arcedge_bap:elephant_head",
  "arcedge_bap:flamingo_head",
  "arcedge_bap:frog_head",
  "arcedge_bap:hermit_crab_head",
  "arcedge_bap:lion_head",
  "arcedge_bap:lizard_head",
  "arcedge_bap:mouse_head",
  "arcedge_bap:panther_head",
  "arcedge_bap:parrot_head",
  "arcedge_bap:polar_bear_head",
  "arcedge_bap:teddy_bear_head",
  "arcedge_bap:tiger_head",
  "arcedge_bap:turtle_head",
  "arcedge_bap:wolf_head",
];

export const buddyTorsos = [
  "arcedge_bap:axolotl_torso",
  "arcedge_bap:dolphin_torso",
  "arcedge_bap:elephant_torso",
  "arcedge_bap:flamingo_torso",
  "arcedge_bap:frog_torso",
  "arcedge_bap:hermit_crab_torso",
  "arcedge_bap:lion_torso",
  "arcedge_bap:lizard_torso",
  "arcedge_bap:mouse_torso",
  "arcedge_bap:panther_torso",
  "arcedge_bap:parrot_torso",
  "arcedge_bap:polar_bear_torso",
  "arcedge_bap:teddy_bear_torso",
  "arcedge_bap:tiger_torso",
  "arcedge_bap:turtle_torso",
  "arcedge_bap:wolf_torso",
];

export const buddyLegs = [
  "arcedge_bap:axolotl_legs",
  "arcedge_bap:dolphin_legs",
  "arcedge_bap:elephant_legs",
  "arcedge_bap:flamingo_legs",
  "arcedge_bap:frog_legs",
  "arcedge_bap:hermit_crab_legs",
  "arcedge_bap:lion_legs",
  "arcedge_bap:lizard_legs",
  "arcedge_bap:mouse_legs",
  "arcedge_bap:panther_legs",
  "arcedge_bap:parrot_legs",
  "arcedge_bap:polar_bear_legs",
  "arcedge_bap:teddy_bear_legs",
  "arcedge_bap:tiger_legs",
  "arcedge_bap:turtle_legs",
  "arcedge_bap:wolf_legs",
];

const buddyHeadsMap = Object.fromEntries(buddyHeads.map((id, i) => [id, i]));
const buddyTorsosMap = Object.fromEntries(buddyTorsos.map((id, i) => [id, i]));
const buddyLegsMap = Object.fromEntries(buddyLegs.map((id, i) => [id, i]));

export function getBuddyPartIndex(itemId) {
  if (itemId in buddyHeadsMap) {
    return { type: "head", index: buddyHeadsMap[itemId] };
  } else if (itemId in buddyTorsosMap) {
    return { type: "torso", index: buddyTorsosMap[itemId] };
  } else if (itemId in buddyLegsMap) {
    return { type: "legs", index: buddyLegsMap[itemId] };
  }
  return null;
}

const buddyFood = [
  "arcedge_bap:axolotl_buddy_food",
  "arcedge_bap:dolphin_buddy_food",
  "arcedge_bap:elephant_buddy_food",
  "arcedge_bap:flamingo_buddy_food",
  "arcedge_bap:frog_buddy_food",
  "arcedge_bap:hermit_crab_buddy_food",
  "arcedge_bap:lion_buddy_food",
  "arcedge_bap:lizard_buddy_food",
  "arcedge_bap:mouse_buddy_food",
  "arcedge_bap:panther_buddy_food",
  "arcedge_bap:parrot_buddy_food",
  "arcedge_bap:polar_bear_buddy_food",
  "arcedge_bap:teddy_bear_buddy_food",
  "arcedge_bap:tiger_buddy_food",
  "arcedge_bap:turtle_buddy_food",
  "arcedge_bap:wolf_buddy_food",
];

const buddyFoodMap = Object.fromEntries(buddyFood.map((id, i) => [id, i]));

export function getBuddyFoodIndex(itemId) {
  if (itemId in buddyFoodMap) {
    return { index: buddyFoodMap[itemId] }
  }
  return null;
}

export function dropBuddyParts(entity) {
  const headIndex = entity.getProperty("arcedge_bap:head_part");
  const torsoIndex = entity.getProperty("arcedge_bap:torso_part");
  const legsIndex = entity.getProperty("arcedge_bap:legs_part");

  const dropItem = (itemId) => {
    const stack = new ItemStack(itemId, 1);
    world.getDimension("overworld").spawnItem(stack, entity.location)
  };

  if (buddyHeads[headIndex - 1] && headIndex > 0)
    dropItem(buddyHeads[headIndex - 1]);
  if (buddyTorsos[torsoIndex - 1] && torsoIndex > 0)
    dropItem(buddyTorsos[torsoIndex - 1]);
  if (buddyLegs[legsIndex - 1] && legsIndex > 0)
    dropItem(buddyLegs[legsIndex - 1]);
}

export function checkBuddyProperties(propertyNumber, entity) {
  return (
    entity.getProperty("arcedge_bap:head_part") === propertyNumber ||
    entity.getProperty("arcedge_bap:torso_part") === propertyNumber ||
    entity.getProperty("arcedge_bap:legs_part") === propertyNumber
  );
}