package me.deftware.client.framework.wrappers.gui;

import me.deftware.mixin.imp.IMixinGuiButton;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.widget.AbstractButtonWidget;

public class IGuiButton extends AbstractButtonWidget implements CustomIGuiEventListener {

    public IGuiButton(int id, int x, int y, String buttonText) {
        super(x, y, 200, 20, buttonText);
    }

    public IGuiButton(int id, int x, int y, int widthIn, int heightIn, String buttonText) {
        super(x, y, widthIn, heightIn, buttonText);
    }

    @Override
    public void render(int mouseX, int mouseY, float float_1) {
        if (onDraw(mouseX, mouseY) == 0) {
            super.render(mouseX, mouseY, float_1);
        }
    }

    public void drawCenteredString(String text, int x, int y, int color) {
        MinecraftClient.getInstance().textRenderer.drawWithShadow(text, x - MinecraftClient.getInstance().textRenderer.getStringWidth(text) / 2f, y, color);
    }

    @Override
    public boolean mouseClicked(double double_1, double double_2, int int_1) {
        if (this.active) {
            if (int_1 == 0) {
                boolean boolean_1 = this.isButtonHovered();
                if (boolean_1) {
                    this.playDownSound(MinecraftClient.getInstance().getSoundManager());
                    onButtonClick(x, y);
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }

    public void onButtonClick(double mouseX, double mouseY) { }

    protected int onDraw(int mouseX, int mouseY) {
        return 0;
    }

    protected boolean isVisible() {
        return visible;
    }

    public void setEnabled(boolean state) {
        active = state;
    }

    public void setVisible(boolean state) {
        visible = state;
    }

    public boolean isEnabled() {
        return active;
    }

    protected int getButtonX() {
        return x;
    }

    protected void setButtonX(int x) {
        this.x = x;
    }

    protected int getButtonY() {
        return y;
    }

    protected void setButtonY(int y) {
        this.y = y;
    }

    protected int getTheButtonWidth() {
        return width;
    }

    protected int getTheButtonHeight() {
        return height;
    }

    protected void setButtonWidth(int width) {
        this.width = width;
    }

    protected void setButtonHeight(int height) {
        this.height = height;
    }

    protected boolean isButtonHovered() {
        return isHovered();
    }

    protected void setButtonHovered(boolean state) {
        ((IMixinGuiButton) this).setIsHovered(state);
    }

    public String getButtonText() {
        return getMessage();
    }

    public IGuiButton setButtonText(String text) {
        setMessage(text);
        return this;
    }

    public void resetToAfter(int ms, String text) {
        new Thread(() -> {
            Thread.currentThread().setName("Button reset thread");
            try {
                Thread.sleep(ms);
            } catch (InterruptedException ignored) { }
            setButtonText(text);
        }).start();
    }

}
@@ -1039,16 +1039,14 @@
 ble 
-double_1
+mouseX
 , do
@@ -1054,27 +1054,26 @@
 ble 
-double_2
+mouseY
 , int 
-int_1
+button
 ) %7B%0A
@@ -1088,152 +1088,55 @@
 if (
-this.active) %7B%0A            if (int_1 == 0) %7B%0A                boolean boolean_1 = this.isButtonHovered();%0A                if (boolean_1) %7B%0A      
+button == 0 && this.clicked(mouseX, mouseY)) %7B%0A
     
@@ -1139,26 +1139,24 @@
             
-  
 this.playDow
@@ -1224,24 +1224,16 @@
         
-        
 onButton
@@ -1253,32 +1253,24 @@
             
-        
 return true;
@@ -1278,24 +1278,16 @@
         
-        
 %7D%0A      
@@ -1292,93 +1292,21 @@
     
-    %7D%0A%0A            return false;%0A        %7D else %7B%0A            return false;%0A        %7D
+return false;
 %0A   

Fix button trigger