package info.u_team.u_team_core.gui.elements;

import com.mojang.blaze3d.matrix.MatrixStack;

import info.u_team.u_team_core.util.*;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.util.text.*;

/**
 * A button that fixes vanilla not drawing the continuous border if the button is smaller than 20. Also adds utility
 * methods to add an {@link IPressable} and {@link ITooltip}
 * 
 * @author HyCraftHD
 */
public class UButton extends Button {
	
	protected static IPressable EMTPY_PRESSABLE = button -> {
	};
	
	protected static ITooltip EMPTY_TOOLTIP = field_238486_s_;
	
	protected RGBA buttonColor;
	
	public UButton(int x, int y, int width, int height, ITextComponent display) {
		this(x, y, width, height, display, EMTPY_PRESSABLE);
	}
	
	public UButton(int x, int y, int width, int height, ITextComponent display, IPressable pessable) {
		this(x, y, width, height, display, pessable, EMPTY_TOOLTIP);
	}
	
	public UButton(int x, int y, int width, int height, ITextComponent display, ITooltip tooltip) {
		this(x, y, width, height, display, EMTPY_PRESSABLE, tooltip);
	}
	
	public UButton(int x, int y, int width, int height, ITextComponent display, IPressable pessable, ITooltip tooltip) {
		super(x, y, width, height, display, pessable);
		buttonColor = RGBA.WHITE;
	}
	
	public void setPressable(IPressable pressable) {
		onPress = pressable;
	}
	
	public void setPressable(Runnable runnable) {
		onPress = button -> runnable.run();
	}
	
	public void setTooltip(ITooltip tooltip) {
		onTooltip = tooltip;
	}
	
	public RGBA getButtonColor() {
		return buttonColor;
	}
	
	public void setButtonColor(RGBA buttonColor) {
		this.buttonColor = buttonColor;
	}
	
	protected RGBA getCurrentButtonColor(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
		return buttonColor;
	}
	
	@Override
	public void renderButton(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
		final Minecraft minecraft = Minecraft.getInstance();
		final FontRenderer fontRenderer = minecraft.fontRenderer;
		
		ITextComponent message = getMessage();
		
		GuiUtil.drawContinuousTexturedBox(matrixStack, WIDGETS_LOCATION, x, y, 0, 46 + getYImage(isHovered()) * 20, width, height, 200, 20, 2, 3, 2, 2, 0, getCurrentButtonColor(matrixStack, mouseY, mouseY, partialTicks));
		
		renderBg(matrixStack, minecraft, mouseX, mouseY);
		
		final int messageWidth = fontRenderer.getStringPropertyWidth(message);
		final int ellipsisWidth = fontRenderer.getStringWidth("...");
		
		if (messageWidth > width - 6 && messageWidth > ellipsisWidth) {
			message = new StringTextComponent(fontRenderer.func_238417_a_(message, width - 6 - ellipsisWidth).getString() + "...");
		}
		
		final float xStart = (x + (width / 2) - messageWidth / 2);
		final float yStart = (y + ((int) (height - 8)) / 2);
		
		fontRenderer.func_243246_a(matrixStack, message, xStart, yStart, getFGColor());
	}
}

@@ -2134,52 +2134,8 @@
 %0A%09%09%0A
-%09%09ITextComponent message = getMessage();%0A%09%09%0A
 %09%09Gu
@@ -2400,24 +2400,112 @@
 mouseY);%0A%09%09%0A
+%09%09ITextComponent message = getMessage();%0A%09%09if (message != StringTextComponent.EMPTY) %7B%0A%09
 %09%09final int 
@@ -2565,16 +2565,17 @@
 ssage);%0A
+%09
 %09%09final 
@@ -2632,17 +2632,19 @@
 ..%22);%0A%09%09
-%0A
+%09%0A%09
 %09%09if (me
@@ -2701,16 +2701,17 @@
 idth) %7B%0A
+%09
 %09%09%09messa
@@ -2829,15 +2829,18 @@
 %22);%0A
+%09
 %09%09%7D%0A%09%09
-%0A
+%09%0A%09
 %09%09fi
@@ -2894,24 +2894,25 @@
 dth / 2);%0A%09%09
+%09
 final float 
@@ -2954,17 +2954,19 @@
 / 2);%0A%09%09
-%0A
+%09%0A%09
 %09%09fontRe
@@ -3039,13 +3039,17 @@
 lor());%0A
+%09%09%7D%0A
 %09%7D%0A%7D%0A

Only try to render the text if the text is not empty