package info.u_team.u_team_core.gui.renderer;

import java.util.Objects;
import java.util.function.*;

import com.mojang.blaze3d.matrix.MatrixStack;

import info.u_team.u_team_core.util.RGBA;
import net.minecraft.client.gui.*;

public class ScalingTextRenderer implements IRenderable {
	
	protected static final Consumer<ScalingTextRenderer> EMPTY_TEXT_CHANGED = renderer -> {
	};
	
	protected final Supplier<FontRenderer> fontRenderSupplier;
	
	protected Supplier<String> textSupplier;
	
	protected float x;
	protected float y;
	
	private String text;
	private int textWidth;
	
	protected RGBA color;
	protected boolean shadow;
	protected float scale;
	private float positionFactor;
	
	protected Consumer<ScalingTextRenderer> textChanged;
	
	public ScalingTextRenderer(Supplier<FontRenderer> fontRenderSupplier, Supplier<String> textSupplier, float x, float y) {
		this.fontRenderSupplier = fontRenderSupplier;
		this.textSupplier = textSupplier;
		this.x = x;
		this.y = y;
		color = RGBA.WHITE;
		scale = 1;
		positionFactor = 1;
		textChanged = EMPTY_TEXT_CHANGED;
	}
	
	public void setTextSupplier(Supplier<String> textSupplier) {
		Objects.requireNonNull(textSupplier);
		this.textSupplier = textSupplier;
	}
	
	public Supplier<String> getTextSupplier() {
		return textSupplier;
	}
	
	public float getX() {
		return x;
	}
	
	public void setX(float x) {
		this.x = x;
	}
	
	public float getY() {
		return y;
	}
	
	public void setY(float y) {
		this.y = y;
	}
	
	public RGBA getColor() {
		return color;
	}
	
	public void setColor(RGBA color) {
		this.color = color;
	}
	
	public boolean isShadow() {
		return shadow;
	}
	
	public void setShadow(boolean shadow) {
		this.shadow = shadow;
	}
	
	public float getScale() {
		return scale;
	}
	
	public void setScale(float scale) {
		this.scale = scale;
		positionFactor = 1 / scale;
	}
	
	public void setTextChanged(Consumer<ScalingTextRenderer> textChanged) {
		this.textChanged = textChanged;
	}
	
	public float getTextWidth() {
		if (textWidth == 0) { // If text width has never been set
			setText(textSupplier.get());
		}
		return textWidth * scale;
	}
	
	protected void setText(String newText) {
		if ((newText != null && !newText.equals(text)) || newText == null) {
			this.text = newText;
			this.textWidth = fontRenderSupplier.get().getStringWidth(newText);
			updatedText();
		}
	}
	
	protected void updatedText() {
		textChanged.accept(this);
	}
	
	@Override
	public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
		// Get new text and set if has changed
		setText(textSupplier.get());
		renderFont(matrixStack, fontRenderSupplier.get(), x, y);
	}
	
	protected void renderFont(MatrixStack matrixStack, FontRenderer fontRenderer, float x, float y) {
		matrixStack.push();
		matrixStack.scale(scale, scale, 0);
		fontRenderer.renderString(text, x * positionFactor, y * positionFactor, color.getColorARGB(), matrixStack.getLast().getMatrix(), shadow, fontRenderer.getBidiFlag());
		matrixStack.pop();
	}
}

@@ -393,25 +393,16 @@
 d final 
-Supplier%3C
 FontRend
@@ -397,33 +397,32 @@
 nal FontRenderer
-%3E
  fontRenderSuppl
@@ -412,30 +412,24 @@
 r fontRender
-Suppli
 er;%0A%09%0A%09prote
@@ -747,25 +747,16 @@
 enderer(
-Supplier%3C
 FontRend
@@ -759,17 +759,16 @@
 Renderer
-%3E
  fontRen
@@ -770,22 +770,16 @@
 ntRender
-Suppli
 er, Supp
@@ -842,22 +842,16 @@
 ntRender
-Suppli
 er = fon
@@ -857,22 +857,16 @@
 ntRender
-Suppli
 er;%0A%09%09th
@@ -2227,30 +2227,18 @@
 ntRender
-Supplier.get()
+er
 .getStri
@@ -2557,30 +2557,18 @@
 ntRender
-Supplier.get()
+er
 , x, y);

Remove font renderer supplier