package net.mcreator.puzzle_code.procedures;

import net.neoforged.neoforge.items.IItemHandlerModifiable;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.common.extensions.ILevelExtension;
import net.neoforged.neoforge.capabilities.Capabilities;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.BlockPos;

public class ItemAreaGUIButtonSwapProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z) {
		double temp = 0;
		ItemStack itemSlot = ItemStack.EMPTY;
		itemSlot = (new Object() {
			public ItemStack getItemStack(LevelAccessor world, BlockPos pos, int slotid) {
				if (world instanceof ILevelExtension _ext) {
					IItemHandler _itemHandler = _ext.getCapability(Capabilities.ItemHandler.BLOCK, pos, null);
					if (_itemHandler != null)
						return _itemHandler.getStackInSlot(slotid).copy();
				}
				return ItemStack.EMPTY;
			}
		}.getItemStack(world, BlockPos.containing(x, y, z), 0)).copy();
		temp = itemSlot.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getDouble((itemSlot.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getString("currentSelectedAxis") + "Pos"));
		{
			final String _tagName = (itemSlot.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getString("currentSelectedAxis") + "Pos");
			final double _tagValue = (itemSlot.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getDouble((itemSlot.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getString("currentSelectedAxis") + "Pos2")));
			CustomData.update(DataComponents.CUSTOM_DATA, itemSlot, tag -> tag.putDouble(_tagName, _tagValue));
		}
		{
			final String _tagName = (itemSlot.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getString("currentSelectedAxis") + "Pos2");
			final double _tagValue = temp;
			CustomData.update(DataComponents.CUSTOM_DATA, itemSlot, tag -> tag.putDouble(_tagName, _tagValue));
		}
		if (world instanceof ILevelExtension _ext && _ext.getCapability(Capabilities.ItemHandler.BLOCK, BlockPos.containing(x, y, z), null) instanceof IItemHandlerModifiable _itemHandlerModifiable) {
			ItemStack _setstack = itemSlot.copy();
			_setstack.setCount(itemSlot.getCount());
			_itemHandlerModifiable.setStackInSlot(0, _setstack);
		}
	}
}
