package aeronicamc.mods.mxtune.gui.widget;

import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.Style;
import net.minecraftforge.common.ForgeHooks;

public class MXLink extends MXButton
{
    private AlignText alignText;
    private String url;

    public enum AlignText
    {
        LEFT, CENTER, RIGHT
    }

    public MXLink(IPressable pOnPress)
    {
        super(pOnPress);
        this.alignText = AlignText.LEFT;
        this.url = "";
    }

    public AlignText getAlignText()
    {
        return alignText;
    }

    public void setAlignText(AlignText alignText)
    {
        this.alignText = alignText;
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    @Override
    public void renderButton(MatrixStack mStack, int mouseX, int mouseY, float partial)
    {
        if (this.visible)
        {
            Minecraft mc = Minecraft.getInstance();
            FontRenderer font = mc.font;
            ITextComponent formattedLink = ForgeHooks.newChatWithLinks(this.url, false);
            int stringWidth = Math.min(font.width(formattedLink.getVisualOrderText()), width);
            int alignX = this.x;
            switch (this.alignText)
            {
                case RIGHT:
                    alignX = this.x - stringWidth;
                    break;
                case CENTER:
                    alignX = this.x - stringWidth + stringWidth / 2;
                    break;
                case LEFT:
                    break;
                default:
            }
            String displayLink = font.plainSubstrByWidth(formattedLink.getString(), width);
            this.fillGradient(mStack,alignX - 2 , y - 2, alignX + width + 2, y +  height, 0xCC000000, 0xCC000000);
            drawString(mStack, font, displayLink, alignX, this.y, 0xFF8080);
        }
    }

    /**
     * Call this method from the parent class that extends GuiScreen e.g. {@link Screen#handleComponentClicked}
     * @return {@link Style} for the URL
     */
    public Style getLinkComponent()
    {
        return ForgeHooks.newChatWithLinks(this.url, false).getSiblings().get(0).getStyle();
    }
}

@@ -295,19 +295,118 @@
 .text.St
-yle
+ringTextComponent;%0Aimport net.minecraft.util.text.Style;%0Aimport net.minecraft.util.text.TextFormatting
 ;%0Aimport
@@ -1616,32 +1616,45 @@
 alignX = this.x 
++ this.width 
 - stringWidth;%0A 
@@ -1652,16 +1652,16 @@
 gWidth;%0A
-
         
@@ -1748,24 +1748,28 @@
 s.x 
-- stringWidth + 
++ (this.width)/2 - (
 stri
@@ -1779,12 +1779,11 @@
 idth
- / 2
+/2)
 ;%0A  
@@ -1896,24 +1896,25 @@
           %7D%0A
+%0A
             
@@ -1985,32 +1985,171 @@
 ring(), width);%0A
+            ITextComponent link = new StringTextComponent(displayLink).withStyle(TextFormatting.UNDERLINE).withStyle(TextFormatting.AQUA);%0A
             this
@@ -2198,17 +2198,23 @@
 lignX + 
-w
+stringW
 idth + 2
@@ -2226,26 +2226,26 @@
   height, 0x
-CC
+40
 000000, 0xCC
@@ -2246,10 +2246,10 @@
 , 0x
-CC
+40
 0000
@@ -2290,24 +2290,17 @@
 , font, 
-displayL
+l
 ink, ali
@@ -2316,16 +2316,10 @@
 .y, 
-0xFF8080
+-1
 );%0A 

Fix text alignment. Adjust text color and style.