/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.google.common.base.MoreObjects
 *  com.llamalad7.mixinextras.injector.ModifyReturnValue
 *  net.minecraft.class_11659
 *  net.minecraft.class_1268
 *  net.minecraft.class_1306
 *  net.minecraft.class_1657
 *  net.minecraft.class_1799
 *  net.minecraft.class_4587
 *  net.minecraft.class_742
 *  net.minecraft.class_759
 *  org.spongepowered.asm.mixin.Mixin
 *  org.spongepowered.asm.mixin.Shadow
 *  org.spongepowered.asm.mixin.injection.At
 *  org.spongepowered.asm.mixin.injection.At$Shift
 *  org.spongepowered.asm.mixin.injection.Inject
 *  org.spongepowered.asm.mixin.injection.ModifyArg
 *  org.spongepowered.asm.mixin.injection.ModifyVariable
 *  org.spongepowered.asm.mixin.injection.callback.CallbackInfo
 */
package meteordevelopment.meteorclient.mixin;

import com.google.common.base.MoreObjects;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.events.render.ArmRenderEvent;
import meteordevelopment.meteorclient.events.render.HeldItemRendererEvent;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.systems.modules.render.HandView;
import net.minecraft.class_11659;
import net.minecraft.class_1268;
import net.minecraft.class_1306;
import net.minecraft.class_1657;
import net.minecraft.class_1799;
import net.minecraft.class_4587;
import net.minecraft.class_742;
import net.minecraft.class_759;
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.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value={class_759.class})
public abstract class HeldItemRendererMixin {
    @Shadow
    private float field_4043;
    @Shadow
    private float field_4052;
    @Shadow
    private class_1799 field_4047;
    @Shadow
    private class_1799 field_4048;

    @Shadow
    protected abstract boolean method_65910(class_1799 var1, class_1799 var2);

    @ModifyVariable(method={"method_22976(FLnet/minecraft/class_4587;Lnet/minecraft/class_11659;Lnet/minecraft/class_746;I)V"}, at=@At(value="STORE", ordinal=0), index=6)
    private float modifySwing(float swingProgress) {
        HandView module = Modules.get().get(HandView.class);
        class_1268 hand = (class_1268)MoreObjects.firstNonNull((Object)MeteorClient.mc.field_1724.field_6266, (Object)class_1268.field_5808);
        if (module.isActive()) {
            if (hand == class_1268.field_5810 && !MeteorClient.mc.field_1724.method_6079().method_7960()) {
                return swingProgress + module.offSwing.get().floatValue();
            }
            if (hand == class_1268.field_5808 && !MeteorClient.mc.field_1724.method_6047().method_7960()) {
                return swingProgress + module.mainSwing.get().floatValue();
            }
        }
        return swingProgress;
    }

    @ModifyReturnValue(method={"method_65910"}, at={@At(value="RETURN")})
    private boolean modifySkipSwapAnimation(boolean original) {
        return original || Modules.get().get(HandView.class).skipSwapping();
    }

    @ModifyArg(method={"method_3220"}, at=@At(value="INVOKE", target="Lnet/minecraft/class_3532;method_15363(FFF)F", ordinal=2), index=0)
    private float modifyEquipProgressMainhand(float value) {
        float f = MeteorClient.mc.field_1724.method_75194(1.0f);
        float modified = Modules.get().get(HandView.class).oldAnimations() ? 1.0f : f * f * f;
        return (this.method_65910(this.field_4047, MeteorClient.mc.field_1724.method_6047()) ? modified : 0.0f) - this.field_4043;
    }

    @ModifyArg(method={"method_3220"}, at=@At(value="INVOKE", target="Lnet/minecraft/class_3532;method_15363(FFF)F", ordinal=3), index=0)
    private float modifyEquipProgressOffhand(float value) {
        return (float)(this.method_65910(this.field_4048, MeteorClient.mc.field_1724.method_6079()) ? 1 : 0) - this.field_4052;
    }

    @Inject(method={"method_3228"}, at={@At(value="INVOKE", target="Lnet/minecraft/class_759;method_3233(Lnet/minecraft/class_1309;Lnet/minecraft/class_1799;Lnet/minecraft/class_811;Lnet/minecraft/class_4587;Lnet/minecraft/class_11659;I)V", shift=At.Shift.BEFORE)})
    private void onRenderItem(class_742 player, float tickProgress, float pitch, class_1268 hand, float swingProgress, class_1799 item, float equipProgress, class_4587 matrices, class_11659 orderedRenderCommandQueue, int light, CallbackInfo ci) {
        MeteorClient.EVENT_BUS.post((Object)HeldItemRendererEvent.get(hand, matrices));
    }

    @Inject(method={"method_3228"}, at={@At(value="INVOKE", target="Lnet/minecraft/class_759;method_3219(Lnet/minecraft/class_4587;Lnet/minecraft/class_11659;IFFLnet/minecraft/class_1306;)V")})
    private void onRenderArm(class_742 player, float tickProgress, float pitch, class_1268 hand, float swingProgress, class_1799 item, float equipProgress, class_4587 matrices, class_11659 orderedRenderCommandQueue, int light, CallbackInfo ci) {
        MeteorClient.EVENT_BUS.post((Object)ArmRenderEvent.get(hand, matrices));
    }

    @Inject(method={"method_3218"}, at={@At(value="INVOKE", target="Ljava/lang/Math;pow(DD)D", shift=At.Shift.BEFORE)}, cancellable=true)
    private void cancelTransformations(class_4587 matrices, float tickDelta, class_1306 arm, class_1799 stack, class_1657 player, CallbackInfo ci) {
        if (Modules.get().get(HandView.class).disableFoodAnimation()) {
            ci.cancel();
        }
    }
}

