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;
		
		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);
		
		ITextComponent message = getMessage();
		if (message != StringTextComponent.EMPTY) {
			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());
		}
	}
}

@@ -1773,44 +1773,43 @@
 %0A%09%0A%09
-protected RGBA getCurrent
+@Override%0A%09public void render
 Button
-Color
 (Mat
@@ -1882,82 +1882,177 @@
 %7B%0A%09%09
-return buttonColor;%0A%09%7D%0A%09%0A%09@Override%0A%09public void renderButton(MatrixStack 
+GuiUtil.drawContinuousTexturedBox(matrixStack, WIDGETS_LOCATION, x, y, 0, 46 + getYImage(isHovered()) * 20, width, height, 200, 20, 2, 3, 2, 2, 0, getCurrentButtonColor(
 matr
@@ -2063,32 +2063,24 @@
 ack,
- int
  mouse
-X, int
+Y,
  mouseY,
  flo
@@ -2067,38 +2067,32 @@
  mouseY, mouseY,
- float
  partialTicks) %7B
@@ -2089,18 +2089,21 @@
 alTicks)
- %7B
+);%0A%09%09
 %0A%09%09final
@@ -2156,229 +2156,281 @@
 ;%0A%09%09
-final FontRenderer fontRenderer = minecraft.fontRenderer;%0A%09%09%0A%09%09GuiUtil.drawContinuousTexturedBox(matrixStack, WIDGETS_LOCATION, x, y, 0, 46 + getYImage(isHovered()) * 20, width, height, 200, 20, 2, 3, 2, 2, 0, g
+%0A%09%09renderBackground(matrixStack, minecraft, mouseX, mouseY, partialTicks);%0A%09%09renderForeground(matrixStack, minecraft, mouseX, mouseY, partialTicks);%0A%09%7D%0A%09%0A%09protected RGBA getCurrentButtonColor(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) %7B%0A%09%09r
 et
-C
 ur
-rentB
+n b
 utto
@@ -2439,37 +2439,117 @@
 olor
-(matrixStack,
+;%0A%09%7D%0A%09%0A%09protected void renderBackground(MatrixStack matrixStack, Minecraft minecraft, int
  mouse
-Y,
+X, int
  mouseY,
  par
@@ -2536,32 +2536,38 @@
 seX, int mouseY,
+ float
  partialTicks));
@@ -2568,25 +2568,113 @@
 cks)
+ %7B%0A%09%09renderBg(matrixStack, minecraft, mouseX, mouseY
 );%0A%09
+%7D%0A
 %09%0A%09
-%09renderBg(
+protected void renderForeground(MatrixStack 
 matr
@@ -2673,32 +2673,42 @@
 ack matrixStack,
+ Minecraft
  minecraft, mous
@@ -2698,32 +2698,36 @@
 t minecraft,
+ int
  mouseX,
  mouseY);%0A%09%09
@@ -2714,24 +2714,109 @@
  mouseX,
+ int
  mouseY
-)
+, float partialTicks) %7B%0A%09%09final FontRenderer fontRenderer = minecraft.fontRenderer
 ;%0A%09%09%0A%09%09I

Split the logic of ubutton apart