package info.u_team.u_team_core.util;

import java.util.List;

import com.mojang.blaze3d.vertex.PoseStack;

import info.u_team.u_team_core.api.gui.BackgroundColorProvider;
import info.u_team.u_team_core.api.gui.PerspectiveRenderable;
import info.u_team.u_team_core.api.gui.ScaleProvider;
import info.u_team.u_team_core.api.gui.TextProvider;
import info.u_team.u_team_core.api.gui.TextureProvider;
import info.u_team.u_team_core.api.gui.TooltipRenderable;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Widget;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.util.Mth;

public class WidgetUtil {
	
	public static boolean isHovered(AbstractWidget widget) {
		return widget.isHovered;
	}
	
	public static <T extends AbstractWidget & PerspectiveRenderable & BackgroundColorProvider> void renderButtonLikeWidget(T widget, TextureProvider textureProvider, PoseStack poseStack, int mouseX, int mouseY, float partialTicks) {
		RenderUtil.drawContinuousTexturedBox(poseStack, widget.x, widget.y, textureProvider.getU(), textureProvider.getV(), widget.width, widget.height, textureProvider.getWidth(), textureProvider.getHeight(), 2, 3, 2, 2, widget.getBlitOffset(), textureProvider.getTexture(), widget.getCurrentBackgroundColor(poseStack, mouseY, mouseY, partialTicks));
		
		widget.renderBackground(poseStack, mouseX, mouseY, partialTicks);
		widget.renderForeground(poseStack, mouseX, mouseY, partialTicks);
	}
	
	public static <T extends AbstractWidget & TextProvider> void renderText(T widget, PoseStack poseStack, int mouseX, int mouseY, float partialTicks) {
		final var minecraft = Minecraft.getInstance(); // TODO replace with font provider
		final var fontRenderer = minecraft.font;
		
		var message = widget.getCurrentText();
		if (message != TextComponent.EMPTY) {
			final var messageWidth = fontRenderer.width(message);
			final var ellipsisWidth = fontRenderer.width("...");
			
			if (messageWidth > widget.width - 6 && messageWidth > ellipsisWidth) {
				message = new TextComponent(fontRenderer.substrByWidth(message, widget.width - 6 - ellipsisWidth).getString() + "...");
			}
			
			final float xStart = (widget.x + (widget.width / 2) - messageWidth / 2);
			final float yStart = (widget.y + (widget.height - 8) / 2);
			
			fontRenderer.drawShadow(poseStack, message, xStart, yStart, widget.getCurrentTextColor(poseStack, mouseX, mouseY, partialTicks).getColorARGB());
		}
	}
	
	public static <T extends AbstractWidget & TextProvider & ScaleProvider> void renderScaledText(T widget, PoseStack poseStack, int mouseX, int mouseY, float partialTicks) {
		final var scale = widget.getCurrentScale(poseStack, mouseX, mouseY, partialTicks);
		
		if (scale == 1) {
			renderText(widget, poseStack, mouseX, mouseY, partialTicks);
		} else {
			final var minecraft = Minecraft.getInstance(); // TODO replace with font provider
			final var fontRenderer = minecraft.font;
			
			var message = widget.getCurrentText();
			if (message != TextComponent.EMPTY) {
				final var messageWidth = Mth.ceil(scale * fontRenderer.width(message));
				final var ellipsisWidth = Mth.ceil(scale * fontRenderer.width("..."));
				
				if (messageWidth > widget.width - 6 && messageWidth > ellipsisWidth) {
					message = new TextComponent(fontRenderer.substrByWidth(message, widget.width - 6 - ellipsisWidth).getString() + "...");
				}
				
				final var positionFactor = 1 / scale;
				
				final var xStart = (widget.x + (widget.width / 2) - messageWidth / 2) * positionFactor;
				final var yStart = (widget.y + ((int) (widget.height - 8 * scale)) / 2) * positionFactor;
				
				poseStack.pushPose();
				poseStack.scale(scale, scale, 0);
				fontRenderer.drawShadow(poseStack, message, xStart, yStart, widget.getCurrentTextColor(poseStack, mouseX, mouseY, partialTicks).getColorARGB());
				poseStack.popPose();
			}
		}
	}
	
	public static void renderTooltips(List<Widget> widgets, PoseStack poseStack, int mouseX, int mouseY, float partialTicks) {
		widgets.forEach(widget -> {
			if (widget instanceof TooltipRenderable tooltipRenderable) {
				tooltipRenderable.renderToolTip(poseStack, mouseX, mouseY, partialTicks);
			}
		});
	}
}

@@ -452,47 +452,8 @@
 le;%0A
-import net.minecraft.client.Minecraft;%0A
 impo
@@ -1643,121 +1643,42 @@
 var 
-minecraft = Minecraft.getInstance(); // TODO replace with font provider%0A%09%09final var fontRenderer = minecraft.f
+font = widget.getCurrentTextF
 ont
+()
 ;%0A%09%09
@@ -1787,32 +1787,24 @@
 Width = font
-Renderer
 .width(messa
@@ -1837,32 +1837,24 @@
 Width = font
-Renderer
 .width(%22...%22
@@ -1966,32 +1966,24 @@
 mponent(font
-Renderer
 .substrByWid
@@ -2204,32 +2204,24 @@
 %0A%09%09%09%0A%09%09%09font
-Renderer
 .drawShadow(
@@ -2722,122 +2722,42 @@
 var 
-minecraft = Minecraft.getInstance(); // TODO replace with font provider%0A%09%09%09final var fontRenderer = minecraft.f
+font = widget.getCurrentTextF
 ont
+()
 ;%0A%09%09
@@ -2887,32 +2887,24 @@
 scale * font
-Renderer
 .width(messa
@@ -2960,24 +2960,16 @@
 e * font
-Renderer
 .width(%22
@@ -3093,24 +3093,16 @@
 ent(font
-Renderer
 .substrB
@@ -3498,16 +3498,8 @@
 font
-Renderer
 .dra

Use getFont