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

export default function createSlushie(Slusher, glassbottle, player) {
  let SlusherPermutation,
    currentFruitState,
    fruittype,
    fillstage,
    newPermutation,
    direction,
    newfill,
    newFruitState,
    currentstate;
  if (player.hasTag("slushie_tutorial_glass")) {
    player.runCommand(`event entity @e[type=float_fc:glass_want_particle,r=8] float_fc:kill_entity`);
    player.removeTag("slushie_tutorial_glass");
  }
  SlusherPermutation = Slusher.permutation;
  fillstage = SlusherPermutation.getState("float_fc:fill_stage");
  if (fillstage > 0) {
    currentFruitState = SlusherPermutation.getState("float_fc:fruit");
    fruittype = "minecraft:glass_bottle";

    if (currentFruitState == 1) {
      fruittype = "pear_slushie";
    } else if (currentFruitState == 2) {
      fruittype = "lemon_slushie";
    } else if (currentFruitState == 3) {
      fruittype = "peach_slushie";
    } else if (currentFruitState == 4) {
      fruittype = "orange_slushie";
    } else if (currentFruitState == 5) {
      fruittype = "cherry_slushie";
    } else if (currentFruitState == 6) {
      fruittype = "banana_slushie";
    } else if (currentFruitState == 7) {
      fruittype = "mango_slushie";
    } else if (currentFruitState == 8) {
      fruittype = "frozen_berry_slushie";
    } else if (currentFruitState == 9) {
      fruittype = "coconut_slushie";
    } else if (currentFruitState == 10) {
      fruittype = "plum_slushie";
    } else if (currentFruitState == 11) {
      fruittype = "rainbow_slushie";
    } else if (currentFruitState == 12) {
      fruittype = "walnut_slushie";
    } else if (currentFruitState == 13) {
      fruittype = "apple_slushie";
    } else if (currentFruitState == 14) {
      fruittype = "hazelnut_slushie";
    } else if (currentFruitState == 15) {
      fruittype = "cactus_slushie";
    }

    currentstate = SlusherPermutation.getState("float_fc:fruit");
    direction = SlusherPermutation.getState("minecraft:cardinal_direction");
    newfill = fillstage - 1 >= 0 ? fillstage - 1 : 0;
    newFruitState = newfill > 0 ? currentstate : 0;

    player.playSound("float_fc.create_slushie");
    newPermutation = BlockPermutation.resolve("float_fc:slushie_maker", {
      "float_fc:fruit": newFruitState,
      "float_fc:fill_stage": newfill,
      "minecraft:cardinal_direction": direction,
    });
    if (newPermutation) {
      Slusher.setPermutation(newPermutation);
      itemcontrol(player, fruittype, Slusher, direction);
    }
    direction = newfill = newFruitState = currentstate = undefined;
  }
  newPermutation = SlusherPermutation = currentFruitState = fruittype = fillstage = undefined;
}

function itemcontrol(player, fruittype, Slushloc, direction) {
  let inventory, addedSlush, item, i;
  inventory = player.getComponent("minecraft:inventory");
  if (inventory && inventory.container) {
    addedSlush = 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 (!addedSlush) {
          addSlushtoinventory(player, fruittype, Slushloc, direction);
          addedSlush = true;
        }
        if (item.amount == 1 || !addedSlush) {
          break;
        }
      }
    }
  }
  inventory = item = undefined;
}

function addSlushtoinventory(player, fruittype, Slushloc, direction) {
  const dimension = player.dimension;
  let locyoffset, spawnx, spawnz;
  locyoffset = Slushloc.y + 1;
  spawnx = Slushloc.x;
  spawnz = Slushloc.z;
  if (direction == "north") {
    spawnz = spawnz + 1.5;
  }
  if (direction == "south") {
    spawnz = spawnz - 1.5;
  }
  if (direction == "east") {
    spawnx = spawnx + 1.5;
  }
  if (direction == "west") {
    spawnx = spawnx - 1.5;
  }
  dimension.runCommand(`loot spawn ${spawnx} ${locyoffset} ${spawnz} loot "float/f/slush/${fruittype}"`);
  locyoffset = spawnx = spawnz = undefined;
}
