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

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import org.cyclops.cyclopscore.client.gui.component.button.GuiButtonArrow;
import org.cyclops.cyclopscore.helper.MinecraftHelpers;

/**
 * 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;
    private boolean isEnabled = true;

    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 void setEnabled(boolean enabled) {
        arrowUp.enabled = enabled;
        arrowDown.enabled = enabled;
        isEnabled = enabled;
        super.setEnabled(enabled);
    }

    @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);
    }

    public int getMinValue() {
        return minValue;
    }

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

    public int getMaxValue() {
        return maxValue;
    }

    /**
     * @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;
        GlStateManager.color(1, 1, 1, 1);
        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;
        }
    }

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

    protected int getDiffAmount() {
        return MinecraftHelpers.isShifted() ? 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) {
        if (this.isEnabled) {
            boolean ret = true;
            if (this.arrows && arrowUp.mousePressed(Minecraft.getMinecraft(), mouseX, mouseY)) {
                increase();
            } else if (this.arrows && arrowDown.mousePressed(Minecraft.getMinecraft(), mouseX, mouseY)) {
                decrease();
            } else {
                ret = super.mouseClicked(mouseX, mouseY, mouseButton);
            }
            if (this.arrows) {
                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;
        }
        return false;
    }

}

@@ -4337,32 +4337,285 @@
   %7D%0A            
+updateArrowsState();%0A            return ret;%0A        %7D%0A        return false;%0A    %7D%0A%0A    @Override%0A    public void setText(String value) %7B%0A        super.setText(value);%0A        updateArrowsState();%0A    %7D%0A%0A    protected void updateArrowsState() %7B%0A        
 if (this.arrows)
@@ -4609,36 +4609,32 @@
 (this.arrows) %7B%0A
-    
             arro
@@ -4659,36 +4659,32 @@
 ue;%0A            
- 
-   
 arrowUp.enabled 
@@ -4699,38 +4699,30 @@
             
-    
 try %7B%0A
-    
             
@@ -4770,36 +4770,32 @@
                 
-    
 arrowDown.enable
@@ -4817,33 +4817,26 @@
             
-    %7D%0A   
+%7D%0A
             
@@ -4831,33 +4831,32 @@
                 
- 
 if (getInt() %3E= 
@@ -4864,36 +4864,32 @@
 his.maxValue) %7B%0A
-    
                 
@@ -4929,32 +4929,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -4943,26 +4943,24 @@
             
-  
 %7D catch (Num
@@ -4988,36 +4988,32 @@
  %7B%0A%0A            
-    
 %7D%0A            %7D%0A
@@ -5010,69 +5010,9 @@
     
- 
-   %7D%0A            return ret;%0A        %7D%0A        return false;
+%7D
 %0A   

Update arrows state in GuiNumberField when changing text directly