/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.llamalad7.mixinextras.sugar.Local
 *  com.llamalad7.mixinextras.sugar.ref.LocalIntRef
 *  net.minecraft.class_2561
 *  net.minecraft.class_310
 *  net.minecraft.class_327
 *  net.minecraft.class_332
 *  net.minecraft.class_3532
 *  net.minecraft.class_355
 *  net.minecraft.class_640
 *  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.Constant
 *  org.spongepowered.asm.mixin.injection.Inject
 *  org.spongepowered.asm.mixin.injection.ModifyArg
 *  org.spongepowered.asm.mixin.injection.ModifyConstant
 *  org.spongepowered.asm.mixin.injection.callback.CallbackInfo
 *  org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
 */
package meteordevelopment.meteorclient.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.ref.LocalIntRef;
import java.util.List;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.systems.modules.render.BetterTab;
import net.minecraft.class_2561;
import net.minecraft.class_310;
import net.minecraft.class_327;
import net.minecraft.class_332;
import net.minecraft.class_3532;
import net.minecraft.class_355;
import net.minecraft.class_640;
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.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(value={class_355.class})
public abstract class PlayerListHudMixin {
    @Shadow
    protected abstract List<class_640> method_48213();

    @ModifyConstant(constant={@Constant(longValue=80L)}, method={"method_48213"})
    private long modifyCount(long count) {
        BetterTab module = Modules.get().get(BetterTab.class);
        return module.isActive() ? (long)module.tabSize.get().intValue() : count;
    }

    @Inject(method={"method_1918"}, at={@At(value="HEAD")}, cancellable=true)
    public void getPlayerName(class_640 playerListEntry, CallbackInfoReturnable<class_2561> info) {
        BetterTab betterTab = Modules.get().get(BetterTab.class);
        if (betterTab.isActive()) {
            info.setReturnValue((Object)betterTab.getPlayerName(playerListEntry));
        }
    }

    @ModifyArg(method={"method_1919"}, at=@At(value="INVOKE", target="Ljava/lang/Math;min(II)I"), index=0)
    private int modifyWidth(int width) {
        BetterTab module = Modules.get().get(BetterTab.class);
        return module.isActive() && module.accurateLatency.get() != false ? width + 30 : width;
    }

    @Inject(method={"method_1919"}, at={@At(value="INVOKE", target="Ljava/lang/Math;min(II)I", shift=At.Shift.BEFORE)})
    private void modifyHeight(CallbackInfo ci, @Local(ordinal=5) LocalIntRef o, @Local(ordinal=6) LocalIntRef p) {
        int newO;
        BetterTab module = Modules.get().get(BetterTab.class);
        if (!module.isActive()) {
            return;
        }
        int newP = 1;
        int totalPlayers = newO = this.method_48213().size();
        while (newO > module.tabHeight.get()) {
            newO = (totalPlayers + ++newP - 1) / newP;
        }
        o.set(newO);
        p.set(newP);
    }

    @Inject(method={"method_1923"}, at={@At(value="HEAD")}, cancellable=true)
    private void onRenderLatencyIcon(class_332 context, int width, int x, int y, class_640 entry, CallbackInfo ci) {
        BetterTab betterTab = Modules.get().get(BetterTab.class);
        if (betterTab.isActive() && betterTab.accurateLatency.get().booleanValue()) {
            class_310 mc = class_310.method_1551();
            class_327 textRenderer = mc.field_1772;
            int latency = class_3532.method_15340((int)entry.method_2959(), (int)0, (int)9999);
            int color = latency < 150 ? -16717456 : (latency < 300 ? -1585120 : -2670024);
            String text = latency + "ms";
            context.method_25303(textRenderer, text, x + width - textRenderer.method_1727(text), y, color);
            ci.cancel();
        }
    }
}

