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.block.state.properties.Property;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.BlockState;
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.Direction;
import net.minecraft.core.BlockPos;

public class NumberToItemNBTCodeBlockUpdateTickProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z) {
		ItemStack itemSlot = ItemStack.EMPTY;
		Direction blockDirection = Direction.NORTH;
		blockDirection = new Object() {
			public Direction getDirection(BlockPos pos) {
				BlockState _bs = world.getBlockState(pos);
				Property<?> property = _bs.getBlock().getStateDefinition().getProperty("facing");
				if (property != null && _bs.getValue(property) instanceof Direction _dir)
					return _dir;
				else if (_bs.hasProperty(BlockStateProperties.AXIS))
					return Direction.fromAxisAndDirection(_bs.getValue(BlockStateProperties.AXIS), Direction.AxisDirection.POSITIVE);
				else if (_bs.hasProperty(BlockStateProperties.HORIZONTAL_AXIS))
					return Direction.fromAxisAndDirection(_bs.getValue(BlockStateProperties.HORIZONTAL_AXIS), Direction.AxisDirection.POSITIVE);
				return Direction.NORTH;
			}
		}.getDirection(BlockPos.containing(x, y, z));
		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(GetXOffsetSystemProcedure.execute(x, blockDirection, "Front"), GetYOffsetSystemProcedure.execute(y, blockDirection, "Front"), GetZOffsetSystemProcedure.execute(z, blockDirection, "Front")), 0))
				.copy();
		{
			final String _tagName = "numberCodeItem";
			final double _tagValue = GetNBTNumberSystemProcedure.execute(world, GetXOffsetSystemProcedure.execute(x, blockDirection, "Back"), GetYOffsetSystemProcedure.execute(y, blockDirection, "Back"),
					GetZOffsetSystemProcedure.execute(z, blockDirection, "Back"), "numberCodeBlock");
			CustomData.update(DataComponents.CUSTOM_DATA, itemSlot, tag -> tag.putDouble(_tagName, _tagValue));
		}
		if (world instanceof ILevelExtension _ext && _ext.getCapability(Capabilities.ItemHandler.BLOCK,
				BlockPos.containing(GetXOffsetSystemProcedure.execute(x, blockDirection, "Back"), GetYOffsetSystemProcedure.execute(y, blockDirection, "Back"), GetZOffsetSystemProcedure.execute(z, blockDirection, "Back")),
				null) instanceof IItemHandlerModifiable _itemHandlerModifiable) {
			ItemStack _setstack = itemSlot.copy();
			_setstack.setCount(itemSlot.getCount());
			_itemHandlerModifiable.setStackInSlot(0, _setstack);
		}
	}
}
