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 boolean positiveOnly = 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.yPosition -= 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) {
        this.positiveOnly = positiveOnly;
    }

    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);
            arrowDown.drawButton(minecraft, mouseX, mouseY);
            offsetX = arrowUp.width;
            xPosition += offsetX;
            width -= offsetX;
        }
        super.drawTextBox(minecraft, mouseX, mouseY);
        if(arrows) {
            xPosition -= offsetX;
            width += offsetX;
        }
    }

    protected int validateNumber(int number) {
        if(this.positiveOnly) {
            return Math.max(number, 0);
        } else {
            return number;
        }
    }

    protected int getDiffAmount() {
        return Minecraft.getMinecraft().thePlayer.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 void mouseClicked(int mouseX, int mouseY, int mouseButton) {
        if(arrowUp.mousePressed(Minecraft.getMinecraft(), mouseX, mouseY)) {
            increase();
        } else if(arrowDown.mousePressed(Minecraft.getMinecraft(), mouseX, mouseY)) {
            decrease();
        } else {
            super.mouseClicked(mouseX, mouseY, mouseButton);
        }
        arrowDown.enabled = true;
        try {
            if (positiveOnly && getInt() == 0) {
                arrowDown.enabled = false;
            }
        } catch (NumberFormatException e ) {

        }
    }

}

@@ -496,35 +496,86 @@
 ate 
-boolean positiveOnly = true
+int minValue = Integer.MIN_VALUE;%0A    private int maxValue = Integer.MAX_VALUE
 ;%0A%0A 
@@ -1382,21 +1382,28 @@
         
-this.
+setMinValue(
 positive
@@ -1411,22 +1411,340 @@
 nly 
-= positiveOnly
+? 0 : Integer.MIN_VALUE);%0A    %7D%0A%0A    /**%0A     * @param minValue The minimal value (inclusive)%0A     */%0A    public void setMinValue(int minValue) %7B%0A        this.minValue = minValue;%0A    %7D%0A%0A    /**%0A     * @param maxValue The maximal value (inclusive)%0A     */%0A    public void setMaxValue(int maxValue) %7B%0A        this.maxValue = maxValue
 ;%0A  
@@ -2659,125 +2659,72 @@
     
-if(this.positiveOnly) %7B%0A            return Math.max(number, 0);%0A        %7D else %7B%0A            return number;%0A        %7D
+return Math.max(this.minValue, Math.min(this.maxValue, number));
 %0A   
@@ -3706,46 +3706,164 @@
     
-try %7B%0A            if (positiveOnly && 
+arrowUp.enabled = true;%0A        try %7B%0A            if (getInt() %3C= this.minValue) %7B%0A                arrowDown.enabled = false;%0A            %7D%0A            if (
 getI
@@ -3871,12 +3871,24 @@
 t() 
-== 0
+%3E= this.maxValue
 ) %7B%0A
@@ -3900,36 +3900,34 @@
            arrow
-Down
+Up
 .enabled = false

Allow min and max values to be set in number fields