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

export default function createJuice(juicer, glassbottle, player) {
  const dimension = player.dimension;
  let newPermutation, juicerPermutation, currentFruitState, fruittype, fillstage, newFruitState, newfill, direction;
  juicerPermutation = juicer.permutation;
  fillstage = juicerPermutation.getState("float_fc:fill_stage");
  if (fillstage != 0) {
    currentFruitState = juicerPermutation.getState("float_fc:fruit");
    fruittype = "minecraft:glass_bottle";

    if (currentFruitState == 1) {
      fruittype = "float_fc:pear_juice";
    } else if (currentFruitState == 2) {
      fruittype = "float_fc:lemon_juice";
    } else if (currentFruitState == 3) {
      fruittype = "float_fc:peach_juice";
    } else if (currentFruitState == 4) {
      fruittype = "float_fc:orange_juice";
    } else if (currentFruitState == 5) {
      fruittype = "float_fc:cherry_juice";
    } else if (currentFruitState == 6) {
      fruittype = "float_fc:banana_juice";
    } else if (currentFruitState == 7) {
      fruittype = "float_fc:mango_juice";
    } else if (currentFruitState == 8) {
      fruittype = "float_fc:frozen_berry_juice";
    } else if (currentFruitState == 9) {
      fruittype = "float_fc:coconut_juice";
    } else if (currentFruitState == 10) {
      fruittype = "float_fc:plum_juice";
    } else if (currentFruitState == 11) {
      fruittype = "float_fc:rainbow_juice";
    } else if (currentFruitState == 12) {
      fruittype = "float_fc:walnut_juice";
    } else if (currentFruitState == 13) {
      fruittype = "float_fc:apple_juice";
    } else if (currentFruitState == 14) {
      fruittype = "float_fc:hazelnut_juice";
    } else if (currentFruitState == 15) {
      fruittype = "float_fc:cactus_juice";
    }

    if (player.hasTag("juicer_tutorial_glass")) {
      player.runCommand(`event entity @e[type=float_fc:glass_want_particle,r=8] float_fc:kill_entity`);
      player.removeTag("juicer_tutorial_glass");
    }
    player.playSound("float_fc.create_juice");

    direction = juicerPermutation.getState("minecraft:cardinal_direction");
    newfill = fillstage - 1 >= 0 ? fillstage - 1 : 0;
    newFruitState = newfill > 0 ? currentFruitState : 0;
    newPermutation = BlockPermutation.resolve("float_fc:juicer", {
      "float_fc:fruit": newFruitState,
      "float_fc:fill_stage": newfill,
      "minecraft:cardinal_direction": direction,
    });
    if (newPermutation) {
      juicer.setPermutation(newPermutation);
      itemcontrol(player, fruittype);
    }
    newPermutation = newFruitState = newfill = direction = undefined;
  }
  fillstage = juicerPermutation = undefined;
}

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

function addjuicetoinventory(player, fruittype) {
  let added, inventory, Juice, item, i, fruitJuice;
  inventory = player.getComponent("minecraft:inventory");
  added = false;
  if (inventory && inventory.container) {
    Juice = new ItemStack(fruittype, 1);
    added = false;
    for (i = 0; i < inventory.container.size; i++) {
      item = inventory.container.getItem(i);
      if (item && item.typeId == fruittype && item.amount < item.maxAmount) {
        item.amount += 1;
        inventory.container.setItem(i, item);
        added = true;
        break;
      } else if (!item) {
        inventory.container.setItem(i, Juice);
        added = true;
        break;
      }
    }
    if (!added) {
      // loots juice if inventory full
      fruitJuice = fruittype.substring(9);
      player.runCommand(
        `loot spawn ${player.location.x} ${player.location.y + 0.5} ${
          player.location.z
        } loot "float/f/juicer/${fruitJuice}"`
      );
      fruitJuice = undefined;
    }
  }
}
