import { world, system, BlockPermutation, ItemStack } from "@minecraft/server";
import { forestryLogs, forestryLeaves, forestryFruit, forestryFruitItems } from "../globals";

export default function chopFruit(choppedr, fruit, player) {
  const dimension = player.dimension;
  let choppingboardperm, Fruit, fruitint, choppedfruit, newPermutation;
  choppingboardperm = choppedr.permutation;
  Fruit = fruit.typeId;
  fruitint = choppingboardperm.getState("float_fc:fruit");
  choppedfruit = "";

  if (Fruit == "float_fc:pear_fruit") {
    fruitint = 1;
    choppedfruit = "chopped_pear_fruit";
  } else if (Fruit == "float_fc:lemon_fruit") {
    fruitint = 2;
    choppedfruit = "chopped_lemon_fruit";
  } else if (Fruit == "float_fc:peach_fruit") {
    fruitint = 3;
    choppedfruit = "chopped_peach_fruit";
  } else if (Fruit == "float_fc:orange_fruit") {
    fruitint = 4;
    choppedfruit = "chopped_orange_fruit";
  } else if (Fruit == "float_fc:cherry_fruit") {
    fruitint = 5;
    choppedfruit = "chopped_cherry_fruit";
  } else if (Fruit == "float_fc:walnut_fruit") {
    fruitint = 6;
    choppedfruit = "chopped_walnut_fruit";
  } else if (Fruit == "float_fc:coconut_fruit") {
    fruitint = 7;
    choppedfruit = "chopped_coconut_fruit";
  } else if (Fruit == "float_fc:banana_fruit") {
    fruitint = 8;
    choppedfruit = "chopped_banana_fruit";
  } else if (Fruit == "float_fc:mango_fruit") {
    fruitint = 9;
    choppedfruit = "chopped_mango_fruit";
  } else if (Fruit == "float_fc:frozen_berry_fruit") {
    fruitint = 10;
    choppedfruit = "chopped_fb_fruit";
  } else if (Fruit == "float_fc:plum_fruit") {
    fruitint = 11;
    choppedfruit = "chopped_plum_fruit";
  } else if (Fruit == "float_fc:rainbow_fruit") {
    fruitint = 12;
    choppedfruit = "chopped_rainbow_fruit";
  } else if (Fruit == "minecraft:apple") {
    fruitint = 13;
    choppedfruit = "chopped_apple_fruit";
  } else if (Fruit == "float_fc:hazelnut_fruit") {
    fruitint = 14;
    choppedfruit = "chopped_hazelnut_fruit";
  } else if (Fruit == "float_fc:cactus_fruit") {
    fruitint = 15;
    choppedfruit = "chopped_cactus_fruit";
  }

  player.playSound("float_fc.chop_fruit");
  player.removeTag("chopping_tutorial");
  player.runCommand(`event entity @e[type=float_fc:fruit_want_particle,r=6] float_fc:kill_entity`);

  newPermutation = BlockPermutation.resolve("float_fc:chopping_board", {
    "float_fc:fruit": fruitint,
    "float_fc:chopping": 1,
    "minecraft:cardinal_direction": choppingboardperm.getState("minecraft:cardinal_direction"),
  });

  system.runTimeout(() => {
    resetchopping(choppingboardperm, fruit, player, choppedr);
  }, 25);

  if (newPermutation) {
    choppedr.setPermutation(newPermutation);
    itemcontrol(player, Fruit, choppedfruit, choppedr);
  }
  newPermutation = undefined;
}

function resetchopping(choppingboardperm, fruit, player, choppingloc) {
  let newPermutation;
  newPermutation = BlockPermutation.resolve("float_fc:chopping_board", {
    "float_fc:fruit": 0,
    "float_fc:chopping": 0,
    "minecraft:cardinal_direction": choppingboardperm.getState("minecraft:cardinal_direction"),
  });
  if (newPermutation) {
    choppingloc.setPermutation(newPermutation);
  }
  newPermutation = undefined;
}

function itemcontrol(player, Fruit, choppedfruit, choppingloc) {
  let inventory, item, i;
  let processAmount = 1; // Default to processing 1 item

  inventory = player.getComponent("minecraft:inventory");
  if (inventory && inventory.container) {
    for (i = 0; i < inventory.container.size; i++) {
      item = inventory.container.getItem(i);
      if (item && item.typeId == Fruit) {
        // Determine how many items to process - use the entire stack
        processAmount = Math.min(item.amount, 64); // Limit to a maximum of 64 items

        // Remove the items from inventory
        if (item.amount > processAmount) {
          item.amount -= processAmount;
          inventory.container.setItem(i, item);
        } else {
          inventory.container.setItem(i, null);
        }

        // Add the processed items
        addchoppedtoinventory(player, Fruit, choppedfruit, choppingloc, processAmount);

        // Stop after processing one stack
        break;
      }
    }
  }
  inventory = undefined;
}

function addchoppedtoinventory(player, Fruit, choppedfruit, choppingloc, amount = 1) {
  const dimension = player.dimension;
  let locyoffset;
  locyoffset = choppingloc.y + 0.5;

  // Use a loop to spawn the specified amount of items
  for (let i = 0; i < amount; i++) {
    dimension.runCommand(
      `loot spawn ${choppingloc.x} ${locyoffset} ${choppingloc.z} loot "float/f/chopped/${choppedfruit}"`
    );
  }
}
