package dev.photosync.platform.mixin;

import com.mojang.blaze3d.platform.Window;
import dev.photosync.platform.PhotoSyncMod;
import dev.photosync.platform.impl.RenderAdapter;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.Hud;
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;

/**
 * Draws the corner notifications over the HUD.
 *
 * <p>At TAIL so they sit above the hotbar and chat rather than under them. This
 * is the in-world HUD, so notifications are invisible on the title screen --
 * acceptable, since the events that raise them all happen in a world.
 *
 * <p>1.21 replaced the loose partial-tick float with {@link DeltaTracker}. The
 * bridge still wants the float, and {@code false} asks for the real one rather
 * than the frozen-while-paused one, so notifications keep animating while the
 * game is paused behind our own screen.
 *
 * <p>26.1 split {@code Gui} in two and the in-world overlay moved to {@code Hud},
 * where {@code render} became {@code extractRenderState}. {@code Gui} kept a
 * method by that name -- it is the outer one that also draws the current screen,
 * and injecting there would put notifications underneath our own GUI.
 */
@Mixin(Hud.class)
public class HudMixin {

    @Inject(method = "extractRenderState", at = @At("TAIL"))
    private void photosync$renderNotifications(GuiGraphicsExtractor graphics, DeltaTracker delta, CallbackInfo callback) {
        Window window = Minecraft.getInstance().getWindow();
        PhotoSyncMod.client().ifPresent(client -> client.renderHud(new RenderAdapter(
                graphics,
                window.getGuiScaledWidth(),
                window.getGuiScaledHeight(),
                delta.getGameTimeDeltaPartialTick(false))));
    }
}
