import { world, ItemStack } from "@minecraft/server";
import { randomInt } from "./buddy/random";

const dnaBottles = [
  "arcedge_bap:dna_bottle_black",
  "arcedge_bap:dna_bottle_blue",
  "arcedge_bap:dna_bottle_brown",
  "arcedge_bap:dna_bottle_cyan",
  "arcedge_bap:dna_bottle_gray",
  "arcedge_bap:dna_bottle_green",
  "arcedge_bap:dna_bottle_light_blue",
  "arcedge_bap:dna_bottle_light_gray",
  "arcedge_bap:dna_bottle_lime",
  "arcedge_bap:dna_bottle_magenta",
  "arcedge_bap:dna_bottle_orange",
  "arcedge_bap:dna_bottle_pink",
  "arcedge_bap:dna_bottle_purple",
  "arcedge_bap:dna_bottle_red",
  "arcedge_bap:dna_bottle_white",
  "arcedge_bap:dna_bottle_yellow"
];

const dropRules = Object.freeze({
  "minecraft:ender_dragon": { itemId: dnaBottles, min: 5, max: 12, chance: 1 },
  "minecraft:warden": { itemId: dnaBottles, min: 3, max: 5, chance: 1 },
  "minecraft:creaking": { itemId: dnaBottles, min: 2, max: 4, chance: 1 },
  "minecraft:ghast": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:witch": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:blaze": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:breeze": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:allay": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:piglin": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:piglin_brute": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:pillager": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:phantom": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:magma_cube": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:enderman": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:endermite": { itemId: dnaBottles, min: 1, max: 1, chance: 0.75 },
  "minecraft:shulker": { itemId: dnaBottles, min: 1, max: 2, chance: 0.75 },
  "minecraft:spider": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
  "minecraft:zombie": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
  "minecraft:zombie_villager": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
  "minecraft:skeleton": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
  "minecraft:cave_spider": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
  "minecraft:silverfish": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
  "minecraft:stray": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
  "minecraft:bogged": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
  "minecraft:slime": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
  "minecraft:creeper": { itemId: dnaBottles, min: 1, max: 2, chance: 0.25 },
});

function shouldDrop(chance) {
  const c =
    chance > 1 ? Math.min(chance / 100, 1) : Math.max(0, Math.min(chance, 1));
  return Math.random() <= c;
}

world.afterEvents.entityDie.subscribe(function onEntityDie(event) {
  const entity = event.deadEntity;
  if (!entity) return;

  const rule = dropRules[entity.typeId];
  if (!rule) return;

  if (!shouldDrop(rule.chance)) return;

  const count = randomInt(rule.min | 0, rule.max | 0);
  if (count <= 0) return;

  if (!rule.itemId) return;

  try {
    entity.dimension.spawnItem(
      new ItemStack(rule.itemId[randomInt(0, dnaBottles.length)], count),
      entity.location
    );
  }
  catch(err) {}
});
