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

export default function boilFruit(pot, fruit, player) {
  let newFruitInt, newPermutation, currentState;
  try {
    currentState = pot.permutation.getState("float_fc:fruit");
    newFruitInt = -1;
    if (fruit.typeId == "float_fc:chopped_pear_fruit") {
      newFruitInt = 1;
    } else if (fruit.typeId == "float_fc:chopped_lemon_fruit") {
      newFruitInt = 2;
    } else if (fruit.typeId == "float_fc:chopped_peach_fruit") {
      newFruitInt = 3;
    } else if (fruit.typeId == "float_fc:chopped_orange_fruit") {
      newFruitInt = 4;
    } else if (fruit.typeId == "float_fc:chopped_cherry_fruit") {
      newFruitInt = 5;
    } else if (fruit.typeId == "float_fc:chopped_banana_fruit") {
      newFruitInt = 6;
    } else if (fruit.typeId == "float_fc:chopped_mango_fruit") {
      newFruitInt = 7;
    } else if (fruit.typeId == "float_fc:chopped_frozen_berry_fruit") {
      newFruitInt = 8;
    } else if (fruit.typeId == "float_fc:chopped_coconut_fruit") {
      newFruitInt = 9;
    } else if (fruit.typeId == "float_fc:chopped_plum_fruit") {
      newFruitInt = 10;
    } else if (fruit.typeId == "float_fc:chopped_rainbow_fruit") {
      newFruitInt = 11;
    } else if (fruit.typeId == "float_fc:chopped_walnut_fruit") {
      newFruitInt = 12;
    } else if (fruit.typeId == "float_fc:maple_sap") {
      newFruitInt = 13;
    } else if (fruit.typeId == "float_fc:chopped_apple_fruit") {
      newFruitInt = 14;
    } else if (fruit.typeId == "float_fc:chopped_hazelnut_fruit") {
      newFruitInt = 15;
    }

    if (newFruitInt !== -1 && (currentState == 0 || currentState == newFruitInt)) {
      // Get the current fruit state from the pot
      player.playSound("float_fc.boil_fruit");
      newPermutation = BlockPermutation.resolve("float_fc:boiling_pot", {
        "float_fc:fruit": newFruitInt,
        "float_fc:fill_stage": pot.permutation.getState("float_fc:fill_stage") + 1,
        "minecraft:cardinal_direction": pot.permutation.getState("minecraft:cardinal_direction"),
      });
      if (newPermutation) {
        pot.setPermutation(newPermutation);
        player.runCommand(`clear @s ${fruit.typeId} -1 1`);
        if (pot.permutation.getState("float_fc:fill_stage") > 3 && player.hasTag("boiling_pot_tutorial_glass")) {
          player.runCommand(
            `summon float_fc:glass_want_particle ${pot.center().x} ${pot.center().y + 1} ${pot.center().z}`
          );
          player.removeTag("boiling_pot_tutorial");

          player.runCommand(`event entity @e[type=float_fc:chopped_fruit_want_particle,r=6] float_fc:kill_entity`);
        }
      }
      newPermutation = undefined;
    }
  } catch {}
}
