package dev.micle.loginprotection.mixin;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.micle.loginprotection.data.ProtectedPlayer;
import dev.micle.loginprotection.proxy.Proxy;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.gui.ForgeIngameGui;
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;

@Mixin(ForgeIngameGui.class)
@OnlyIn(Dist.CLIENT)
public class GuiRenderTickMixin {
    @Inject(method = "render", at = @At(value = "HEAD"))
    private void onClientTick(PoseStack poseStack, float partialTicks, CallbackInfo callbackInfo) {
        // Setup
        poseStack.pushPose();
        poseStack.translate(Minecraft.getInstance().getWindow().getGuiScaledWidth() / 2.0,
                Minecraft.getInstance().getWindow().getGuiScaledHeight() / 2.0, 0);
        RenderSystem.enableBlend();
        RenderSystem.defaultBlendFunc();

        // Render player state text
        poseStack.pushPose();
        poseStack.scale(5, 5, 5);

        // Check if we should draw the state
        if (Proxy.Client.getPlayerState() != null && !Proxy.Client.getPlayerState().equals(ProtectedPlayer.State.ACTIVE)) {
            // Initialize variables
            Font font = Minecraft.getInstance().font;
            float offsetX = -(font.width(Proxy.Client.getPlayerState().toString()) / 2f);
            float offsetY = -((font.lineHeight / 2f) + ((Minecraft.getInstance().getWindow().getGuiScaledHeight() / 4f) / 5));
            int argb = 0xFFFFFFFF;

            // Draw the player's protection state
            font.drawShadow(poseStack, Proxy.Client.getPlayerState().toString().replace("_", " "), offsetX, offsetY, argb);
        }
        poseStack.popPose();

        // Finish
        RenderSystem.disableBlend();
        poseStack.popPose();
    }
}
