package blade.addon.mixin;

import blade.addon.features.other.SwingAnimation;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.FirstPersonHandsAndItemsRenderer;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.HumanoidArm;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
 * 26.3 split the old net.minecraft.client.renderer.ItemInHandRenderer: the arm/item transforms
 * now live in net.minecraft.client.renderer.FirstPersonHandsAndItemsRenderer.
 */
@Mixin(FirstPersonHandsAndItemsRenderer.class)
public class FirstPersonHandsAndItemsRendererMixin {

    @Inject(method = "applyItemArmTransform", at = @At("HEAD"), cancellable = true)
    private void applyEquipOffset(PoseStack matrices, HumanoidArm arm, float swingProgress, CallbackInfo ci) {
        if (!SwingAnimation.shouldIgnore()) return;
        int side = arm == HumanoidArm.RIGHT ? 1 : -1;
        matrices.translate((float) side * 0.56F, -0.52F, -0.72F);
        ci.cancel();
    }

    @Inject(method = "swingArm", at = @At("HEAD"), cancellable = true)
    private void swingArm(float swingProgress, PoseStack matrixStack, int i, HumanoidArm arm, CallbackInfo ci) {
        if (!SwingAnimation.shouldIgnore()) return;
        float f = -0.4F * Mth.sin(Mth.sqrt(1) * (float) Math.PI);
        float g = 0.2F * Mth.sin(Mth.sqrt(1) * (float) (Math.PI * 2));
        float h = -0.2F * Mth.sin(1 * (float) Math.PI);
        matrixStack.translate(i * f, g, h);
        ci.cancel();
    }
}