package xyz.illuc.neuralgiacore.mixin.Tfc;

import com.mojang.blaze3d.vertex.PoseStack;
import net.adinvas.prototype_pain.PlayerHealthProvider;
import net.dries007.tfc.client.IngameOverlays;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.client.gui.overlay.ForgeGui;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import static net.dries007.tfc.client.IngameOverlays.setup;
import static net.minecraft.util.Mth.clamp;

@Mixin(IngameOverlays.class)
public class MixinHealthBar {
    @Redirect(
            method = "renderHealth",
            at = @At(value = "INVOKE", target = "Lnet/dries007/tfc/client/IngameOverlays;renderHealthBar(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraftforge/client/gui/overlay/ForgeGui;Lnet/minecraft/client/gui/GuiGraphics;II)V"),
            remap = false
    )
    private static void renderHealth(LivingEntity player, ForgeGui gui, GuiGraphics graphics, int width, int height) {
        tfcpain$renderConscioussnes(gui, graphics, width, height);
        tfcpain$renderOxygen(gui, graphics, width, height);

    }
    @Unique
    private static final ResourceLocation TEXTURE = new ResourceLocation("tfcpain", "textures/overlay.png");

    @Unique
    private static void tfcpain$renderConscioussnes(ForgeGui gui, GuiGraphics graphics, int width, int height) {
        PoseStack stack = graphics.pose();
        Minecraft minecraft = Minecraft.getInstance();
        if ( gui.shouldDrawSurvivalElements() && setup(gui, minecraft)) {
            Player player = (Player)minecraft.getCameraEntity();

            assert player != null;


            player.getCapability(PlayerHealthProvider.PLAYER_HEALTH_DATA, null).ifPresent(cap-> {
                var fill = cap.getContiousness()/100;
                fill = clamp(fill, 0.0F, 1.0F);

                int centerX = width / 2;
                int y = height - gui.leftHeight;

                stack.pushPose();
                stack.translate(centerX - 91, y+4, 0.0F);

                graphics.blit(TEXTURE, 0, 0, 0, 20, 90, 5);
                graphics.blit(TEXTURE, 0, 0, 0, 25, (int)(90.0F * fill), 5);

                stack.popPose();

                gui.leftHeight += 10;
            });

        }

    }

    @Unique
    private static void tfcpain$renderOxygen(ForgeGui gui, GuiGraphics graphics, int width, int height) {
        PoseStack stack = graphics.pose();
        Minecraft minecraft = Minecraft.getInstance();
        if ( gui.shouldDrawSurvivalElements() && setup(gui, minecraft)) {
            Player player = (Player)minecraft.getCameraEntity();

            assert player != null;




            player.getCapability(PlayerHealthProvider.PLAYER_HEALTH_DATA, null).ifPresent(cap-> {
                var fill = cap.getOxygen()/100;
                fill = clamp(fill, 0.0F, 1.0F);

                int centerX = width / 2;
                int y = height - gui.leftHeight;

                stack.pushPose();
                stack.translate(centerX - 91, y+8, 0.0F);

                graphics.blit(TEXTURE, 0, 0, 90, 20, 90, 5);
                graphics.blit(TEXTURE, 0, 0, 90, 25, (int)(90.0F * fill), 5);

                stack.popPose();

                gui.leftHeight += 10;
            });

        }

    }
}

