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

export default function createJam(pot, glassbottle, player) {
  let newPermutation, potPermutation, currentFruitState, fruittype, choppedfruit, fillstage, newfill, newFruitState;
  potPermutation = pot.permutation;
  fillstage = potPermutation.getState("float_fc:fill_stage");
  if (player.hasTag("boiling_pot_tutorial_glass")) {
    player.runCommand(`event entity @e[type=float_fc:glass_want_particle,r=8] float_fc:kill_entity`);
    player.removeTag("boiling_pot_tutorial_glass");
  }
  if (fillstage >= 4) {
    currentFruitState = potPermutation.getState("float_fc:fruit");
    fruittype = "minecraft:glass_bottle";
    choppedfruit = "";
    if (currentFruitState == 1) {
      fruittype = "pear_jam";
    } else if (currentFruitState == 2) {
      fruittype = "lemon_jam";
    } else if (currentFruitState == 3) {
      fruittype = "peach_jam";
    } else if (currentFruitState == 4) {
      fruittype = "orange_jam";
    } else if (currentFruitState == 5) {
      fruittype = "cherry_jam";
    } else if (currentFruitState == 6) {
      fruittype = "banana_jam";
    } else if (currentFruitState == 7) {
      fruittype = "mango_jam";
    } else if (currentFruitState == 8) {
      fruittype = "frozen_berry_jam";
    } else if (currentFruitState == 9) {
      fruittype = "coconut_jam";
    } else if (currentFruitState == 10) {
      fruittype = "plum_jam";
    } else if (currentFruitState == 11) {
      fruittype = "rainbow_jam";
    } else if (currentFruitState == 12) {
      fruittype = "walnut_jam";
    } else if (currentFruitState == 13) {
      fruittype = "maple_syrup";
    } else if (currentFruitState == 14) {
      fruittype = "apple_jam";
    } else if (currentFruitState == 15) {
      fruittype = "hazelnut_jam";
    }
    newfill = fillstage - 4 >= 0 ? fillstage - 4 : 0;
    newFruitState = newfill > 0 ? 1 : 0;

    player.playSound("float_fc.create_jam");
    newPermutation = BlockPermutation.resolve("float_fc:boiling_pot", {
      "float_fc:fruit": newFruitState,
      "float_fc:fill_stage": newfill,
      "minecraft:cardinal_direction": potPermutation.getState("minecraft:cardinal_direction"),
    });
    if (newPermutation) {
      pot.setPermutation(newPermutation);
      itemcontrol(player, fruittype, choppedfruit, pot);
    }
  }
  newPermutation =
    potPermutation =
    currentFruitState =
    fruittype =
    choppedfruit =
    fillstage =
    newfill =
    newFruitState =
      undefined;
}

function itemcontrol(player, fruittype, choppedfruit, jamloc) {
  let inventory, addedJam, item, i;
  inventory = player.getComponent("minecraft:inventory");
  if (inventory && inventory.container) {
    addedJam = false;
    for (i = 0; i < inventory.container.size; i++) {
      item = inventory.container.getItem(i);
      if (item && item.typeId == "minecraft:glass_bottle") {
        if (item.amount > 1) {
          item.amount -= 1;
          inventory.container.setItem(i, item);
        } else {
          inventory.container.setItem(i, null);
        }
        if (!addedJam) {
          addjamtoinventory(player, fruittype, choppedfruit, jamloc);
          addedJam = true;
        }
        if (item.amount == 1 || !addedJam) {
          break;
        }
      }
    }
  }
  inventory = addedJam = item = undefined;
}

function addjamtoinventory(player, fruittype, choppedfruit, jamloc) {
  const dimension = player.dimension;
  let locyoffset;
  locyoffset = jamloc.y + 0.5;
  dimension.runCommand(`loot spawn ${jamloc.x} ${locyoffset} ${jamloc.z} loot "float/f/jam/single/${fruittype}"`);
}
