package xyz.illuc.neuralgiacore.mixin.PrototypePain;

import net.adinvas.prototype_pain.limbs.Limb;
import net.adinvas.prototype_pain.limbs.PlayerHealthData;
import net.minecraft.server.level.ServerPlayer;
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.CallbackInfo;
import xyz.illuc.neuralgiacore.interfaces.IPlayerHealthDataAccessor;

@Mixin(PlayerHealthData.class)
public abstract class MixinSepsis {
    @Shadow
    public abstract float getLimbInfection(Limb limb);

    @Shadow public abstract float getOxygenCap();

    @Shadow public abstract void setOxygenCap(float value);

    //https://scavprototype.wiki.gg/wiki/Infection
    @Inject(method = "tickUpdate", at = @At(value = "INVOKE", target = "Ljava/lang/Math;max(FF)F", ordinal = 5, shift = At.Shift.AFTER), remap = false)
    private void updateSepsis(ServerPlayer player, CallbackInfo ci) {

        float totalInfection = this.getLimbInfection(Limb.HEAD) +
                this.getLimbInfection(Limb.CHEST) +
                this.getLimbInfection(Limb.LEFT_ARM) +
                this.getLimbInfection(Limb.LEFT_FOOT) +
                this.getLimbInfection(Limb.LEFT_LEG) +
                this.getLimbInfection(Limb.LEFT_HAND) +
                this.getLimbInfection(Limb.RIGHT_ARM) +
                this.getLimbInfection(Limb.RIGHT_FOOT) +
                this.getLimbInfection(Limb.RIGHT_HAND) +
                this.getLimbInfection(Limb.RIGHT_LEG);

        IPlayerHealthDataAccessor accessor = (IPlayerHealthDataAccessor) this;
        if (totalInfection > 100) {
            accessor.setSepsis((float) Math.max(0, Math.min(100, (accessor.getSepsis() + (0.028*totalInfection/100/20)))));
        } else {
            accessor.setSepsis((float) Math.max(0, Math.min(100, (accessor.getSepsis() - 0.07/20))));
        }

        this.setOxygenCap((float) (this.getOxygenCap() - accessor.getSepsis()*0.72));
    }
}
