package org.cyclops.cyclopscore.client.gui.component.input;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import org.cyclops.cyclopscore.client.gui.component.button.GuiButtonArrow;

/**
 * A number field which by default only accepts positive numbers.
 * @author rubensworks
 */
public class GuiNumberField extends GuiTextFieldExtended {

    private final boolean arrows;
    private GuiButtonArrow arrowUp;
    private GuiButtonArrow arrowDown;
    private int minValue = Integer.MIN_VALUE;
    private int maxValue = Integer.MAX_VALUE;

    public GuiNumberField(int componentId, FontRenderer fontrenderer, int x, int y, int width, int height, boolean arrows, boolean background) {
        super(componentId, fontrenderer, x, y, width, height, background);
        this.arrows = arrows;

        if(this.arrows) {
            arrowUp = new GuiButtonArrow(0, x, y + height / 2, GuiButtonArrow.Direction.NORTH);
            arrowDown = new GuiButtonArrow(1, x, y + height / 2, GuiButtonArrow.Direction.SOUTH);
            arrowUp.y -= arrowUp.height;
        }
        setEnableBackgroundDrawing(true);
        setText("0");
    }

    @Override
    public boolean getEnableBackgroundDrawing() {
        return false; // We want the offset, but not the drawing itself.
    }

    public void setPositiveOnly(boolean positiveOnly) {
        setMinValue(positiveOnly ? 0 : Integer.MIN_VALUE);
    }

    /**
     * @param minValue The minimal value (inclusive)
     */
    public void setMinValue(int minValue) {
        this.minValue = minValue;
    }

    /**
     * @param maxValue The maximal value (inclusive)
     */
    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    public int getInt() throws NumberFormatException {
        return Integer.parseInt(getText());
    }

    public double getDouble() throws NumberFormatException {
        return Double.parseDouble(getText());
    }

    public float getFloat() throws NumberFormatException {
        return Float.parseFloat(getText());
    }

    @Override
    public void drawTextBox(Minecraft minecraft, int mouseX, int mouseY) {
        int offsetX = 0;
        if(arrows) {
            arrowUp.drawButton(minecraft, mouseX, mouseY, minecraft.getRenderPartialTicks());
            arrowDown.drawButton(minecraft, mouseX, mouseY, minecraft.getRenderPartialTicks());
            offsetX = arrowUp.width;
            x += offsetX;
            width -= offsetX;
        }
        super.drawTextBox(minecraft, mouseX, mouseY);
        if(arrows) {
            x -= offsetX;
            width += offsetX;
        }
    }

    protected int validateNumber(int number) {
        return Math.max(this.minValue, Math.min(this.maxValue, number));
    }

    protected int getDiffAmount() {
        return Minecraft.getMinecraft().player.isSneaking() ? 10 : 1;
    }

    protected void increase() {
        try {
            setText(Integer.toString(validateNumber(getInt() + getDiffAmount())));
        } catch (NumberFormatException e) {
            setText("0");
        }
    }

    protected void decrease() {
        try {
            setText(Integer.toString(validateNumber(getInt() - getDiffAmount())));
        } catch (NumberFormatException e) {
            setText("0");
        }
    }

    @Override
    public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) {
        boolean ret = true;
        if(arrowUp.mousePressed(Minecraft.getMinecraft(), mouseX, mouseY)) {
            increase();
        } else if(arrowDown.mousePressed(Minecraft.getMinecraft(), mouseX, mouseY)) {
            decrease();
        } else {
            ret = super.mouseClicked(mouseX, mouseY, mouseButton);
        }
        arrowDown.enabled = true;
        arrowUp.enabled = true;
        try {
            if (getInt() <= this.minValue) {
                arrowDown.enabled = false;
            }
            if (getInt() >= this.maxValue) {
                arrowUp.enabled = false;
            }
        } catch (NumberFormatException e ) {

        }
        return ret;
    }

}

@@ -571,16 +571,54 @@
 X_VALUE;
+%0A    private boolean isEnabled = true;
 %0A%0A    pu
@@ -1196,32 +1196,235 @@
 xt(%220%22);%0A    %7D%0A%0A
+    @Override%0A    public void setEnabled(boolean enabled) %7B%0A        arrowUp.enabled = enabled;%0A        arrowDown.enabled = enabled;%0A        isEnabled = enabled;%0A        super.setEnabled(enabled);%0A    %7D%0A%0A
     @Override%0A  
@@ -3644,24 +3644,58 @@
 seButton) %7B%0A
+        if (this.isEnabled) %7B%0A    
         bool
@@ -3714,26 +3714,31 @@
 ue;%0A        
+   
+ 
 if
+ 
 (arrowUp.mou
@@ -3804,16 +3804,20 @@
         
+    
 increase
@@ -3824,24 +3824,28 @@
 ();%0A        
+   
+ 
 %7D else if(ar
@@ -3841,16 +3841,17 @@
  else if
+ 
 (arrowDo
@@ -3923,16 +3923,20 @@
         
+    
 decrease
@@ -3943,24 +3943,28 @@
 ();%0A        
+   
+ 
 %7D else %7B%0A   
@@ -3960,16 +3960,20 @@
  else %7B%0A
+    
         
@@ -4031,16 +4031,20 @@
 utton);%0A
+    
         
@@ -4045,32 +4045,36 @@
       %7D%0A        
+    
 arrowDown.enable
@@ -4083,32 +4083,36 @@
 = true;%0A        
+    
 arrowUp.enabled 
@@ -4119,38 +4119,46 @@
 = true;%0A        
+ 
+   
 try %7B%0A
+    
             if (
@@ -4194,32 +4194,36 @@
                 
+  
+  
 arrowDown.enable
@@ -4237,34 +4237,41 @@
 se;%0A            
-%7D%0A
+    %7D%0A   
             if (
@@ -4258,32 +4258,33 @@
 %0A               
+ 
 if (getInt() %3E= 
@@ -4308,32 +4308,36 @@
                 
+    
 arrowUp.enabled 
@@ -4337,32 +4337,36 @@
 nabled = false;%0A
+    
             %7D%0A  
@@ -4363,32 +4363,36 @@
       %7D%0A        
+    
 %7D catch (NumberF
@@ -4411,14 +4411,17 @@
 on e
- 
 ) %7B%0A%0A
+    
     
@@ -4430,24 +4430,28 @@
   %7D%0A        
+    
 return ret;%0A
@@ -4450,16 +4450,48 @@
 rn ret;%0A
+        %7D%0A        return false;%0A
     %7D%0A%0A%7D

Fix disabled numbered fields still having enabled buttons