package de.winniepat.citrus.mixins;

import de.winniepat.citrus.gui.TestScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.render.DiffuseLighting;
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 to render the player entity in the TestScreen.
 * This hooks into Screen.render to draw on the active UI frame.
 */
@Mixin(Screen.class)
public class ScreenRenderMixin {

    @Inject(method = "render", at = @At("TAIL"))
    private void citrus$renderPlayerPreview(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
        TestScreen testScreen = TestScreen.getActiveInstance();
        if (testScreen == null || !testScreen.isPreviewBoundsSet()) return;

        MinecraftClient client = MinecraftClient.getInstance();
        if (client.player == null || client.currentScreen == null) return;

        // Ensure UI-friendly lighting for entity rendering.
        DiffuseLighting.enableGuiDepthLighting();

        TestScreen.renderPlayerEntity(
            context,
            testScreen.getPreviewCenterX(),
            testScreen.getPreviewCenterY(),
            testScreen.getPreviewSize(),
            testScreen.getPlayerRotationX(),
            testScreen.getPlayerRotationY(),
            client.player
        );
    }
}
