package info.u_team.u_team_core.gui.elements;

import com.mojang.blaze3d.matrix.MatrixStack;

import info.u_team.u_team_core.api.gui.*;
import info.u_team.u_team_core.util.RGBA;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.*;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;

public class ScalableTextField extends UTextField implements IScaleable, IScaleProvider {
	
	protected float scale;
	
	public ScalableTextField(FontRenderer fontRenderer, int x, int y, int width, int height, UTextField previousTextField, ITextComponent title, float scale) {
		this(fontRenderer, x, y, width, height, previousTextField, title, scale, EMPTY_TOOLTIP);
	}
	
	public ScalableTextField(FontRenderer fontRenderer, int x, int y, int width, int height, UTextField previousTextField, ITextComponent title, float scale, ITooltip tooltip) {
		super(fontRenderer, x, y, width, height, previousTextField, title, tooltip);
		this.scale = scale;
	}
	
	@Override
	public float getScale() {
		return scale;
	}
	
	@Override
	public void setScale(float scale) {
		this.scale = scale;
	}
	
	@Override
	public void renderForeground(MatrixStack matrixStack, Minecraft minecraft, int mouseX, int mouseY, float partialTicks) {
		final float currentScale = getCurrentScale(matrixStack, mouseX, mouseY, partialTicks);
		
		final float positionFactor = 1 / scale;
		
		matrixStack.push();
		matrixStack.scale(currentScale, currentScale, 0);
		
		final RGBA currentTextColor = getCurrentTextColor(matrixStack, mouseX, mouseY, partialTicks);
		
		final String currentText = fontRenderer.func_238412_a_(text.substring(lineScrollOffset), getAdjustedWidth());
		
		final int cursorOffset = cursorPosition - lineScrollOffset;
		final int selectionOffset = Math.min(selectionEnd - lineScrollOffset, currentText.length());
		
		final boolean isCursorInText = cursorOffset >= 0 && cursorOffset <= currentText.length();
		final boolean shouldCursorBlink = isFocused() && cursorCounter / 6 % 2 == 0 && isCursorInText;
		final boolean isCursorInTheMiddle = cursorPosition < text.length() || text.length() >= maxStringLength;
		
		final int xOffset = (int) ((enableBackgroundDrawing ? x + 4 : x) * positionFactor);
		final int yOffset = (int) ((enableBackgroundDrawing ? y + (int) (height - 8 * scale) / 2 : y) * positionFactor);
		
		int leftRenderedTextX = xOffset;
		
		if (!currentText.isEmpty()) {
			final String firstTextPart = isCursorInText ? currentText.substring(0, cursorOffset) : currentText;
			leftRenderedTextX = fontRenderer.func_238407_a_(matrixStack, textFormatter.apply(firstTextPart, lineScrollOffset), xOffset, yOffset, currentTextColor.getColorARGB());
		}
		
		int rightRenderedTextX = leftRenderedTextX;
		
		if (!isCursorInText) {
			rightRenderedTextX = cursorOffset > 0 ? xOffset + width : xOffset;
		} else if (isCursorInTheMiddle) {
			rightRenderedTextX = leftRenderedTextX - 1;
			--leftRenderedTextX;
		}
		
		if (!currentText.isEmpty() && isCursorInText && cursorOffset < currentText.length()) {
			fontRenderer.func_238407_a_(matrixStack, textFormatter.apply(currentText.substring(cursorOffset), cursorPosition), leftRenderedTextX, yOffset, currentTextColor.getColorARGB());
		}
		
		if (!isCursorInTheMiddle && suggestion != null) {
			fontRenderer.drawStringWithShadow(matrixStack, suggestion, rightRenderedTextX - 1, yOffset, getCurrentSuggestionTextColor(matrixStack, mouseX, mouseY, partialTicks).getColorARGB());
		}
		
		if (shouldCursorBlink) {
			if (isCursorInTheMiddle) {
				AbstractGui.fill(matrixStack, rightRenderedTextX, yOffset - 1, rightRenderedTextX + 1, yOffset + 1 + 9, getCurrentCursorColor(matrixStack, mouseX, mouseY, partialTicks).getColorARGB());
			} else {
				fontRenderer.drawStringWithShadow(matrixStack, "_", rightRenderedTextX, yOffset, currentTextColor.getColorARGB());
			}
		}
		
		if (selectionOffset != cursorOffset) {
			final int selectedX = xOffset + fontRenderer.getStringWidth(currentText.substring(0, selectionOffset));
			drawSelectionBox((int) (rightRenderedTextX * currentScale), (int) ((yOffset - 1) * currentScale), (int) ((selectedX - 1) * currentScale), (int) ((yOffset + 1 + 9) * currentScale));
		}
		
		matrixStack.pop();
	}
	
	@Override
	public float getCurrentScale(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
		return scale;
	}
	
	@Override
	public boolean mouseClicked(double mouseX, double mouseY, int button) {
		if (!visible) {
			return false;
		} else {
			final boolean clicked = clicked(mouseX, mouseY);
			
			if (canLoseFocus) {
				setFocused2(clicked);
			}
			
			if (isFocused() && clicked && button == 0) {
				int clickOffset = MathHelper.floor(mouseX) - x;
				if (enableBackgroundDrawing) {
					clickOffset -= 4;
				}
				
				clickOffset /= scale;
				
				final String currentText = fontRenderer.func_238412_a_(text.substring(lineScrollOffset), getAdjustedWidth());
				setCursorPosition(fontRenderer.func_238412_a_(currentText, clickOffset).length() + lineScrollOffset);
				return true;
			} else {
				return false;
			}
		}
	}
	
}

@@ -4336,24 +4336,133 @@
 ialTicks) %7B%0A
+%09%09return getCurrentScale(mouseX, mouseY);%0A%09%7D%0A%09%0A%09public float getCurrentScale(double mouseX, double mouseY) %7B%0A
 %09%09return sca
@@ -4902,21 +4902,47 @@
 fset /= 
-scale
+getCurrentScale(mouseX, mouseY)
 ;%0A%09%09%09%09%0A%09

Restructure some stuff