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

export default function slushJuice(slusher, fruit, player) {
  let slusherPermutation, Fruit, newFruitInt, currentFruitState, newPermutation, spawnx, spawnz, fillstage, direction;
  slusherPermutation = slusher.permutation;
  Fruit = fruit.typeId;
  if (Fruit == "float_fc:pear_juice") {
    newFruitInt = 1;
  } else if (Fruit == "float_fc:lemon_juice") {
    newFruitInt = 2;
  } else if (Fruit == "float_fc:peach_juice") {
    newFruitInt = 3;
  } else if (Fruit == "float_fc:orange_juice") {
    newFruitInt = 4;
  } else if (Fruit == "float_fc:cherry_juice") {
    newFruitInt = 5;
  } else if (Fruit == "float_fc:banana_juice") {
    newFruitInt = 6;
  } else if (Fruit == "float_fc:mango_juice") {
    newFruitInt = 7;
  } else if (Fruit == "float_fc:frozen_berry_juice") {
    newFruitInt = 8;
  } else if (Fruit == "float_fc:coconut_juice") {
    newFruitInt = 9;
  } else if (Fruit == "float_fc:plum_juice") {
    newFruitInt = 10;
  } else if (Fruit == "float_fc:rainbow_juice") {
    newFruitInt = 11;
  } else if (Fruit == "float_fc:walnut_juice") {
    newFruitInt = 12;
  } else if (Fruit == "float_fc:apple_juice") {
    newFruitInt = 13;
  } else if (Fruit == "float_fc:hazelnut_juice") {
    newFruitInt = 14;
  } else if (Fruit == "float_fc:cactus_juice") {
    newFruitInt = 15;
  }

  // Get the current fruit state from the slusher
  currentFruitState = slusherPermutation.getState("float_fc:fruit");

  // Check if the current fruit state is 0 or matches the new fruit being added
  if (currentFruitState == 0 || currentFruitState == newFruitInt) {
    if (player.hasTag("slushie_tutorial")) {
      player.runCommand(`event entity @e[type=float_fc:juice_want_particle,r=8] float_fc:kill_entity`);
      player.runCommand(
        `summon float_fc:glass_want_particle ${slusher.center().x} ${slusher.center().y + 0.5} ${slusher.center().z}`
      );
      player.addTag("slushie_tutorial_glass");
      player.removeTag("slushie_tutorial");
    }

    fillstage = slusherPermutation.getState("float_fc:fill_stage");
    direction = slusherPermutation.getState("minecraft:cardinal_direction");
    fillstage++;
    slusher.dimension.runCommand(
      `particle float_fc:slushie_freeze ${slusher.location.x} ${slusher.location.y} ${slusher.location.z}`
    );
    newPermutation = BlockPermutation.resolve("float_fc:slushie_maker", {
      "float_fc:fruit": newFruitInt,
      "float_fc:fill_stage": fillstage,
      "minecraft:cardinal_direction": direction,
    });
    player.playSound("float_fc.add_juice_to_slushie");
    spawnx = slusher.x;
    spawnz = slusher.z;
    if (direction == "north") {
      spawnz = spawnz + 1.5;
    } else if (direction == "south") {
      spawnz = spawnz - 1.5;
    } else if (direction == "east") {
      spawnx = spawnx + 1.5;
    } else if (direction == "west") {
      spawnx = spawnx - 1.5;
    }

    slusher.dimension.runCommand(`loot spawn ${spawnx} ${slusher.y + 0.5} ${spawnz} loot "float/f/slush/glass"`);
    if (newPermutation) {
      slusher.setPermutation(newPermutation);
      player.runCommand(`clear @s ${Fruit} -1 1`);
    }
    if (fillstage == 5 && player.hasTag("slushie_tutorial_glass")) {
      slusher.dimension.runCommand(
        `summon float_fc:glass_want_particle ${slusher.center().x} ${slusher.center().y + 0.5} ${slusher.center().z}`
      );
    }
  }
  slusherPermutation =
    Fruit =
    newFruitInt =
    currentFruitState =
    newPermutation =
    spawnx =
    spawnz =
    fillstage =
    direction =
      undefined;
}
