package ru.dbotthepony.mc.otm.mixin;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.dbotthepony.mc.otm.item.tool.RedstoneInteractorItem;

@Mixin(BlockBehaviour.BlockStateBase.class)
public abstract class BlockStateBaseMixin {
	@Shadow
	protected abstract BlockState asState();

	@Inject(
		method = "getSignal(Lnet/minecraft/world/level/BlockGetter;Lnet/minecraft/core/BlockPos;Lnet/minecraft/core/Direction;)I",
		at = @At("HEAD"),
		cancellable = true,
		remap = false
	)
	public void getSignal(BlockGetter p_60747_, BlockPos p_60748_, Direction p_60749_, CallbackInfoReturnable<Integer> info) {
		if (p_60747_ instanceof Level level) {
			int hookResult = RedstoneInteractorItem.Companion.getSignalHook(level, this.asState(), p_60748_, p_60749_);

			if (hookResult != -1) {
				info.setReturnValue(hookResult);
			}
		}
	}
}
