import { world, GameMode, system, ItemStack } from "@minecraft/server";
import { updateBlock } from "../utils/block";

const Utils = {
  getPlayerEquipment(player) {
    return player.getComponent("equippable");
  },
  giveItem(player, id, amount = 1) {
    player.getComponent("inventory").container.addItem(new ItemStack(id, amount));
  },
  getSelectedItem(player) {
    const equip = this.getPlayerEquipment(player);
    return equip ? equip.getEquipment("Mainhand") : null;
  },
  playSound(block, sound) {
    block.dimension.playSound(sound, block.location);
  },
  consumeItem(player, item) {
    if (player.getGameMode() === GameMode.creative) return;
    const equip = this.getPlayerEquipment(player);
    consumeEquipped(equip, item);
  },
  setBlockState(block, stateName, value) {
    updateBlock(block, { [stateName]: value });
  }
};

world.beforeEvents.worldInitialize.subscribe(init => {
  init.blockComponentRegistry.registerCustomComponent("peach:cupcake_logic", {
    onPlayerInteract(e) {
      const { player, block } = e;
      
      if (!player || !block) return;
      const isSneaking = player.isSneaking;
      if (isSneaking) return;
      const nutrition = 3;
      
      const currentStage = block.permutation.getState("peach:food_stage");
      const { x, y, z } = block.location;
      
      block.dimension.playSound("random.eat", { x, y, z });
      
      system.run(() => {
        try {
          const saturationAmplifier = Math.floor(nutrition / 2);
          player.runCommand(`effect @s saturation 1 ${saturationAmplifier} true`);
        } catch (err) {}
      });
      
      if (currentStage === 0) {
        block.setPermutation(
          block.permutation.withState("peach:food_stage", 1)
        );
      } else if (currentStage === 1) {
        block.dimension.setBlockType({ x, y, z }, "minecraft:air");
      }
    }
  });
  init.blockComponentRegistry.registerCustomComponent("peach:cake_logic", {
    onPlayerInteract(e) {
      const { player, block } = e;
      
      if (!player || !block) return;
      const isSneaking = player.isSneaking;
      if (isSneaking) return;
      const nutrition = 5;
      
      const currentStage = block.permutation.getState("peach:food_stage");
      const { x, y, z } = block.location;
      
      block.dimension.playSound("random.eat", { x, y, z });
      
      system.run(() => {
        try {
          const saturationAmplifier = Math.floor(nutrition / 2);
          player.runCommand(`effect @s saturation 1 ${saturationAmplifier} true`);
        } catch (err) {}
      });
      
      if (currentStage === 0) {
        block.setPermutation(
          block.permutation.withState("peach:food_stage", 1)
        );
      }
      if (currentStage === 1) {
        block.setPermutation(
          block.permutation.withState("peach:food_stage", 2)
        );
      }
      if (currentStage === 2) {
        block.setPermutation(
          block.permutation.withState("peach:food_stage", 3)
        );
      }
      if (currentStage === 3) {
        block.setPermutation(
          block.permutation.withState("peach:food_stage", 4)
        );
      }
      if (currentStage === 4) {
        block.setPermutation(
          block.permutation.withState("peach:food_stage", 5)
        );
      }
      if (currentStage === 5) {
        block.setPermutation(
          block.permutation.withState("peach:food_stage", 6)
        );
      }
      if (currentStage === 6) {
        block.dimension.setBlockType({ x, y, z }, "minecraft:air");
      }
    }
  });
  init.blockComponentRegistry.registerCustomComponent("peach:spaghetti_logic", {
    onPlayerInteract(e) {
      const { player, block } = e;
      if (!player || !block) return;
      
      const nutrition = 15;
      const { x, y, z } = block.location;
      const isSneaking = player.isSneaking;
      
      if (isSneaking) {
        Utils.giveItem(player, "peach:plate_spaghetti");
        block.dimension.setBlockType({ x, y, z }, "minecraft:air");
        block.dimension.playSound("random.pop", { x, y, z });
        return;
      }
      if (!isSneaking) {
        system.run(() => {
        try {
          const saturationAmplifier = Math.floor(nutrition / 2);
          player.runCommand(`effect @s saturation 1 ${saturationAmplifier} true`);
        } catch (err) {}
        });
        block.dimension.playSound("random.eat", { x, y, z });
        block.dimension.setBlockType({ x, y, z }, "peach:plate");
      }
    }
  });
  init.blockComponentRegistry.registerCustomComponent("peach:pancake_logic", {
    onPlayerInteract(e) {
      const { player, block } = e;
      if (!player || !block) return;
      
      const nutrition = 15;
      const { x, y, z } = block.location;
      const isSneaking = player.isSneaking;
      
      if (isSneaking) {
        Utils.giveItem(player, "peach:plate_pancake");
        block.dimension.setBlockType({ x, y, z }, "minecraft:air");
        block.dimension.playSound("random.pop", { x, y, z });
        return;
      }
      if (!isSneaking) {
        system.run(() => {
        try {
          const saturationAmplifier = Math.floor(nutrition / 2);
          player.runCommand(`effect @s saturation 1 ${saturationAmplifier} true`);
        } catch (err) {}
        });
        block.dimension.playSound("random.eat", { x, y, z });
        block.dimension.setBlockType({ x, y, z }, "peach:plate");
      }
    }
  });
  init.blockComponentRegistry.registerCustomComponent("peach:plate_logic", {
    onPlayerInteract: e => {
      const { player, block } = e;
      const selectedItem = Utils.getSelectedItem(player);

      let stage = block.permutation.getState("peach:stage");
      let plate_item = block.permutation.getState("peach:plate_item");

      const itemMap = {
        1: "peach:hamburger_beef",
        2: "peach:hamburger_big_bacon_beef",
        3: "peach:hamburger_big_chicken",
        4: "peach:hamburger_cheese",
        5: "peach:hamburger_shrimp",
        6: "peach:hamburger_vegan",
        7: "peach:pizza_bacon_and_pineapple",
        8: "peach:pizza_cheese",
        9: "peach:pizza_coriander",
        10: "peach:pizza_onion",
        11: "peach:pizza_oregano_and_tomatoes"
      };
      
      if (stage > 0) {
        const itemNoPlate = itemMap[plate_item];
        const isPizza = plate_item >= 7 && plate_item <= 11;
        
        if (selectedItem?.typeId === itemNoPlate) {
          if (stage < 4) {
            Utils.consumeItem(player, selectedItem);
            Utils.setBlockState(block, "peach:stage", stage + 1);
            Utils.playSound(block, "use.honey_block");
            return;
          }
        } else {
          Utils.giveItem(player, itemNoPlate);
          if (isPizza) {
            const newStage = stage - 1;
            if (newStage === 0) {
              Utils.setBlockState(block, "peach:plate_item", 0);
            }
            Utils.setBlockState(block, "peach:stage", newStage);
          } else {
            Utils.setBlockState(block, "peach:plate_item", 0);
            Utils.setBlockState(block, "peach:stage", 0);
          }
          Utils.playSound(block, "use.honey_block");
          return;
        }
      }

      if (stage === 0 && plate_item === 0) {
        if (selectedItem?.typeId === "peach:hamburger_beef") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 1);
          Utils.setBlockState(block, "peach:stage", 4);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:hamburger_big_bacon_beef") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 2);
          Utils.setBlockState(block, "peach:stage", 4);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:hamburger_big_chicken") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 3);
          Utils.setBlockState(block, "peach:stage", 4);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:hamburger_cheese") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 4);
          Utils.setBlockState(block, "peach:stage", 4);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:hamburger_shrimp") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 5);
          Utils.setBlockState(block, "peach:stage", 4);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:hamburger_vegan") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 6);
          Utils.setBlockState(block, "peach:stage", 4);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:pizza_bacon_and_pineapple") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 7);
          Utils.setBlockState(block, "peach:stage", 1);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:pizza_cheese") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 8);
          Utils.setBlockState(block, "peach:stage", 1);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:pizza_coriander") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 9);
          Utils.setBlockState(block, "peach:stage", 1);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:pizza_onion") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 10);
          Utils.setBlockState(block, "peach:stage", 1);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:pizza_oregano_and_tomatoes") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:plate_item", 11);
          Utils.setBlockState(block, "peach:stage", 1);
          Utils.playSound(block, "use.honey_block");
          return;
        }
      }
    }
  });
  
  init.blockComponentRegistry.registerCustomComponent("peach:bowl_logic", {
    onPlayerInteract: e => {
      const { player, block } = e;
      const selectedItem = Utils.getSelectedItem(player);

      let bowl_item = block.permutation.getState("peach:bowl_item");

      if (bowl_item === 0) {
        if (selectedItem?.typeId === "minecraft:cooked_beef") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:bowl_item", 1);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "minecraft:cooked_chicken") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:bowl_item", 2);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "minecraft:cooked_salmon") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:bowl_item", 3);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "minecraft:cooked_cod") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:bowl_item", 4);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "minecraft:cooked_rabbit") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:bowl_item", 5);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "minecraft:pumpkin_pie") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:bowl_item", 6);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:pie_apple") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:bowl_item", 7);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "peach:pie_lemonlatte") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:bowl_item", 8);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "minecraft:bread") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:bowl_item", 9);
          Utils.playSound(block, "use.cloth");
          return;
        }
        if (selectedItem?.typeId === "minecraft:beetroot_soup") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:bowl");
          Utils.setBlockState(block, "peach:bowl_item", 10);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "minecraft:mushroom_stew") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:bowl");
          Utils.setBlockState(block, "peach:bowl_item", 11);
          Utils.playSound(block, "use.honey_block");
          return;
        }
        if (selectedItem?.typeId === "minecraft:rabbit_stew") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:bowl");
          Utils.setBlockState(block, "peach:bowl_item", 12);
          Utils.playSound(block, "use.honey_block");
          return;
        }
      }
      if (bowl_item === 1) {
        Utils.giveItem(player, "minecraft:cooked_beef");
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 2) {
        Utils.giveItem(player, "minecraft:cooked_chicken");
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 3) {
        Utils.giveItem(player, "minecraft:cooked_salmon");
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 4) {
        Utils.giveItem(player, "minecraft:cooked_cod");
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 5) {
        Utils.giveItem(player, "minecraft:cooked_rabbit");
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 6) {
        Utils.giveItem(player, "minecraft:pumpkin_pie");
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 7) {
        Utils.giveItem(player, "peach:pie_apple");
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 8) {
        Utils.giveItem(player, "peach:pie_lemonlatte");
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 9) {
        Utils.giveItem(player, "minecraft:bread");
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.cloth");
        return;
      }
      if (bowl_item === 10 && selectedItem?.typeId === "minecraft:bowl") {
        Utils.giveItem(player, "minecraft:beetroot_soup");
        Utils.consumeItem(player, selectedItem);
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 11 && selectedItem?.typeId === "minecraft:bowl") {
        Utils.giveItem(player, "minecraft:mushroom_stew");
        Utils.consumeItem(player, selectedItem);
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
      if (bowl_item === 12 && selectedItem?.typeId === "minecraft:bowl") {
        Utils.giveItem(player, "minecraft:rabbit_stew");
        Utils.consumeItem(player, selectedItem);
        Utils.setBlockState(block, "peach:bowl_item", 0);
        Utils.playSound(block, "use.honey_block");
        return;
      }
    }
  });
});